TipTap extension providing subscript text formatting with commands, keyboard shortcuts, and HTML parsing/rendering.
npx @tessl/cli install tessl/npm-tiptap--extension-subscript@3.4.0The @tiptap/extension-subscript package provides subscript text formatting functionality for TipTap rich text editors. This extension allows users to create subscript text with keyboard shortcuts, programmatic commands, and automatic HTML parsing/rendering.
npm install @tiptap/extension-subscript@tiptap/core, @tiptap/pmimport { Subscript, SubscriptExtensionOptions } from '@tiptap/extension-subscript';Default import:
import Subscript from '@tiptap/extension-subscript';CommonJS:
const { Subscript } = require('@tiptap/extension-subscript');import { Editor } from '@tiptap/core';
import Subscript from '@tiptap/extension-subscript';
// Create editor with subscript extension
const editor = new Editor({
extensions: [
Subscript.configure({
HTMLAttributes: {
class: 'my-subscript',
},
}),
],
content: '<p>H<sub>2</sub>O is water</p>',
});
// Use programmatic commands
editor.commands.toggleSubscript();
editor.commands.setSubscript();
editor.commands.unsetSubscript();
// Use keyboard shortcut: Mod-,The main extension class that creates subscript functionality in TipTap editors.
/**
* TipTap Mark extension that provides subscript text formatting
*/
declare const Subscript: Mark<SubscriptExtensionOptions>;Options interface for configuring the subscript extension.
interface SubscriptExtensionOptions {
/**
* HTML attributes to add to the subscript element.
* @default {}
* @example { class: 'foo' }
*/
HTMLAttributes: Record<string, any>;
}Commands added to the TipTap editor for manipulating subscript marks.
interface Commands<ReturnType> {
subscript: {
/**
* Set a subscript mark on the current selection
* @example editor.commands.setSubscript()
*/
setSubscript: () => ReturnType;
/**
* Toggle a subscript mark on the current selection
* @example editor.commands.toggleSubscript()
*/
toggleSubscript: () => ReturnType;
/**
* Remove subscript mark from the current selection
* @example editor.commands.unsetSubscript()
*/
unsetSubscript: () => ReturnType;
};
}The extension automatically parses:
<sub> HTML tags: H<sub>2</sub>Overtical-align: sub styles: <span style="vertical-align: sub">2</span>All subscript content is rendered as <sub> elements with merged HTML attributes:
<sub class="my-subscript">2</sub>import { Editor } from '@tiptap/core';
import Subscript from '@tiptap/extension-subscript';
const editor = new Editor({
extensions: [Subscript],
content: 'Type H2O and select the 2, then press Cmd-,',
});
// Programmatically apply subscript
editor.chain().focus().setSubscript().run();const editor = new Editor({
extensions: [
Subscript.configure({
HTMLAttributes: {
class: 'subscript-text',
'data-type': 'subscript',
},
}),
],
});// Perfect for scientific content
const chemistryEditor = new Editor({
extensions: [Subscript],
content: `
<p>Water: H<sub>2</sub>O</p>
<p>Carbon dioxide: CO<sub>2</sub></p>
<p>Mathematical notation: x<sub>n</sub></p>
`,
});interface SubscriptExtensionOptions {
/**
* HTML attributes to add to the subscript element.
* @default {}
* @example { class: 'foo' }
*/
HTMLAttributes: Record<string, any>;
}
type ReturnType = boolean;
interface Mark<T = any> {
create<O extends Record<string, any>>(config: any): Mark<O>;
configure(options?: Partial<T>): Mark<T>;
}