@tiptap/extension-mathematics 3.6.7 → 3.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +61 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +61 -0
- package/dist/index.js.map +1 -1
- package/package.json +5 -5
- package/src/extensions/BlockMath.ts +37 -0
- package/src/extensions/InlineMath.ts +36 -0
package/dist/index.cjs
CHANGED
|
@@ -119,6 +119,37 @@ var BlockMath = import_core.Node.create({
|
|
|
119
119
|
renderHTML({ HTMLAttributes }) {
|
|
120
120
|
return ["div", (0, import_core.mergeAttributes)(HTMLAttributes, { "data-type": "block-math" })];
|
|
121
121
|
},
|
|
122
|
+
parseMarkdown: (token) => {
|
|
123
|
+
return {
|
|
124
|
+
type: "blockMath",
|
|
125
|
+
attrs: {
|
|
126
|
+
latex: token.latex
|
|
127
|
+
}
|
|
128
|
+
};
|
|
129
|
+
},
|
|
130
|
+
renderMarkdown: (node) => {
|
|
131
|
+
var _a;
|
|
132
|
+
const latex = ((_a = node.attrs) == null ? void 0 : _a.latex) || "";
|
|
133
|
+
const output = ["$$", latex, "$$"];
|
|
134
|
+
return output.join("\n");
|
|
135
|
+
},
|
|
136
|
+
markdownTokenizer: {
|
|
137
|
+
name: "blockMath",
|
|
138
|
+
level: "block",
|
|
139
|
+
start: (src) => src.indexOf("$$"),
|
|
140
|
+
tokenize: (src) => {
|
|
141
|
+
const match = src.match(/^\$\$([^$]+)\$\$/);
|
|
142
|
+
if (!match) {
|
|
143
|
+
return void 0;
|
|
144
|
+
}
|
|
145
|
+
const [fullMatch, latex] = match;
|
|
146
|
+
return {
|
|
147
|
+
type: "blockMath",
|
|
148
|
+
raw: fullMatch,
|
|
149
|
+
latex: latex.trim()
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
},
|
|
122
153
|
addInputRules() {
|
|
123
154
|
return [
|
|
124
155
|
new import_core.InputRule({
|
|
@@ -254,6 +285,36 @@ var InlineMath = import_core2.Node.create({
|
|
|
254
285
|
renderHTML({ HTMLAttributes }) {
|
|
255
286
|
return ["span", (0, import_core2.mergeAttributes)(HTMLAttributes, { "data-type": "inline-math" })];
|
|
256
287
|
},
|
|
288
|
+
parseMarkdown: (token) => {
|
|
289
|
+
return {
|
|
290
|
+
type: "inlineMath",
|
|
291
|
+
attrs: {
|
|
292
|
+
latex: token.latex
|
|
293
|
+
}
|
|
294
|
+
};
|
|
295
|
+
},
|
|
296
|
+
renderMarkdown: (node) => {
|
|
297
|
+
var _a;
|
|
298
|
+
const latex = ((_a = node.attrs) == null ? void 0 : _a.latex) || "";
|
|
299
|
+
return `$${latex}$`;
|
|
300
|
+
},
|
|
301
|
+
markdownTokenizer: {
|
|
302
|
+
name: "inlineMath",
|
|
303
|
+
level: "inline",
|
|
304
|
+
start: (src) => src.indexOf("$"),
|
|
305
|
+
tokenize: (src) => {
|
|
306
|
+
const match = src.match(/^\$([^$]+)\$(?!\$)/);
|
|
307
|
+
if (!match) {
|
|
308
|
+
return void 0;
|
|
309
|
+
}
|
|
310
|
+
const [fullMatch, latex] = match;
|
|
311
|
+
return {
|
|
312
|
+
type: "inlineMath",
|
|
313
|
+
raw: fullMatch,
|
|
314
|
+
latex: latex.trim()
|
|
315
|
+
};
|
|
316
|
+
}
|
|
317
|
+
},
|
|
257
318
|
addInputRules() {
|
|
258
319
|
return [
|
|
259
320
|
new import_core2.InputRule({
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts","../src/mathematics.ts","../src/extensions/BlockMath.ts","../src/extensions/InlineMath.ts","../src/utils.ts"],"sourcesContent":["import { Mathematics } from './mathematics.js'\n\nexport * from './extensions/index.js'\nexport * from './mathematics.js'\nexport * from './types.js'\nexport * from './utils.js'\n\nexport default Mathematics\n","import { Extension } from '@tiptap/core'\n\nimport { BlockMath, InlineMath } from './extensions/index.js'\nimport type { MathematicsOptions } from './types.js'\n\n/**\n * Mathematics extension for Tiptap that provides both inline and block math support using KaTeX.\n * This extension combines the InlineMath and BlockMath extensions to provide a complete\n * mathematical expression solution for rich text editing. It supports LaTeX syntax,\n * custom rendering options, and interactive math nodes.\n *\n * @example\n * ```typescript\n * import { Editor } from '@tiptap/core'\n * import { Mathematics } from '@tiptap/extension-mathematics'\n * import { migrateMathStrings } from '@tiptap/extension-mathematics/utils'\n *\n * const editor = new Editor({\n * extensions: [\n * Mathematics.configure({\n * inlineOptions: {\n * onClick: (node, pos) => {\n * console.log('Inline math clicked:', node.attrs.latex)\n * }\n * },\n * blockOptions: {\n * onClick: (node, pos) => {\n * console.log('Block math clicked:', node.attrs.latex)\n * }\n * },\n * katexOptions: {\n * displayMode: false,\n * throwOnError: false,\n * macros: {\n * '\\\\RR': '\\\\mathbb{R}',\n * '\\\\ZZ': '\\\\mathbb{Z}'\n * }\n * }\n * })\n * ],\n * content: `\n * <p>Inline math: $E = mc^2$</p>\n * <div data-type=\"block-math\" data-latex=\"\\\\sum_{i=1}^{n} x_i = X\"></div>\n * `,\n * onCreate({ editor }) {\n * // Optional: Migrate existing math strings to math nodes\n * migrateMathStrings(editor)\n * }\n * })\n * ```\n */\nexport const Mathematics = Extension.create<MathematicsOptions>({\n name: 'Mathematics',\n\n addOptions() {\n return {\n inlineOptions: undefined,\n blockOptions: undefined,\n katexOptions: undefined,\n }\n },\n\n addExtensions() {\n return [\n BlockMath.configure({ ...this.options.blockOptions, katexOptions: this.options.katexOptions }),\n InlineMath.configure({ ...this.options.inlineOptions, katexOptions: this.options.katexOptions }),\n ]\n },\n})\n\nexport default Mathematics\n","import { InputRule, mergeAttributes, Node } from '@tiptap/core'\nimport type { Node as PMNode } from '@tiptap/pm/model'\nimport katex, { type KatexOptions } from 'katex'\n\n/**\n * Configuration options for the BlockMath extension.\n */\nexport type BlockMathOptions = {\n /**\n * KaTeX specific options\n * @see https://katex.org/docs/options.html\n * @example\n * ```ts\n * katexOptions: {\n * displayMode: true,\n * throwOnError: false,\n * },\n */\n katexOptions?: KatexOptions\n\n /**\n * Optional click handler for block math nodes.\n * Called when a user clicks on a block math expression in the editor.\n *\n * @param node - The ProseMirror node representing the block math element\n * @param pos - The position of the node within the document\n * @example\n * ```ts\n * onClick: (node, pos) => {\n * console.log('Block math clicked:', node.attrs.latex, 'at position:', pos)\n * },\n * ```\n */\n onClick?: (node: PMNode, pos: number) => void\n}\n\ndeclare module '@tiptap/core' {\n interface Commands<ReturnType> {\n insertBlockMath: {\n /**\n * Inserts a math block node with LaTeX string.\n * @param options - Options for inserting block math.\n * @returns ReturnType\n */\n insertBlockMath: (options: { latex: string; pos?: number }) => ReturnType\n\n /**\n * Deletes a block math node.\n * @returns ReturnType\n */\n deleteBlockMath: (options?: { pos?: number }) => ReturnType\n\n /**\n * Update block math node with optional LaTeX string.\n * @param options - Options for updating block math.\n * @returns ReturnType\n */\n updateBlockMath: (options?: { latex: string; pos?: number }) => ReturnType\n }\n }\n}\n\n/**\n * BlockMath is a Tiptap extension for rendering block mathematical expressions using KaTeX.\n * It allows users to insert LaTeX formatted math expressions block within text.\n * It supports rendering, input rules for LaTeX syntax, and click handling for interaction.\n *\n * @example\n * ```javascript\n * import { BlockMath } from '@tiptap/extension-mathematics'\n * import { Editor } from '@tiptap/core'\n *\n * const editor = new Editor({\n * extensions: [\n * BlockMath.configure({\n * onClick: (node, pos) => {\n * console.log('Block math clicked:', node.attrs.latex, 'at position:', pos)\n * },\n * }),\n * ],\n * })\n */\nexport const BlockMath = Node.create<BlockMathOptions>({\n name: 'blockMath',\n\n group: 'block',\n\n atom: true,\n\n addOptions() {\n return {\n onClick: undefined,\n katexOptions: undefined,\n }\n },\n\n addAttributes() {\n return {\n latex: {\n default: '',\n parseHTML: element => element.getAttribute('data-latex'),\n renderHTML: attributes => {\n return {\n 'data-latex': attributes.latex,\n }\n },\n },\n }\n },\n\n addCommands() {\n return {\n insertBlockMath:\n options =>\n ({ commands, editor }) => {\n const { latex, pos } = options\n\n if (!latex) {\n return false\n }\n\n return commands.insertContentAt(pos ?? editor.state.selection.from, {\n type: this.name,\n attrs: { latex },\n })\n },\n\n deleteBlockMath:\n options =>\n ({ editor, tr }) => {\n const pos = options?.pos ?? editor.state.selection.$from.pos\n const node = editor.state.doc.nodeAt(pos)\n\n if (!node || node.type.name !== this.name) {\n return false\n }\n\n tr.delete(pos, pos + node.nodeSize)\n return true\n },\n\n updateBlockMath:\n options =>\n ({ editor, tr }) => {\n const latex = options?.latex\n let pos = options?.pos\n\n if (pos === undefined) {\n pos = editor.state.selection.$from.pos\n }\n\n const node = editor.state.doc.nodeAt(pos)\n\n if (!node || node.type.name !== this.name) {\n return false\n }\n\n tr.setNodeMarkup(pos, this.type, {\n ...node.attrs,\n latex: latex || node.attrs.latex,\n })\n\n return true\n },\n }\n },\n\n parseHTML() {\n return [\n {\n tag: 'div[data-type=\"block-math\"]',\n },\n ]\n },\n\n renderHTML({ HTMLAttributes }) {\n return ['div', mergeAttributes(HTMLAttributes, { 'data-type': 'block-math' })]\n },\n\n addInputRules() {\n return [\n new InputRule({\n find: /^\\$\\$\\$([^$]+)\\$\\$\\$$/,\n handler: ({ state, range, match }) => {\n const [, latex] = match\n const { tr } = state\n const start = range.from\n const end = range.to\n\n tr.replaceWith(start, end, this.type.create({ latex }))\n },\n }),\n ]\n },\n\n addNodeView() {\n const { katexOptions } = this.options\n\n return ({ node, getPos }) => {\n const wrapper = document.createElement('div')\n const innerWrapper = document.createElement('div')\n wrapper.className = 'tiptap-mathematics-render'\n\n if (this.editor.isEditable) {\n wrapper.classList.add('tiptap-mathematics-render--editable')\n }\n\n innerWrapper.className = 'block-math-inner'\n wrapper.dataset.type = 'block-math'\n wrapper.setAttribute('data-latex', node.attrs.latex)\n wrapper.appendChild(innerWrapper)\n\n function renderMath() {\n try {\n katex.render(node.attrs.latex, innerWrapper, katexOptions)\n wrapper.classList.remove('block-math-error')\n } catch {\n wrapper.textContent = node.attrs.latex\n wrapper.classList.add('block-math-error')\n }\n }\n\n const handleClick = (event: MouseEvent) => {\n event.preventDefault()\n event.stopPropagation()\n const pos = getPos()\n\n if (pos == null) {\n return\n }\n\n if (this.options.onClick) {\n this.options.onClick(node, pos)\n }\n }\n\n if (this.options.onClick) {\n wrapper.addEventListener('click', handleClick)\n }\n\n renderMath()\n\n return {\n dom: wrapper,\n destroy() {\n wrapper.removeEventListener('click', handleClick)\n },\n }\n }\n },\n})\n","import { InputRule, mergeAttributes, Node } from '@tiptap/core'\nimport type { Node as PMNode } from '@tiptap/pm/model'\nimport katex, { type KatexOptions } from 'katex'\n\n/**\n * Configuration options for the InlineMath extension.\n */\nexport type InlineMathOptions = {\n /**\n * KaTeX specific options\n * @see https://katex.org/docs/options.html\n * @example\n * ```ts\n * katexOptions: {\n * displayMode: false,\n * throwOnError: false,\n * macros: {\n * '\\\\RR': '\\\\mathbb{R}',\n * '\\\\ZZ': '\\\\mathbb{Z}'\n * }\n * }\n * ```\n */\n katexOptions?: KatexOptions\n\n /**\n * Optional click handler for inline math nodes.\n * Called when a user clicks on an inline math expression in the editor.\n *\n * @param node - The ProseMirror node representing the inline math element\n * @param pos - The position of the node within the document\n * @example\n * ```ts\n * onClick: (node, pos) => {\n * console.log('Inline math clicked:', node.attrs.latex, 'at position:', pos)\n * }\n * ```\n */\n onClick?: (node: PMNode, pos: number) => void\n}\n\ndeclare module '@tiptap/core' {\n interface Commands<ReturnType> {\n inlineMath: {\n /**\n * Insert a inline math node with LaTeX string.\n * @param options - Options for inserting inline math.\n * @returns ReturnType\n */\n insertInlineMath: (options: { latex: string; pos?: number }) => ReturnType\n\n /**\n * Delete an inline math node.\n * @returns ReturnType\n */\n deleteInlineMath: (options?: { pos?: number }) => ReturnType\n\n /**\n * Update inline math node with optional LaTeX string.\n * @param options - Options for updating inline math.\n * @returns ReturnType\n */\n updateInlineMath: (options?: { latex?: string; pos?: number }) => ReturnType\n }\n }\n}\n\n/**\n * InlineMath is a Tiptap extension for rendering inline mathematical expressions using KaTeX.\n * It allows users to insert LaTeX formatted math expressions inline within text.\n * It supports rendering, input rules for LaTeX syntax, and click handling for interaction.\n *\n * @example\n * ```javascript\n * import { InlineMath } from '@tiptap/extension-mathematics'\n * import { Editor } from '@tiptap/core'\n *\n * const editor = new Editor({\n * extensions: [\n * InlineMath.configure({\n * onClick: (node, pos) => {\n * console.log('Inline math clicked:', node.attrs.latex, 'at position:', pos)\n * },\n * }),\n * ],\n * })\n */\nexport const InlineMath = Node.create<InlineMathOptions>({\n name: 'inlineMath',\n\n group: 'inline',\n\n inline: true,\n\n atom: true,\n\n addOptions() {\n return {\n onClick: undefined,\n katexOptions: undefined,\n }\n },\n\n addAttributes() {\n return {\n latex: {\n default: '',\n parseHTML: element => element.getAttribute('data-latex'),\n renderHTML: attributes => {\n return {\n 'data-latex': attributes.latex,\n }\n },\n },\n }\n },\n\n addCommands() {\n return {\n insertInlineMath:\n options =>\n ({ editor, tr }) => {\n const latex = options.latex\n\n const from = options?.pos ?? editor.state.selection.from\n\n if (!latex) {\n return false\n }\n\n tr.replaceWith(from, from, this.type.create({ latex }))\n return true\n },\n\n deleteInlineMath:\n options =>\n ({ editor, tr }) => {\n const pos = options?.pos ?? editor.state.selection.$from.pos\n const node = editor.state.doc.nodeAt(pos)\n\n if (!node || node.type.name !== this.name) {\n return false\n }\n\n tr.delete(pos, pos + node.nodeSize)\n return true\n },\n\n updateInlineMath:\n options =>\n ({ editor, tr }) => {\n const latex = options?.latex\n let pos = options?.pos\n\n if (pos === undefined) {\n pos = editor.state.selection.$from.pos\n }\n\n const node = editor.state.doc.nodeAt(pos)\n\n if (!node || node.type.name !== this.name) {\n return false\n }\n\n tr.setNodeMarkup(pos, this.type, { ...node.attrs, latex })\n\n return true\n },\n }\n },\n\n parseHTML() {\n return [\n {\n tag: 'span[data-type=\"inline-math\"]',\n },\n ]\n },\n\n renderHTML({ HTMLAttributes }) {\n return ['span', mergeAttributes(HTMLAttributes, { 'data-type': 'inline-math' })]\n },\n\n addInputRules() {\n return [\n new InputRule({\n find: /(^|[^$])(\\$\\$([^$\\n]+?)\\$\\$)(?!\\$)/,\n handler: ({ state, range, match }) => {\n const latex = match[3]\n const { tr } = state\n const start = range.from\n const end = range.to\n\n tr.replaceWith(start, end, this.type.create({ latex }))\n },\n }),\n ]\n },\n\n addNodeView() {\n const { katexOptions } = this.options\n\n return ({ node, getPos }) => {\n const wrapper = document.createElement('span')\n wrapper.className = 'tiptap-mathematics-render'\n\n if (this.editor.isEditable) {\n wrapper.classList.add('tiptap-mathematics-render--editable')\n }\n\n wrapper.dataset.type = 'inline-math'\n wrapper.setAttribute('data-latex', node.attrs.latex)\n\n function renderMath() {\n try {\n katex.render(node.attrs.latex, wrapper, katexOptions)\n wrapper.classList.remove('inline-math-error')\n } catch {\n wrapper.textContent = node.attrs.latex\n wrapper.classList.add('inline-math-error')\n }\n }\n\n const handleClick = (event: MouseEvent) => {\n event.preventDefault()\n event.stopPropagation()\n const pos = getPos()\n\n if (pos == null) {\n return\n }\n\n if (this.options.onClick) {\n this.options.onClick(node, pos)\n }\n }\n\n if (this.options.onClick) {\n wrapper.addEventListener('click', handleClick)\n }\n\n renderMath()\n\n return {\n dom: wrapper,\n destroy() {\n wrapper.removeEventListener('click', handleClick)\n },\n }\n }\n },\n})\n","import type { Editor } from '@tiptap/core'\nimport type { Transaction } from '@tiptap/pm/state'\n\n/**\n * Regular expression to match LaTeX math strings wrapped in single dollar signs.\n * This should not catch dollar signs which are not part of a math expression,\n * like those used for currency or other purposes.\n * It ensures that the dollar signs are not preceded or followed by digits,\n * allowing for proper identification of inline math expressions.\n *\n * - `$x^2 + y^2 = z^2$` will match\n * - `This is $inline math$ in text.` will match\n * - `This is $100$ dollars.` will not match (as it is not a math expression)\n * - `This is $x^2 + y^2 = z^2$ and $100$ dollars.` will match both math expressions\n */\nexport const mathMigrationRegex = /\\$(?!\\d+\\$)(.+?)\\$(?!\\d)/g\n\n/**\n * Creates a transaction that migrates existing math strings in the document to new math nodes.\n * This function traverses the document and replaces LaTeX math syntax (wrapped in single dollar signs)\n * with proper inline math nodes, preserving the mathematical content.\n *\n * @param editor - The editor instance containing the schema and configuration\n * @param tr - The transaction to modify with the migration operations\n * @returns The modified transaction with math string replacements\n *\n * @example\n * ```typescript\n * const editor = new Editor({ ... })\n * const tr = editor.state.tr\n * const updatedTr = createMathMigrateTransaction(editor, tr)\n * editor.view.dispatch(updatedTr)\n * ```\n */\nexport function createMathMigrateTransaction(editor: Editor, tr: Transaction, regex: RegExp = mathMigrationRegex) {\n // we traverse the document and replace all math nodes with the new math nodes\n tr.doc.descendants((node, pos) => {\n if (!node.isText || !node.text || !node.text.includes('$')) {\n return\n }\n\n const { text } = node\n\n const match = node.text.match(regex)\n if (!match) {\n return\n }\n\n match.forEach(mathMatch => {\n const start = text.indexOf(mathMatch)\n const end = start + mathMatch.length\n\n const from = tr.mapping.map(pos + start)\n\n const $from = tr.doc.resolve(from)\n const parent = $from.parent\n const index = $from.index()\n\n const { inlineMath } = editor.schema.nodes\n\n if (!parent.canReplaceWith(index, index + 1, inlineMath)) {\n return\n }\n\n // Replace the math syntax with a new math node\n tr.replaceWith(\n tr.mapping.map(pos + start),\n tr.mapping.map(pos + end),\n inlineMath.create({ latex: mathMatch.slice(1, -1) }),\n )\n })\n })\n\n // don't add to history\n tr.setMeta('addToHistory', false)\n return tr\n}\n\n/**\n * Migrates existing math strings in the editor document to math nodes.\n * This function creates and dispatches a transaction that converts LaTeX math syntax\n * (text wrapped in single dollar signs) into proper inline math nodes. The migration\n * happens immediately and is not added to the editor's history.\n *\n * @param editor - The editor instance to perform the migration on\n *\n * @example\n * ```typescript\n * const editor = new Editor({\n * extensions: [Mathematics],\n * content: 'This is inline math: $x^2 + y^2 = z^2$ in text.'\n * })\n *\n * // Math strings will be automatically migrated to math nodes\n * migrateMathStrings(editor)\n * ```\n */\nexport function migrateMathStrings(editor: Editor, regex: RegExp = mathMigrationRegex) {\n const tr = createMathMigrateTransaction(editor, editor.state.tr, regex)\n editor.view.dispatch(tr)\n}\n"],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AAE1B,YAAM,EAAE,WAAW,IAAI,OAAO,OAAO;AAErC,UAAI,CAAC,OAAO,eAAe,OAAO,QAAQ,GAAG,UAAU,GAAG;AACxD;AAAA,MACF;AAGA,SAAG;AAAA,QACD,GAAG,QAAQ,IAAI,MAAM,KAAK;AAAA,QAC1B,GAAG,QAAQ,IAAI,MAAM,GAAG;AAAA,QACxB,WAAW,OAAO,EAAE,OAAO,UAAU,MAAM,GAAG,EAAE,EAAE,CAAC;AAAA,MACrD;AAAA,IACF,CAAC;AAAA,EACH,CAAC;AAGD,KAAG,QAAQ,gBAAgB,KAAK;AAChC,SAAO;AACT;AAqBO,SAAS,mBAAmB,QAAgB,QAAgB,oBAAoB;AACrF,QAAM,KAAK,6BAA6B,QAAQ,OAAO,MAAM,IAAI,KAAK;AACtE,SAAO,KAAK,SAAS,EAAE;AACzB;;;AJ7FA,IAAO,gBAAQ;","names":["import_core","katex","import_core","import_katex","katex"]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/mathematics.ts","../src/extensions/BlockMath.ts","../src/extensions/InlineMath.ts","../src/utils.ts"],"sourcesContent":["import { Mathematics } from './mathematics.js'\n\nexport * from './extensions/index.js'\nexport * from './mathematics.js'\nexport * from './types.js'\nexport * from './utils.js'\n\nexport default Mathematics\n","import { Extension } from '@tiptap/core'\n\nimport { BlockMath, InlineMath } from './extensions/index.js'\nimport type { MathematicsOptions } from './types.js'\n\n/**\n * Mathematics extension for Tiptap that provides both inline and block math support using KaTeX.\n * This extension combines the InlineMath and BlockMath extensions to provide a complete\n * mathematical expression solution for rich text editing. It supports LaTeX syntax,\n * custom rendering options, and interactive math nodes.\n *\n * @example\n * ```typescript\n * import { Editor } from '@tiptap/core'\n * import { Mathematics } from '@tiptap/extension-mathematics'\n * import { migrateMathStrings } from '@tiptap/extension-mathematics/utils'\n *\n * const editor = new Editor({\n * extensions: [\n * Mathematics.configure({\n * inlineOptions: {\n * onClick: (node, pos) => {\n * console.log('Inline math clicked:', node.attrs.latex)\n * }\n * },\n * blockOptions: {\n * onClick: (node, pos) => {\n * console.log('Block math clicked:', node.attrs.latex)\n * }\n * },\n * katexOptions: {\n * displayMode: false,\n * throwOnError: false,\n * macros: {\n * '\\\\RR': '\\\\mathbb{R}',\n * '\\\\ZZ': '\\\\mathbb{Z}'\n * }\n * }\n * })\n * ],\n * content: `\n * <p>Inline math: $E = mc^2$</p>\n * <div data-type=\"block-math\" data-latex=\"\\\\sum_{i=1}^{n} x_i = X\"></div>\n * `,\n * onCreate({ editor }) {\n * // Optional: Migrate existing math strings to math nodes\n * migrateMathStrings(editor)\n * }\n * })\n * ```\n */\nexport const Mathematics = Extension.create<MathematicsOptions>({\n name: 'Mathematics',\n\n addOptions() {\n return {\n inlineOptions: undefined,\n blockOptions: undefined,\n katexOptions: undefined,\n }\n },\n\n addExtensions() {\n return [\n BlockMath.configure({ ...this.options.blockOptions, katexOptions: this.options.katexOptions }),\n InlineMath.configure({ ...this.options.inlineOptions, katexOptions: this.options.katexOptions }),\n ]\n },\n})\n\nexport default Mathematics\n","import { InputRule, mergeAttributes, Node } from '@tiptap/core'\nimport type { Node as PMNode } from '@tiptap/pm/model'\nimport katex, { type KatexOptions } from 'katex'\n\n/**\n * Configuration options for the BlockMath extension.\n */\nexport type BlockMathOptions = {\n /**\n * KaTeX specific options\n * @see https://katex.org/docs/options.html\n * @example\n * ```ts\n * katexOptions: {\n * displayMode: true,\n * throwOnError: false,\n * },\n */\n katexOptions?: KatexOptions\n\n /**\n * Optional click handler for block math nodes.\n * Called when a user clicks on a block math expression in the editor.\n *\n * @param node - The ProseMirror node representing the block math element\n * @param pos - The position of the node within the document\n * @example\n * ```ts\n * onClick: (node, pos) => {\n * console.log('Block math clicked:', node.attrs.latex, 'at position:', pos)\n * },\n * ```\n */\n onClick?: (node: PMNode, pos: number) => void\n}\n\ndeclare module '@tiptap/core' {\n interface Commands<ReturnType> {\n insertBlockMath: {\n /**\n * Inserts a math block node with LaTeX string.\n * @param options - Options for inserting block math.\n * @returns ReturnType\n */\n insertBlockMath: (options: { latex: string; pos?: number }) => ReturnType\n\n /**\n * Deletes a block math node.\n * @returns ReturnType\n */\n deleteBlockMath: (options?: { pos?: number }) => ReturnType\n\n /**\n * Update block math node with optional LaTeX string.\n * @param options - Options for updating block math.\n * @returns ReturnType\n */\n updateBlockMath: (options?: { latex: string; pos?: number }) => ReturnType\n }\n }\n}\n\n/**\n * BlockMath is a Tiptap extension for rendering block mathematical expressions using KaTeX.\n * It allows users to insert LaTeX formatted math expressions block within text.\n * It supports rendering, input rules for LaTeX syntax, and click handling for interaction.\n *\n * @example\n * ```javascript\n * import { BlockMath } from '@tiptap/extension-mathematics'\n * import { Editor } from '@tiptap/core'\n *\n * const editor = new Editor({\n * extensions: [\n * BlockMath.configure({\n * onClick: (node, pos) => {\n * console.log('Block math clicked:', node.attrs.latex, 'at position:', pos)\n * },\n * }),\n * ],\n * })\n */\nexport const BlockMath = Node.create<BlockMathOptions>({\n name: 'blockMath',\n\n group: 'block',\n\n atom: true,\n\n addOptions() {\n return {\n onClick: undefined,\n katexOptions: undefined,\n }\n },\n\n addAttributes() {\n return {\n latex: {\n default: '',\n parseHTML: element => element.getAttribute('data-latex'),\n renderHTML: attributes => {\n return {\n 'data-latex': attributes.latex,\n }\n },\n },\n }\n },\n\n addCommands() {\n return {\n insertBlockMath:\n options =>\n ({ commands, editor }) => {\n const { latex, pos } = options\n\n if (!latex) {\n return false\n }\n\n return commands.insertContentAt(pos ?? editor.state.selection.from, {\n type: this.name,\n attrs: { latex },\n })\n },\n\n deleteBlockMath:\n options =>\n ({ editor, tr }) => {\n const pos = options?.pos ?? editor.state.selection.$from.pos\n const node = editor.state.doc.nodeAt(pos)\n\n if (!node || node.type.name !== this.name) {\n return false\n }\n\n tr.delete(pos, pos + node.nodeSize)\n return true\n },\n\n updateBlockMath:\n options =>\n ({ editor, tr }) => {\n const latex = options?.latex\n let pos = options?.pos\n\n if (pos === undefined) {\n pos = editor.state.selection.$from.pos\n }\n\n const node = editor.state.doc.nodeAt(pos)\n\n if (!node || node.type.name !== this.name) {\n return false\n }\n\n tr.setNodeMarkup(pos, this.type, {\n ...node.attrs,\n latex: latex || node.attrs.latex,\n })\n\n return true\n },\n }\n },\n\n parseHTML() {\n return [\n {\n tag: 'div[data-type=\"block-math\"]',\n },\n ]\n },\n\n renderHTML({ HTMLAttributes }) {\n return ['div', mergeAttributes(HTMLAttributes, { 'data-type': 'block-math' })]\n },\n\n parseMarkdown: (token: any) => {\n return {\n type: 'blockMath',\n attrs: {\n latex: token.latex,\n },\n }\n },\n\n renderMarkdown: node => {\n const latex = node.attrs?.latex || ''\n\n const output = ['$$', latex, '$$']\n return output.join('\\n')\n },\n\n markdownTokenizer: {\n name: 'blockMath',\n level: 'block',\n start: (src: string) => src.indexOf('$$'),\n tokenize: (src: string) => {\n // Match $$latex$$ syntax for block math\n const match = src.match(/^\\$\\$([^$]+)\\$\\$/)\n if (!match) {\n return undefined\n }\n\n const [fullMatch, latex] = match\n\n return {\n type: 'blockMath',\n raw: fullMatch,\n latex: latex.trim(),\n }\n },\n },\n\n addInputRules() {\n return [\n new InputRule({\n find: /^\\$\\$\\$([^$]+)\\$\\$\\$$/,\n handler: ({ state, range, match }) => {\n const [, latex] = match\n const { tr } = state\n const start = range.from\n const end = range.to\n\n tr.replaceWith(start, end, this.type.create({ latex }))\n },\n }),\n ]\n },\n\n addNodeView() {\n const { katexOptions } = this.options\n\n return ({ node, getPos }) => {\n const wrapper = document.createElement('div')\n const innerWrapper = document.createElement('div')\n wrapper.className = 'tiptap-mathematics-render'\n\n if (this.editor.isEditable) {\n wrapper.classList.add('tiptap-mathematics-render--editable')\n }\n\n innerWrapper.className = 'block-math-inner'\n wrapper.dataset.type = 'block-math'\n wrapper.setAttribute('data-latex', node.attrs.latex)\n wrapper.appendChild(innerWrapper)\n\n function renderMath() {\n try {\n katex.render(node.attrs.latex, innerWrapper, katexOptions)\n wrapper.classList.remove('block-math-error')\n } catch {\n wrapper.textContent = node.attrs.latex\n wrapper.classList.add('block-math-error')\n }\n }\n\n const handleClick = (event: MouseEvent) => {\n event.preventDefault()\n event.stopPropagation()\n const pos = getPos()\n\n if (pos == null) {\n return\n }\n\n if (this.options.onClick) {\n this.options.onClick(node, pos)\n }\n }\n\n if (this.options.onClick) {\n wrapper.addEventListener('click', handleClick)\n }\n\n renderMath()\n\n return {\n dom: wrapper,\n destroy() {\n wrapper.removeEventListener('click', handleClick)\n },\n }\n }\n },\n})\n","import { InputRule, mergeAttributes, Node } from '@tiptap/core'\nimport type { Node as PMNode } from '@tiptap/pm/model'\nimport katex, { type KatexOptions } from 'katex'\n\n/**\n * Configuration options for the InlineMath extension.\n */\nexport type InlineMathOptions = {\n /**\n * KaTeX specific options\n * @see https://katex.org/docs/options.html\n * @example\n * ```ts\n * katexOptions: {\n * displayMode: false,\n * throwOnError: false,\n * macros: {\n * '\\\\RR': '\\\\mathbb{R}',\n * '\\\\ZZ': '\\\\mathbb{Z}'\n * }\n * }\n * ```\n */\n katexOptions?: KatexOptions\n\n /**\n * Optional click handler for inline math nodes.\n * Called when a user clicks on an inline math expression in the editor.\n *\n * @param node - The ProseMirror node representing the inline math element\n * @param pos - The position of the node within the document\n * @example\n * ```ts\n * onClick: (node, pos) => {\n * console.log('Inline math clicked:', node.attrs.latex, 'at position:', pos)\n * }\n * ```\n */\n onClick?: (node: PMNode, pos: number) => void\n}\n\ndeclare module '@tiptap/core' {\n interface Commands<ReturnType> {\n inlineMath: {\n /**\n * Insert a inline math node with LaTeX string.\n * @param options - Options for inserting inline math.\n * @returns ReturnType\n */\n insertInlineMath: (options: { latex: string; pos?: number }) => ReturnType\n\n /**\n * Delete an inline math node.\n * @returns ReturnType\n */\n deleteInlineMath: (options?: { pos?: number }) => ReturnType\n\n /**\n * Update inline math node with optional LaTeX string.\n * @param options - Options for updating inline math.\n * @returns ReturnType\n */\n updateInlineMath: (options?: { latex?: string; pos?: number }) => ReturnType\n }\n }\n}\n\n/**\n * InlineMath is a Tiptap extension for rendering inline mathematical expressions using KaTeX.\n * It allows users to insert LaTeX formatted math expressions inline within text.\n * It supports rendering, input rules for LaTeX syntax, and click handling for interaction.\n *\n * @example\n * ```javascript\n * import { InlineMath } from '@tiptap/extension-mathematics'\n * import { Editor } from '@tiptap/core'\n *\n * const editor = new Editor({\n * extensions: [\n * InlineMath.configure({\n * onClick: (node, pos) => {\n * console.log('Inline math clicked:', node.attrs.latex, 'at position:', pos)\n * },\n * }),\n * ],\n * })\n */\nexport const InlineMath = Node.create<InlineMathOptions>({\n name: 'inlineMath',\n\n group: 'inline',\n\n inline: true,\n\n atom: true,\n\n addOptions() {\n return {\n onClick: undefined,\n katexOptions: undefined,\n }\n },\n\n addAttributes() {\n return {\n latex: {\n default: '',\n parseHTML: element => element.getAttribute('data-latex'),\n renderHTML: attributes => {\n return {\n 'data-latex': attributes.latex,\n }\n },\n },\n }\n },\n\n addCommands() {\n return {\n insertInlineMath:\n options =>\n ({ editor, tr }) => {\n const latex = options.latex\n\n const from = options?.pos ?? editor.state.selection.from\n\n if (!latex) {\n return false\n }\n\n tr.replaceWith(from, from, this.type.create({ latex }))\n return true\n },\n\n deleteInlineMath:\n options =>\n ({ editor, tr }) => {\n const pos = options?.pos ?? editor.state.selection.$from.pos\n const node = editor.state.doc.nodeAt(pos)\n\n if (!node || node.type.name !== this.name) {\n return false\n }\n\n tr.delete(pos, pos + node.nodeSize)\n return true\n },\n\n updateInlineMath:\n options =>\n ({ editor, tr }) => {\n const latex = options?.latex\n let pos = options?.pos\n\n if (pos === undefined) {\n pos = editor.state.selection.$from.pos\n }\n\n const node = editor.state.doc.nodeAt(pos)\n\n if (!node || node.type.name !== this.name) {\n return false\n }\n\n tr.setNodeMarkup(pos, this.type, { ...node.attrs, latex })\n\n return true\n },\n }\n },\n\n parseHTML() {\n return [\n {\n tag: 'span[data-type=\"inline-math\"]',\n },\n ]\n },\n\n renderHTML({ HTMLAttributes }) {\n return ['span', mergeAttributes(HTMLAttributes, { 'data-type': 'inline-math' })]\n },\n\n parseMarkdown: (token: any) => {\n return {\n type: 'inlineMath',\n attrs: {\n latex: token.latex,\n },\n }\n },\n\n renderMarkdown: node => {\n const latex = node.attrs?.latex || ''\n\n return `$${latex}$`\n },\n\n markdownTokenizer: {\n name: 'inlineMath',\n level: 'inline',\n start: (src: string) => src.indexOf('$'),\n tokenize: (src: string) => {\n // Match $latex$ syntax for inline math (but not $$)\n const match = src.match(/^\\$([^$]+)\\$(?!\\$)/)\n if (!match) {\n return undefined\n }\n\n const [fullMatch, latex] = match\n\n return {\n type: 'inlineMath',\n raw: fullMatch,\n latex: latex.trim(),\n }\n },\n },\n\n addInputRules() {\n return [\n new InputRule({\n find: /(^|[^$])(\\$\\$([^$\\n]+?)\\$\\$)(?!\\$)/,\n handler: ({ state, range, match }) => {\n const latex = match[3]\n const { tr } = state\n const start = range.from\n const end = range.to\n\n tr.replaceWith(start, end, this.type.create({ latex }))\n },\n }),\n ]\n },\n\n addNodeView() {\n const { katexOptions } = this.options\n\n return ({ node, getPos }) => {\n const wrapper = document.createElement('span')\n wrapper.className = 'tiptap-mathematics-render'\n\n if (this.editor.isEditable) {\n wrapper.classList.add('tiptap-mathematics-render--editable')\n }\n\n wrapper.dataset.type = 'inline-math'\n wrapper.setAttribute('data-latex', node.attrs.latex)\n\n function renderMath() {\n try {\n katex.render(node.attrs.latex, wrapper, katexOptions)\n wrapper.classList.remove('inline-math-error')\n } catch {\n wrapper.textContent = node.attrs.latex\n wrapper.classList.add('inline-math-error')\n }\n }\n\n const handleClick = (event: MouseEvent) => {\n event.preventDefault()\n event.stopPropagation()\n const pos = getPos()\n\n if (pos == null) {\n return\n }\n\n if (this.options.onClick) {\n this.options.onClick(node, pos)\n }\n }\n\n if (this.options.onClick) {\n wrapper.addEventListener('click', handleClick)\n }\n\n renderMath()\n\n return {\n dom: wrapper,\n destroy() {\n wrapper.removeEventListener('click', handleClick)\n },\n }\n }\n },\n})\n","import type { Editor } from '@tiptap/core'\nimport type { Transaction } from '@tiptap/pm/state'\n\n/**\n * Regular expression to match LaTeX math strings wrapped in single dollar signs.\n * This should not catch dollar signs which are not part of a math expression,\n * like those used for currency or other purposes.\n * It ensures that the dollar signs are not preceded or followed by digits,\n * allowing for proper identification of inline math expressions.\n *\n * - `$x^2 + y^2 = z^2$` will match\n * - `This is $inline math$ in text.` will match\n * - `This is $100$ dollars.` will not match (as it is not a math expression)\n * - `This is $x^2 + y^2 = z^2$ and $100$ dollars.` will match both math expressions\n */\nexport const mathMigrationRegex = /\\$(?!\\d+\\$)(.+?)\\$(?!\\d)/g\n\n/**\n * Creates a transaction that migrates existing math strings in the document to new math nodes.\n * This function traverses the document and replaces LaTeX math syntax (wrapped in single dollar signs)\n * with proper inline math nodes, preserving the mathematical content.\n *\n * @param editor - The editor instance containing the schema and configuration\n * @param tr - The transaction to modify with the migration operations\n * @returns The modified transaction with math string replacements\n *\n * @example\n * ```typescript\n * const editor = new Editor({ ... })\n * const tr = editor.state.tr\n * const updatedTr = createMathMigrateTransaction(editor, tr)\n * editor.view.dispatch(updatedTr)\n * ```\n */\nexport function createMathMigrateTransaction(editor: Editor, tr: Transaction, regex: RegExp = mathMigrationRegex) {\n // we traverse the document and replace all math nodes with the new math nodes\n tr.doc.descendants((node, pos) => {\n if (!node.isText || !node.text || !node.text.includes('$')) {\n return\n }\n\n const { text } = node\n\n const match = node.text.match(regex)\n if (!match) {\n return\n }\n\n match.forEach(mathMatch => {\n const start = text.indexOf(mathMatch)\n const end = start + mathMatch.length\n\n const from = tr.mapping.map(pos + start)\n\n const $from = tr.doc.resolve(from)\n const parent = $from.parent\n const index = $from.index()\n\n const { inlineMath } = editor.schema.nodes\n\n if (!parent.canReplaceWith(index, index + 1, inlineMath)) {\n return\n }\n\n // Replace the math syntax with a new math node\n tr.replaceWith(\n tr.mapping.map(pos + start),\n tr.mapping.map(pos + end),\n inlineMath.create({ latex: mathMatch.slice(1, -1) }),\n )\n })\n })\n\n // don't add to history\n tr.setMeta('addToHistory', false)\n return tr\n}\n\n/**\n * Migrates existing math strings in the editor document to math nodes.\n * This function creates and dispatches a transaction that converts LaTeX math syntax\n * (text wrapped in single dollar signs) into proper inline math nodes. The migration\n * happens immediately and is not added to the editor's history.\n *\n * @param editor - The editor instance to perform the migration on\n *\n * @example\n * ```typescript\n * const editor = new Editor({\n * extensions: [Mathematics],\n * content: 'This is inline math: $x^2 + y^2 = z^2$ in text.'\n * })\n *\n * // Math strings will be automatically migrated to math nodes\n * migrateMathStrings(editor)\n * ```\n */\nexport function migrateMathStrings(editor: Editor, regex: RegExp = mathMigrationRegex) {\n const tr = createMathMigrateTransaction(editor, editor.state.tr, regex)\n editor.view.dispatch(tr)\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,IAAAA,eAA0B;;;ACA1B,kBAAiD;AAEjD,mBAAyC;AAgFlC,IAAM,YAAY,iBAAK,OAAyB;AAAA,EACrD,MAAM;AAAA,EAEN,OAAO;AAAA,EAEP,MAAM;AAAA,EAEN,aAAa;AACX,WAAO;AAAA,MACL,SAAS;AAAA,MACT,cAAc;AAAA,IAChB;AAAA,EACF;AAAA,EAEA,gBAAgB;AACd,WAAO;AAAA,MACL,OAAO;AAAA,QACL,SAAS;AAAA,QACT,WAAW,aAAW,QAAQ,aAAa,YAAY;AAAA,QACvD,YAAY,gBAAc;AACxB,iBAAO;AAAA,YACL,cAAc,WAAW;AAAA,UAC3B;AAAA,QACF;AAAA,MACF;AAAA,IACF;AAAA,EACF;AAAA,EAEA,cAAc;AACZ,WAAO;AAAA,MACL,iBACE,aACA,CAAC,EAAE,UAAU,OAAO,MAAM;AACxB,cAAM,EAAE,OAAO,IAAI,IAAI;AAEvB,YAAI,CAAC,OAAO;AACV,iBAAO;AAAA,QACT;AAEA,eAAO,SAAS,gBAAgB,oBAAO,OAAO,MAAM,UAAU,MAAM;AAAA,UAClE,MAAM,KAAK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,MAAM,QAAQ;AAChC,QAAI,CAAC,KAAK,UAAU,CAAC,KAAK,QAAQ,CAAC,KAAK,KAAK,SAAS,GAAG,GAAG;AAC1D;AAAA,IACF;AAEA,UAAM,EAAE,KAAK,IAAI;AAEjB,UAAM,QAAQ,KAAK,KAAK,MAAM,KAAK;AACnC,QAAI,CAAC,OAAO;AACV;AAAA,IACF;AAEA,UAAM,QAAQ,eAAa;AACzB,YAAM,QAAQ,KAAK,QAAQ,SAAS;AACpC,YAAM,MAAM,QAAQ,UAAU;AAE9B,YAAM,OAAO,GAAG,QAAQ,IAAI,MAAM,KAAK;AAEvC,YAAM,QAAQ,GAAG,IAAI,QAAQ,IAAI;AACjC,YAAM,SAAS,MAAM;AACrB,YAAM,QAAQ,MAAM,MAAM;AAE1B,YAAM,EAAE,WAAW,IAAI,OAAO,OAAO;AAErC,UAAI,CAAC,OAAO,eAAe,OAAO,QAAQ,GAAG,UAAU,GAAG;AACxD;AAAA,MACF;AAGA,SAAG;AAAA,QACD,GAAG,QAAQ,IAAI,MAAM,KAAK;AAAA,QAC1B,GAAG,QAAQ,IAAI,MAAM,GAAG;AAAA,QACxB,WAAW,OAAO,EAAE,OAAO,UAAU,MAAM,GAAG,EAAE,EAAE,CAAC;AAAA,MACrD;AAAA,IACF,CAAC;AAAA,EACH,CAAC;AAGD,KAAG,QAAQ,gBAAgB,KAAK;AAChC,SAAO;AACT;AAqBO,SAAS,mBAAmB,QAAgB,QAAgB,oBAAoB;AACrF,QAAM,KAAK,6BAA6B,QAAQ,OAAO,MAAM,IAAI,KAAK;AACtE,SAAO,KAAK,SAAS,EAAE;AACzB;;;AJ7FA,IAAO,gBAAQ;","names":["import_core","katex","import_core","import_katex","katex"]}
|
package/dist/index.js
CHANGED
|
@@ -77,6 +77,37 @@ var BlockMath = Node.create({
|
|
|
77
77
|
renderHTML({ HTMLAttributes }) {
|
|
78
78
|
return ["div", mergeAttributes(HTMLAttributes, { "data-type": "block-math" })];
|
|
79
79
|
},
|
|
80
|
+
parseMarkdown: (token) => {
|
|
81
|
+
return {
|
|
82
|
+
type: "blockMath",
|
|
83
|
+
attrs: {
|
|
84
|
+
latex: token.latex
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
},
|
|
88
|
+
renderMarkdown: (node) => {
|
|
89
|
+
var _a;
|
|
90
|
+
const latex = ((_a = node.attrs) == null ? void 0 : _a.latex) || "";
|
|
91
|
+
const output = ["$$", latex, "$$"];
|
|
92
|
+
return output.join("\n");
|
|
93
|
+
},
|
|
94
|
+
markdownTokenizer: {
|
|
95
|
+
name: "blockMath",
|
|
96
|
+
level: "block",
|
|
97
|
+
start: (src) => src.indexOf("$$"),
|
|
98
|
+
tokenize: (src) => {
|
|
99
|
+
const match = src.match(/^\$\$([^$]+)\$\$/);
|
|
100
|
+
if (!match) {
|
|
101
|
+
return void 0;
|
|
102
|
+
}
|
|
103
|
+
const [fullMatch, latex] = match;
|
|
104
|
+
return {
|
|
105
|
+
type: "blockMath",
|
|
106
|
+
raw: fullMatch,
|
|
107
|
+
latex: latex.trim()
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
},
|
|
80
111
|
addInputRules() {
|
|
81
112
|
return [
|
|
82
113
|
new InputRule({
|
|
@@ -212,6 +243,36 @@ var InlineMath = Node2.create({
|
|
|
212
243
|
renderHTML({ HTMLAttributes }) {
|
|
213
244
|
return ["span", mergeAttributes2(HTMLAttributes, { "data-type": "inline-math" })];
|
|
214
245
|
},
|
|
246
|
+
parseMarkdown: (token) => {
|
|
247
|
+
return {
|
|
248
|
+
type: "inlineMath",
|
|
249
|
+
attrs: {
|
|
250
|
+
latex: token.latex
|
|
251
|
+
}
|
|
252
|
+
};
|
|
253
|
+
},
|
|
254
|
+
renderMarkdown: (node) => {
|
|
255
|
+
var _a;
|
|
256
|
+
const latex = ((_a = node.attrs) == null ? void 0 : _a.latex) || "";
|
|
257
|
+
return `$${latex}$`;
|
|
258
|
+
},
|
|
259
|
+
markdownTokenizer: {
|
|
260
|
+
name: "inlineMath",
|
|
261
|
+
level: "inline",
|
|
262
|
+
start: (src) => src.indexOf("$"),
|
|
263
|
+
tokenize: (src) => {
|
|
264
|
+
const match = src.match(/^\$([^$]+)\$(?!\$)/);
|
|
265
|
+
if (!match) {
|
|
266
|
+
return void 0;
|
|
267
|
+
}
|
|
268
|
+
const [fullMatch, latex] = match;
|
|
269
|
+
return {
|
|
270
|
+
type: "inlineMath",
|
|
271
|
+
raw: fullMatch,
|
|
272
|
+
latex: latex.trim()
|
|
273
|
+
};
|
|
274
|
+
}
|
|
275
|
+
},
|
|
215
276
|
addInputRules() {
|
|
216
277
|
return [
|
|
217
278
|
new InputRule2({
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/mathematics.ts","../src/extensions/BlockMath.ts","../src/extensions/InlineMath.ts","../src/utils.ts","../src/index.ts"],"sourcesContent":["import { Extension } from '@tiptap/core'\n\nimport { BlockMath, InlineMath } from './extensions/index.js'\nimport type { MathematicsOptions } from './types.js'\n\n/**\n * Mathematics extension for Tiptap that provides both inline and block math support using KaTeX.\n * This extension combines the InlineMath and BlockMath extensions to provide a complete\n * mathematical expression solution for rich text editing. It supports LaTeX syntax,\n * custom rendering options, and interactive math nodes.\n *\n * @example\n * ```typescript\n * import { Editor } from '@tiptap/core'\n * import { Mathematics } from '@tiptap/extension-mathematics'\n * import { migrateMathStrings } from '@tiptap/extension-mathematics/utils'\n *\n * const editor = new Editor({\n * extensions: [\n * Mathematics.configure({\n * inlineOptions: {\n * onClick: (node, pos) => {\n * console.log('Inline math clicked:', node.attrs.latex)\n * }\n * },\n * blockOptions: {\n * onClick: (node, pos) => {\n * console.log('Block math clicked:', node.attrs.latex)\n * }\n * },\n * katexOptions: {\n * displayMode: false,\n * throwOnError: false,\n * macros: {\n * '\\\\RR': '\\\\mathbb{R}',\n * '\\\\ZZ': '\\\\mathbb{Z}'\n * }\n * }\n * })\n * ],\n * content: `\n * <p>Inline math: $E = mc^2$</p>\n * <div data-type=\"block-math\" data-latex=\"\\\\sum_{i=1}^{n} x_i = X\"></div>\n * `,\n * onCreate({ editor }) {\n * // Optional: Migrate existing math strings to math nodes\n * migrateMathStrings(editor)\n * }\n * })\n * ```\n */\nexport const Mathematics = Extension.create<MathematicsOptions>({\n name: 'Mathematics',\n\n addOptions() {\n return {\n inlineOptions: undefined,\n blockOptions: undefined,\n katexOptions: undefined,\n }\n },\n\n addExtensions() {\n return [\n BlockMath.configure({ ...this.options.blockOptions, katexOptions: this.options.katexOptions }),\n InlineMath.configure({ ...this.options.inlineOptions, katexOptions: this.options.katexOptions }),\n ]\n },\n})\n\nexport default Mathematics\n","import { InputRule, mergeAttributes, Node } from '@tiptap/core'\nimport type { Node as PMNode } from '@tiptap/pm/model'\nimport katex, { type KatexOptions } from 'katex'\n\n/**\n * Configuration options for the BlockMath extension.\n */\nexport type BlockMathOptions = {\n /**\n * KaTeX specific options\n * @see https://katex.org/docs/options.html\n * @example\n * ```ts\n * katexOptions: {\n * displayMode: true,\n * throwOnError: false,\n * },\n */\n katexOptions?: KatexOptions\n\n /**\n * Optional click handler for block math nodes.\n * Called when a user clicks on a block math expression in the editor.\n *\n * @param node - The ProseMirror node representing the block math element\n * @param pos - The position of the node within the document\n * @example\n * ```ts\n * onClick: (node, pos) => {\n * console.log('Block math clicked:', node.attrs.latex, 'at position:', pos)\n * },\n * ```\n */\n onClick?: (node: PMNode, pos: number) => void\n}\n\ndeclare module '@tiptap/core' {\n interface Commands<ReturnType> {\n insertBlockMath: {\n /**\n * Inserts a math block node with LaTeX string.\n * @param options - Options for inserting block math.\n * @returns ReturnType\n */\n insertBlockMath: (options: { latex: string; pos?: number }) => ReturnType\n\n /**\n * Deletes a block math node.\n * @returns ReturnType\n */\n deleteBlockMath: (options?: { pos?: number }) => ReturnType\n\n /**\n * Update block math node with optional LaTeX string.\n * @param options - Options for updating block math.\n * @returns ReturnType\n */\n updateBlockMath: (options?: { latex: string; pos?: number }) => ReturnType\n }\n }\n}\n\n/**\n * BlockMath is a Tiptap extension for rendering block mathematical expressions using KaTeX.\n * It allows users to insert LaTeX formatted math expressions block within text.\n * It supports rendering, input rules for LaTeX syntax, and click handling for interaction.\n *\n * @example\n * ```javascript\n * import { BlockMath } from '@tiptap/extension-mathematics'\n * import { Editor } from '@tiptap/core'\n *\n * const editor = new Editor({\n * extensions: [\n * BlockMath.configure({\n * onClick: (node, pos) => {\n * console.log('Block math clicked:', node.attrs.latex, 'at position:', pos)\n * },\n * }),\n * ],\n * })\n */\nexport const BlockMath = Node.create<BlockMathOptions>({\n name: 'blockMath',\n\n group: 'block',\n\n atom: true,\n\n addOptions() {\n return {\n onClick: undefined,\n katexOptions: undefined,\n }\n },\n\n addAttributes() {\n return {\n latex: {\n default: '',\n parseHTML: element => element.getAttribute('data-latex'),\n renderHTML: attributes => {\n return {\n 'data-latex': attributes.latex,\n }\n },\n },\n }\n },\n\n addCommands() {\n return {\n insertBlockMath:\n options =>\n ({ commands, editor }) => {\n const { latex, pos } = options\n\n if (!latex) {\n return false\n }\n\n return commands.insertContentAt(pos ?? editor.state.selection.from, {\n type: this.name,\n attrs: { latex },\n })\n },\n\n deleteBlockMath:\n options =>\n ({ editor, tr }) => {\n const pos = options?.pos ?? editor.state.selection.$from.pos\n const node = editor.state.doc.nodeAt(pos)\n\n if (!node || node.type.name !== this.name) {\n return false\n }\n\n tr.delete(pos, pos + node.nodeSize)\n return true\n },\n\n updateBlockMath:\n options =>\n ({ editor, tr }) => {\n const latex = options?.latex\n let pos = options?.pos\n\n if (pos === undefined) {\n pos = editor.state.selection.$from.pos\n }\n\n const node = editor.state.doc.nodeAt(pos)\n\n if (!node || node.type.name !== this.name) {\n return false\n }\n\n tr.setNodeMarkup(pos, this.type, {\n ...node.attrs,\n latex: latex || node.attrs.latex,\n })\n\n return true\n },\n }\n },\n\n parseHTML() {\n return [\n {\n tag: 'div[data-type=\"block-math\"]',\n },\n ]\n },\n\n renderHTML({ HTMLAttributes }) {\n return ['div', mergeAttributes(HTMLAttributes, { 'data-type': 'block-math' })]\n },\n\n addInputRules() {\n return [\n new InputRule({\n find: /^\\$\\$\\$([^$]+)\\$\\$\\$$/,\n handler: ({ state, range, match }) => {\n const [, latex] = match\n const { tr } = state\n const start = range.from\n const end = range.to\n\n tr.replaceWith(start, end, this.type.create({ latex }))\n },\n }),\n ]\n },\n\n addNodeView() {\n const { katexOptions } = this.options\n\n return ({ node, getPos }) => {\n const wrapper = document.createElement('div')\n const innerWrapper = document.createElement('div')\n wrapper.className = 'tiptap-mathematics-render'\n\n if (this.editor.isEditable) {\n wrapper.classList.add('tiptap-mathematics-render--editable')\n }\n\n innerWrapper.className = 'block-math-inner'\n wrapper.dataset.type = 'block-math'\n wrapper.setAttribute('data-latex', node.attrs.latex)\n wrapper.appendChild(innerWrapper)\n\n function renderMath() {\n try {\n katex.render(node.attrs.latex, innerWrapper, katexOptions)\n wrapper.classList.remove('block-math-error')\n } catch {\n wrapper.textContent = node.attrs.latex\n wrapper.classList.add('block-math-error')\n }\n }\n\n const handleClick = (event: MouseEvent) => {\n event.preventDefault()\n event.stopPropagation()\n const pos = getPos()\n\n if (pos == null) {\n return\n }\n\n if (this.options.onClick) {\n this.options.onClick(node, pos)\n }\n }\n\n if (this.options.onClick) {\n wrapper.addEventListener('click', handleClick)\n }\n\n renderMath()\n\n return {\n dom: wrapper,\n destroy() {\n wrapper.removeEventListener('click', handleClick)\n },\n }\n }\n },\n})\n","import { InputRule, mergeAttributes, Node } from '@tiptap/core'\nimport type { Node as PMNode } from '@tiptap/pm/model'\nimport katex, { type KatexOptions } from 'katex'\n\n/**\n * Configuration options for the InlineMath extension.\n */\nexport type InlineMathOptions = {\n /**\n * KaTeX specific options\n * @see https://katex.org/docs/options.html\n * @example\n * ```ts\n * katexOptions: {\n * displayMode: false,\n * throwOnError: false,\n * macros: {\n * '\\\\RR': '\\\\mathbb{R}',\n * '\\\\ZZ': '\\\\mathbb{Z}'\n * }\n * }\n * ```\n */\n katexOptions?: KatexOptions\n\n /**\n * Optional click handler for inline math nodes.\n * Called when a user clicks on an inline math expression in the editor.\n *\n * @param node - The ProseMirror node representing the inline math element\n * @param pos - The position of the node within the document\n * @example\n * ```ts\n * onClick: (node, pos) => {\n * console.log('Inline math clicked:', node.attrs.latex, 'at position:', pos)\n * }\n * ```\n */\n onClick?: (node: PMNode, pos: number) => void\n}\n\ndeclare module '@tiptap/core' {\n interface Commands<ReturnType> {\n inlineMath: {\n /**\n * Insert a inline math node with LaTeX string.\n * @param options - Options for inserting inline math.\n * @returns ReturnType\n */\n insertInlineMath: (options: { latex: string; pos?: number }) => ReturnType\n\n /**\n * Delete an inline math node.\n * @returns ReturnType\n */\n deleteInlineMath: (options?: { pos?: number }) => ReturnType\n\n /**\n * Update inline math node with optional LaTeX string.\n * @param options - Options for updating inline math.\n * @returns ReturnType\n */\n updateInlineMath: (options?: { latex?: string; pos?: number }) => ReturnType\n }\n }\n}\n\n/**\n * InlineMath is a Tiptap extension for rendering inline mathematical expressions using KaTeX.\n * It allows users to insert LaTeX formatted math expressions inline within text.\n * It supports rendering, input rules for LaTeX syntax, and click handling for interaction.\n *\n * @example\n * ```javascript\n * import { InlineMath } from '@tiptap/extension-mathematics'\n * import { Editor } from '@tiptap/core'\n *\n * const editor = new Editor({\n * extensions: [\n * InlineMath.configure({\n * onClick: (node, pos) => {\n * console.log('Inline math clicked:', node.attrs.latex, 'at position:', pos)\n * },\n * }),\n * ],\n * })\n */\nexport const InlineMath = Node.create<InlineMathOptions>({\n name: 'inlineMath',\n\n group: 'inline',\n\n inline: true,\n\n atom: true,\n\n addOptions() {\n return {\n onClick: undefined,\n katexOptions: undefined,\n }\n },\n\n addAttributes() {\n return {\n latex: {\n default: '',\n parseHTML: element => element.getAttribute('data-latex'),\n renderHTML: attributes => {\n return {\n 'data-latex': attributes.latex,\n }\n },\n },\n }\n },\n\n addCommands() {\n return {\n insertInlineMath:\n options =>\n ({ editor, tr }) => {\n const latex = options.latex\n\n const from = options?.pos ?? editor.state.selection.from\n\n if (!latex) {\n return false\n }\n\n tr.replaceWith(from, from, this.type.create({ latex }))\n return true\n },\n\n deleteInlineMath:\n options =>\n ({ editor, tr }) => {\n const pos = options?.pos ?? editor.state.selection.$from.pos\n const node = editor.state.doc.nodeAt(pos)\n\n if (!node || node.type.name !== this.name) {\n return false\n }\n\n tr.delete(pos, pos + node.nodeSize)\n return true\n },\n\n updateInlineMath:\n options =>\n ({ editor, tr }) => {\n const latex = options?.latex\n let pos = options?.pos\n\n if (pos === undefined) {\n pos = editor.state.selection.$from.pos\n }\n\n const node = editor.state.doc.nodeAt(pos)\n\n if (!node || node.type.name !== this.name) {\n return false\n }\n\n tr.setNodeMarkup(pos, this.type, { ...node.attrs, latex })\n\n return true\n },\n }\n },\n\n parseHTML() {\n return [\n {\n tag: 'span[data-type=\"inline-math\"]',\n },\n ]\n },\n\n renderHTML({ HTMLAttributes }) {\n return ['span', mergeAttributes(HTMLAttributes, { 'data-type': 'inline-math' })]\n },\n\n addInputRules() {\n return [\n new InputRule({\n find: /(^|[^$])(\\$\\$([^$\\n]+?)\\$\\$)(?!\\$)/,\n handler: ({ state, range, match }) => {\n const latex = match[3]\n const { tr } = state\n const start = range.from\n const end = range.to\n\n tr.replaceWith(start, end, this.type.create({ latex }))\n },\n }),\n ]\n },\n\n addNodeView() {\n const { katexOptions } = this.options\n\n return ({ node, getPos }) => {\n const wrapper = document.createElement('span')\n wrapper.className = 'tiptap-mathematics-render'\n\n if (this.editor.isEditable) {\n wrapper.classList.add('tiptap-mathematics-render--editable')\n }\n\n wrapper.dataset.type = 'inline-math'\n wrapper.setAttribute('data-latex', node.attrs.latex)\n\n function renderMath() {\n try {\n katex.render(node.attrs.latex, wrapper, katexOptions)\n wrapper.classList.remove('inline-math-error')\n } catch {\n wrapper.textContent = node.attrs.latex\n wrapper.classList.add('inline-math-error')\n }\n }\n\n const handleClick = (event: MouseEvent) => {\n event.preventDefault()\n event.stopPropagation()\n const pos = getPos()\n\n if (pos == null) {\n return\n }\n\n if (this.options.onClick) {\n this.options.onClick(node, pos)\n }\n }\n\n if (this.options.onClick) {\n wrapper.addEventListener('click', handleClick)\n }\n\n renderMath()\n\n return {\n dom: wrapper,\n destroy() {\n wrapper.removeEventListener('click', handleClick)\n },\n }\n }\n },\n})\n","import type { Editor } from '@tiptap/core'\nimport type { Transaction } from '@tiptap/pm/state'\n\n/**\n * Regular expression to match LaTeX math strings wrapped in single dollar signs.\n * This should not catch dollar signs which are not part of a math expression,\n * like those used for currency or other purposes.\n * It ensures that the dollar signs are not preceded or followed by digits,\n * allowing for proper identification of inline math expressions.\n *\n * - `$x^2 + y^2 = z^2$` will match\n * - `This is $inline math$ in text.` will match\n * - `This is $100$ dollars.` will not match (as it is not a math expression)\n * - `This is $x^2 + y^2 = z^2$ and $100$ dollars.` will match both math expressions\n */\nexport const mathMigrationRegex = /\\$(?!\\d+\\$)(.+?)\\$(?!\\d)/g\n\n/**\n * Creates a transaction that migrates existing math strings in the document to new math nodes.\n * This function traverses the document and replaces LaTeX math syntax (wrapped in single dollar signs)\n * with proper inline math nodes, preserving the mathematical content.\n *\n * @param editor - The editor instance containing the schema and configuration\n * @param tr - The transaction to modify with the migration operations\n * @returns The modified transaction with math string replacements\n *\n * @example\n * ```typescript\n * const editor = new Editor({ ... })\n * const tr = editor.state.tr\n * const updatedTr = createMathMigrateTransaction(editor, tr)\n * editor.view.dispatch(updatedTr)\n * ```\n */\nexport function createMathMigrateTransaction(editor: Editor, tr: Transaction, regex: RegExp = mathMigrationRegex) {\n // we traverse the document and replace all math nodes with the new math nodes\n tr.doc.descendants((node, pos) => {\n if (!node.isText || !node.text || !node.text.includes('$')) {\n return\n }\n\n const { text } = node\n\n const match = node.text.match(regex)\n if (!match) {\n return\n }\n\n match.forEach(mathMatch => {\n const start = text.indexOf(mathMatch)\n const end = start + mathMatch.length\n\n const from = tr.mapping.map(pos + start)\n\n const $from = tr.doc.resolve(from)\n const parent = $from.parent\n const index = $from.index()\n\n const { inlineMath } = editor.schema.nodes\n\n if (!parent.canReplaceWith(index, index + 1, inlineMath)) {\n return\n }\n\n // Replace the math syntax with a new math node\n tr.replaceWith(\n tr.mapping.map(pos + start),\n tr.mapping.map(pos + end),\n inlineMath.create({ latex: mathMatch.slice(1, -1) }),\n )\n })\n })\n\n // don't add to history\n tr.setMeta('addToHistory', false)\n return tr\n}\n\n/**\n * Migrates existing math strings in the editor document to math nodes.\n * This function creates and dispatches a transaction that converts LaTeX math syntax\n * (text wrapped in single dollar signs) into proper inline math nodes. The migration\n * happens immediately and is not added to the editor's history.\n *\n * @param editor - The editor instance to perform the migration on\n *\n * @example\n * ```typescript\n * const editor = new Editor({\n * extensions: [Mathematics],\n * content: 'This is inline math: $x^2 + y^2 = z^2$ in text.'\n * })\n *\n * // Math strings will be automatically migrated to math nodes\n * migrateMathStrings(editor)\n * ```\n */\nexport function migrateMathStrings(editor: Editor, regex: RegExp = mathMigrationRegex) {\n const tr = createMathMigrateTransaction(editor, editor.state.tr, regex)\n editor.view.dispatch(tr)\n}\n","import { Mathematics } from './mathematics.js'\n\nexport * from './extensions/index.js'\nexport * from './mathematics.js'\nexport * from './types.js'\nexport * from './utils.js'\n\nexport default Mathematics\n"],"mappings":";AAAA,SAAS,iBAAiB;;;ACA1B,SAAS,WAAW,iBAAiB,YAAY;AAEjD,OAAO,WAAkC;AAgFlC,IAAM,YAAY,KAAK,OAAyB;AAAA,EACrD,MAAM;AAAA,EAEN,OAAO;AAAA,EAEP,MAAM;AAAA,EAEN,aAAa;AACX,WAAO;AAAA,MACL,SAAS;AAAA,MACT,cAAc;AAAA,IAChB;AAAA,EACF;AAAA,EAEA,gBAAgB;AACd,WAAO;AAAA,MACL,OAAO;AAAA,QACL,SAAS;AAAA,QACT,WAAW,aAAW,QAAQ,aAAa,YAAY;AAAA,QACvD,YAAY,gBAAc;AACxB,iBAAO;AAAA,YACL,cAAc,WAAW;AAAA,UAC3B;AAAA,QACF;AAAA,MACF;AAAA,IACF;AAAA,E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AO,gBAAQ;","names":["InputRule","mergeAttributes","Node","katex"]}
|
|
1
|
+
{"version":3,"sources":["../src/mathematics.ts","../src/extensions/BlockMath.ts","../src/extensions/InlineMath.ts","../src/utils.ts","../src/index.ts"],"sourcesContent":["import { Extension } from '@tiptap/core'\n\nimport { BlockMath, InlineMath } from './extensions/index.js'\nimport type { MathematicsOptions } from './types.js'\n\n/**\n * Mathematics extension for Tiptap that provides both inline and block math support using KaTeX.\n * This extension combines the InlineMath and BlockMath extensions to provide a complete\n * mathematical expression solution for rich text editing. It supports LaTeX syntax,\n * custom rendering options, and interactive math nodes.\n *\n * @example\n * ```typescript\n * import { Editor } from '@tiptap/core'\n * import { Mathematics } from '@tiptap/extension-mathematics'\n * import { migrateMathStrings } from '@tiptap/extension-mathematics/utils'\n *\n * const editor = new Editor({\n * extensions: [\n * Mathematics.configure({\n * inlineOptions: {\n * onClick: (node, pos) => {\n * console.log('Inline math clicked:', node.attrs.latex)\n * }\n * },\n * blockOptions: {\n * onClick: (node, pos) => {\n * console.log('Block math clicked:', node.attrs.latex)\n * }\n * },\n * katexOptions: {\n * displayMode: false,\n * throwOnError: false,\n * macros: {\n * '\\\\RR': '\\\\mathbb{R}',\n * '\\\\ZZ': '\\\\mathbb{Z}'\n * }\n * }\n * })\n * ],\n * content: `\n * <p>Inline math: $E = mc^2$</p>\n * <div data-type=\"block-math\" data-latex=\"\\\\sum_{i=1}^{n} x_i = X\"></div>\n * `,\n * onCreate({ editor }) {\n * // Optional: Migrate existing math strings to math nodes\n * migrateMathStrings(editor)\n * }\n * })\n * ```\n */\nexport const Mathematics = Extension.create<MathematicsOptions>({\n name: 'Mathematics',\n\n addOptions() {\n return {\n inlineOptions: undefined,\n blockOptions: undefined,\n katexOptions: undefined,\n }\n },\n\n addExtensions() {\n return [\n BlockMath.configure({ ...this.options.blockOptions, katexOptions: this.options.katexOptions }),\n InlineMath.configure({ ...this.options.inlineOptions, katexOptions: this.options.katexOptions }),\n ]\n },\n})\n\nexport default Mathematics\n","import { InputRule, mergeAttributes, Node } from '@tiptap/core'\nimport type { Node as PMNode } from '@tiptap/pm/model'\nimport katex, { type KatexOptions } from 'katex'\n\n/**\n * Configuration options for the BlockMath extension.\n */\nexport type BlockMathOptions = {\n /**\n * KaTeX specific options\n * @see https://katex.org/docs/options.html\n * @example\n * ```ts\n * katexOptions: {\n * displayMode: true,\n * throwOnError: false,\n * },\n */\n katexOptions?: KatexOptions\n\n /**\n * Optional click handler for block math nodes.\n * Called when a user clicks on a block math expression in the editor.\n *\n * @param node - The ProseMirror node representing the block math element\n * @param pos - The position of the node within the document\n * @example\n * ```ts\n * onClick: (node, pos) => {\n * console.log('Block math clicked:', node.attrs.latex, 'at position:', pos)\n * },\n * ```\n */\n onClick?: (node: PMNode, pos: number) => void\n}\n\ndeclare module '@tiptap/core' {\n interface Commands<ReturnType> {\n insertBlockMath: {\n /**\n * Inserts a math block node with LaTeX string.\n * @param options - Options for inserting block math.\n * @returns ReturnType\n */\n insertBlockMath: (options: { latex: string; pos?: number }) => ReturnType\n\n /**\n * Deletes a block math node.\n * @returns ReturnType\n */\n deleteBlockMath: (options?: { pos?: number }) => ReturnType\n\n /**\n * Update block math node with optional LaTeX string.\n * @param options - Options for updating block math.\n * @returns ReturnType\n */\n updateBlockMath: (options?: { latex: string; pos?: number }) => ReturnType\n }\n }\n}\n\n/**\n * BlockMath is a Tiptap extension for rendering block mathematical expressions using KaTeX.\n * It allows users to insert LaTeX formatted math expressions block within text.\n * It supports rendering, input rules for LaTeX syntax, and click handling for interaction.\n *\n * @example\n * ```javascript\n * import { BlockMath } from '@tiptap/extension-mathematics'\n * import { Editor } from '@tiptap/core'\n *\n * const editor = new Editor({\n * extensions: [\n * BlockMath.configure({\n * onClick: (node, pos) => {\n * console.log('Block math clicked:', node.attrs.latex, 'at position:', pos)\n * },\n * }),\n * ],\n * })\n */\nexport const BlockMath = Node.create<BlockMathOptions>({\n name: 'blockMath',\n\n group: 'block',\n\n atom: true,\n\n addOptions() {\n return {\n onClick: undefined,\n katexOptions: undefined,\n }\n },\n\n addAttributes() {\n return {\n latex: {\n default: '',\n parseHTML: element => element.getAttribute('data-latex'),\n renderHTML: attributes => {\n return {\n 'data-latex': attributes.latex,\n }\n },\n },\n }\n },\n\n addCommands() {\n return {\n insertBlockMath:\n options =>\n ({ commands, editor }) => {\n const { latex, pos } = options\n\n if (!latex) {\n return false\n }\n\n return commands.insertContentAt(pos ?? editor.state.selection.from, {\n type: this.name,\n attrs: { latex },\n })\n },\n\n deleteBlockMath:\n options =>\n ({ editor, tr }) => {\n const pos = options?.pos ?? editor.state.selection.$from.pos\n const node = editor.state.doc.nodeAt(pos)\n\n if (!node || node.type.name !== this.name) {\n return false\n }\n\n tr.delete(pos, pos + node.nodeSize)\n return true\n },\n\n updateBlockMath:\n options =>\n ({ editor, tr }) => {\n const latex = options?.latex\n let pos = options?.pos\n\n if (pos === undefined) {\n pos = editor.state.selection.$from.pos\n }\n\n const node = editor.state.doc.nodeAt(pos)\n\n if (!node || node.type.name !== this.name) {\n return false\n }\n\n tr.setNodeMarkup(pos, this.type, {\n ...node.attrs,\n latex: latex || node.attrs.latex,\n })\n\n return true\n },\n }\n },\n\n parseHTML() {\n return [\n {\n tag: 'div[data-type=\"block-math\"]',\n },\n ]\n },\n\n renderHTML({ HTMLAttributes }) {\n return ['div', mergeAttributes(HTMLAttributes, { 'data-type': 'block-math' })]\n },\n\n parseMarkdown: (token: any) => {\n return {\n type: 'blockMath',\n attrs: {\n latex: token.latex,\n },\n }\n },\n\n renderMarkdown: node => {\n const latex = node.attrs?.latex || ''\n\n const output = ['$$', latex, '$$']\n return output.join('\\n')\n },\n\n markdownTokenizer: {\n name: 'blockMath',\n level: 'block',\n start: (src: string) => src.indexOf('$$'),\n tokenize: (src: string) => {\n // Match $$latex$$ syntax for block math\n const match = src.match(/^\\$\\$([^$]+)\\$\\$/)\n if (!match) {\n return undefined\n }\n\n const [fullMatch, latex] = match\n\n return {\n type: 'blockMath',\n raw: fullMatch,\n latex: latex.trim(),\n }\n },\n },\n\n addInputRules() {\n return [\n new InputRule({\n find: /^\\$\\$\\$([^$]+)\\$\\$\\$$/,\n handler: ({ state, range, match }) => {\n const [, latex] = match\n const { tr } = state\n const start = range.from\n const end = range.to\n\n tr.replaceWith(start, end, this.type.create({ latex }))\n },\n }),\n ]\n },\n\n addNodeView() {\n const { katexOptions } = this.options\n\n return ({ node, getPos }) => {\n const wrapper = document.createElement('div')\n const innerWrapper = document.createElement('div')\n wrapper.className = 'tiptap-mathematics-render'\n\n if (this.editor.isEditable) {\n wrapper.classList.add('tiptap-mathematics-render--editable')\n }\n\n innerWrapper.className = 'block-math-inner'\n wrapper.dataset.type = 'block-math'\n wrapper.setAttribute('data-latex', node.attrs.latex)\n wrapper.appendChild(innerWrapper)\n\n function renderMath() {\n try {\n katex.render(node.attrs.latex, innerWrapper, katexOptions)\n wrapper.classList.remove('block-math-error')\n } catch {\n wrapper.textContent = node.attrs.latex\n wrapper.classList.add('block-math-error')\n }\n }\n\n const handleClick = (event: MouseEvent) => {\n event.preventDefault()\n event.stopPropagation()\n const pos = getPos()\n\n if (pos == null) {\n return\n }\n\n if (this.options.onClick) {\n this.options.onClick(node, pos)\n }\n }\n\n if (this.options.onClick) {\n wrapper.addEventListener('click', handleClick)\n }\n\n renderMath()\n\n return {\n dom: wrapper,\n destroy() {\n wrapper.removeEventListener('click', handleClick)\n },\n }\n }\n },\n})\n","import { InputRule, mergeAttributes, Node } from '@tiptap/core'\nimport type { Node as PMNode } from '@tiptap/pm/model'\nimport katex, { type KatexOptions } from 'katex'\n\n/**\n * Configuration options for the InlineMath extension.\n */\nexport type InlineMathOptions = {\n /**\n * KaTeX specific options\n * @see https://katex.org/docs/options.html\n * @example\n * ```ts\n * katexOptions: {\n * displayMode: false,\n * throwOnError: false,\n * macros: {\n * '\\\\RR': '\\\\mathbb{R}',\n * '\\\\ZZ': '\\\\mathbb{Z}'\n * }\n * }\n * ```\n */\n katexOptions?: KatexOptions\n\n /**\n * Optional click handler for inline math nodes.\n * Called when a user clicks on an inline math expression in the editor.\n *\n * @param node - The ProseMirror node representing the inline math element\n * @param pos - The position of the node within the document\n * @example\n * ```ts\n * onClick: (node, pos) => {\n * console.log('Inline math clicked:', node.attrs.latex, 'at position:', pos)\n * }\n * ```\n */\n onClick?: (node: PMNode, pos: number) => void\n}\n\ndeclare module '@tiptap/core' {\n interface Commands<ReturnType> {\n inlineMath: {\n /**\n * Insert a inline math node with LaTeX string.\n * @param options - Options for inserting inline math.\n * @returns ReturnType\n */\n insertInlineMath: (options: { latex: string; pos?: number }) => ReturnType\n\n /**\n * Delete an inline math node.\n * @returns ReturnType\n */\n deleteInlineMath: (options?: { pos?: number }) => ReturnType\n\n /**\n * Update inline math node with optional LaTeX string.\n * @param options - Options for updating inline math.\n * @returns ReturnType\n */\n updateInlineMath: (options?: { latex?: string; pos?: number }) => ReturnType\n }\n }\n}\n\n/**\n * InlineMath is a Tiptap extension for rendering inline mathematical expressions using KaTeX.\n * It allows users to insert LaTeX formatted math expressions inline within text.\n * It supports rendering, input rules for LaTeX syntax, and click handling for interaction.\n *\n * @example\n * ```javascript\n * import { InlineMath } from '@tiptap/extension-mathematics'\n * import { Editor } from '@tiptap/core'\n *\n * const editor = new Editor({\n * extensions: [\n * InlineMath.configure({\n * onClick: (node, pos) => {\n * console.log('Inline math clicked:', node.attrs.latex, 'at position:', pos)\n * },\n * }),\n * ],\n * })\n */\nexport const InlineMath = Node.create<InlineMathOptions>({\n name: 'inlineMath',\n\n group: 'inline',\n\n inline: true,\n\n atom: true,\n\n addOptions() {\n return {\n onClick: undefined,\n katexOptions: undefined,\n }\n },\n\n addAttributes() {\n return {\n latex: {\n default: '',\n parseHTML: element => element.getAttribute('data-latex'),\n renderHTML: attributes => {\n return {\n 'data-latex': attributes.latex,\n }\n },\n },\n }\n },\n\n addCommands() {\n return {\n insertInlineMath:\n options =>\n ({ editor, tr }) => {\n const latex = options.latex\n\n const from = options?.pos ?? editor.state.selection.from\n\n if (!latex) {\n return false\n }\n\n tr.replaceWith(from, from, this.type.create({ latex }))\n return true\n },\n\n deleteInlineMath:\n options =>\n ({ editor, tr }) => {\n const pos = options?.pos ?? editor.state.selection.$from.pos\n const node = editor.state.doc.nodeAt(pos)\n\n if (!node || node.type.name !== this.name) {\n return false\n }\n\n tr.delete(pos, pos + node.nodeSize)\n return true\n },\n\n updateInlineMath:\n options =>\n ({ editor, tr }) => {\n const latex = options?.latex\n let pos = options?.pos\n\n if (pos === undefined) {\n pos = editor.state.selection.$from.pos\n }\n\n const node = editor.state.doc.nodeAt(pos)\n\n if (!node || node.type.name !== this.name) {\n return false\n }\n\n tr.setNodeMarkup(pos, this.type, { ...node.attrs, latex })\n\n return true\n },\n }\n },\n\n parseHTML() {\n return [\n {\n tag: 'span[data-type=\"inline-math\"]',\n },\n ]\n },\n\n renderHTML({ HTMLAttributes }) {\n return ['span', mergeAttributes(HTMLAttributes, { 'data-type': 'inline-math' })]\n },\n\n parseMarkdown: (token: any) => {\n return {\n type: 'inlineMath',\n attrs: {\n latex: token.latex,\n },\n }\n },\n\n renderMarkdown: node => {\n const latex = node.attrs?.latex || ''\n\n return `$${latex}$`\n },\n\n markdownTokenizer: {\n name: 'inlineMath',\n level: 'inline',\n start: (src: string) => src.indexOf('$'),\n tokenize: (src: string) => {\n // Match $latex$ syntax for inline math (but not $$)\n const match = src.match(/^\\$([^$]+)\\$(?!\\$)/)\n if (!match) {\n return undefined\n }\n\n const [fullMatch, latex] = match\n\n return {\n type: 'inlineMath',\n raw: fullMatch,\n latex: latex.trim(),\n }\n },\n },\n\n addInputRules() {\n return [\n new InputRule({\n find: /(^|[^$])(\\$\\$([^$\\n]+?)\\$\\$)(?!\\$)/,\n handler: ({ state, range, match }) => {\n const latex = match[3]\n const { tr } = state\n const start = range.from\n const end = range.to\n\n tr.replaceWith(start, end, this.type.create({ latex }))\n },\n }),\n ]\n },\n\n addNodeView() {\n const { katexOptions } = this.options\n\n return ({ node, getPos }) => {\n const wrapper = document.createElement('span')\n wrapper.className = 'tiptap-mathematics-render'\n\n if (this.editor.isEditable) {\n wrapper.classList.add('tiptap-mathematics-render--editable')\n }\n\n wrapper.dataset.type = 'inline-math'\n wrapper.setAttribute('data-latex', node.attrs.latex)\n\n function renderMath() {\n try {\n katex.render(node.attrs.latex, wrapper, katexOptions)\n wrapper.classList.remove('inline-math-error')\n } catch {\n wrapper.textContent = node.attrs.latex\n wrapper.classList.add('inline-math-error')\n }\n }\n\n const handleClick = (event: MouseEvent) => {\n event.preventDefault()\n event.stopPropagation()\n const pos = getPos()\n\n if (pos == null) {\n return\n }\n\n if (this.options.onClick) {\n this.options.onClick(node, pos)\n }\n }\n\n if (this.options.onClick) {\n wrapper.addEventListener('click', handleClick)\n }\n\n renderMath()\n\n return {\n dom: wrapper,\n destroy() {\n wrapper.removeEventListener('click', handleClick)\n },\n }\n }\n },\n})\n","import type { Editor } from '@tiptap/core'\nimport type { Transaction } from '@tiptap/pm/state'\n\n/**\n * Regular expression to match LaTeX math strings wrapped in single dollar signs.\n * This should not catch dollar signs which are not part of a math expression,\n * like those used for currency or other purposes.\n * It ensures that the dollar signs are not preceded or followed by digits,\n * allowing for proper identification of inline math expressions.\n *\n * - `$x^2 + y^2 = z^2$` will match\n * - `This is $inline math$ in text.` will match\n * - `This is $100$ dollars.` will not match (as it is not a math expression)\n * - `This is $x^2 + y^2 = z^2$ and $100$ dollars.` will match both math expressions\n */\nexport const mathMigrationRegex = /\\$(?!\\d+\\$)(.+?)\\$(?!\\d)/g\n\n/**\n * Creates a transaction that migrates existing math strings in the document to new math nodes.\n * This function traverses the document and replaces LaTeX math syntax (wrapped in single dollar signs)\n * with proper inline math nodes, preserving the mathematical content.\n *\n * @param editor - The editor instance containing the schema and configuration\n * @param tr - The transaction to modify with the migration operations\n * @returns The modified transaction with math string replacements\n *\n * @example\n * ```typescript\n * const editor = new Editor({ ... })\n * const tr = editor.state.tr\n * const updatedTr = createMathMigrateTransaction(editor, tr)\n * editor.view.dispatch(updatedTr)\n * ```\n */\nexport function createMathMigrateTransaction(editor: Editor, tr: Transaction, regex: RegExp = mathMigrationRegex) {\n // we traverse the document and replace all math nodes with the new math nodes\n tr.doc.descendants((node, pos) => {\n if (!node.isText || !node.text || !node.text.includes('$')) {\n return\n }\n\n const { text } = node\n\n const match = node.text.match(regex)\n if (!match) {\n return\n }\n\n match.forEach(mathMatch => {\n const start = text.indexOf(mathMatch)\n const end = start + mathMatch.length\n\n const from = tr.mapping.map(pos + start)\n\n const $from = tr.doc.resolve(from)\n const parent = $from.parent\n const index = $from.index()\n\n const { inlineMath } = editor.schema.nodes\n\n if (!parent.canReplaceWith(index, index + 1, inlineMath)) {\n return\n }\n\n // Replace the math syntax with a new math node\n tr.replaceWith(\n tr.mapping.map(pos + start),\n tr.mapping.map(pos + end),\n inlineMath.create({ latex: mathMatch.slice(1, -1) }),\n )\n })\n })\n\n // don't add to history\n tr.setMeta('addToHistory', false)\n return tr\n}\n\n/**\n * Migrates existing math strings in the editor document to math nodes.\n * This function creates and dispatches a transaction that converts LaTeX math syntax\n * (text wrapped in single dollar signs) into proper inline math nodes. The migration\n * happens immediately and is not added to the editor's history.\n *\n * @param editor - The editor instance to perform the migration on\n *\n * @example\n * ```typescript\n * const editor = new Editor({\n * extensions: [Mathematics],\n * content: 'This is inline math: $x^2 + y^2 = z^2$ in text.'\n * })\n *\n * // Math strings will be automatically migrated to math nodes\n * migrateMathStrings(editor)\n * ```\n */\nexport function migrateMathStrings(editor: Editor, regex: RegExp = mathMigrationRegex) {\n const tr = createMathMigrateTransaction(editor, editor.state.tr, regex)\n editor.view.dispatch(tr)\n}\n","import { Mathematics } from './mathematics.js'\n\nexport * from './extensions/index.js'\nexport * from './mathematics.js'\nexport * from './types.js'\nexport * from './utils.js'\n\nexport default Mathematics\n"],"mappings":";AAAA,SAAS,iBAAiB;;;ACA1B,SAAS,WAAW,iBAAiB,YAAY;AAEjD,OAAO,WAAkC;AAgFlC,IAAM,YAAY,KAAK,OAAyB;A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AAGA,SAAG;AAAA,QACD,GAAG,QAAQ,IAAI,MAAM,KAAK;AAAA,QAC1B,GAAG,QAAQ,IAAI,MAAM,GAAG;AAAA,QACxB,WAAW,OAAO,EAAE,OAAO,UAAU,MAAM,GAAG,EAAE,EAAE,CAAC;AAAA,MACrD;AAAA,IACF,CAAC;AAAA,EACH,CAAC;AAGD,KAAG,QAAQ,gBAAgB,KAAK;AAChC,SAAO;AACT;AAqBO,SAAS,mBAAmB,QAAgB,QAAgB,oBAAoB;AACrF,QAAM,KAAK,6BAA6B,QAAQ,OAAO,MAAM,IAAI,KAAK;AACtE,SAAO,KAAK,SAAS,EAAE;AACzB;;;AC7FA,IAAO,gBAAQ;","names":["InputRule","mergeAttributes","Node","katex"]}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tiptap/extension-mathematics",
|
|
3
3
|
"description": "latex math extension for tiptap",
|
|
4
|
-
"version": "3.
|
|
4
|
+
"version": "3.7.0",
|
|
5
5
|
"homepage": "https://tiptap.dev/api/extensions/mathematics",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"tiptap",
|
|
@@ -37,13 +37,13 @@
|
|
|
37
37
|
],
|
|
38
38
|
"devDependencies": {
|
|
39
39
|
"@types/katex": "^0.16.7",
|
|
40
|
-
"@tiptap/core": "^3.
|
|
41
|
-
"@tiptap/pm": "^3.
|
|
40
|
+
"@tiptap/core": "^3.7.0",
|
|
41
|
+
"@tiptap/pm": "^3.7.0"
|
|
42
42
|
},
|
|
43
43
|
"peerDependencies": {
|
|
44
44
|
"katex": "^0.16.4",
|
|
45
|
-
"@tiptap/core": "^3.
|
|
46
|
-
"@tiptap/pm": "^3.
|
|
45
|
+
"@tiptap/core": "^3.7.0",
|
|
46
|
+
"@tiptap/pm": "^3.7.0"
|
|
47
47
|
},
|
|
48
48
|
"scripts": {
|
|
49
49
|
"build": "tsup",
|
|
@@ -177,6 +177,43 @@ export const BlockMath = Node.create<BlockMathOptions>({
|
|
|
177
177
|
return ['div', mergeAttributes(HTMLAttributes, { 'data-type': 'block-math' })]
|
|
178
178
|
},
|
|
179
179
|
|
|
180
|
+
parseMarkdown: (token: any) => {
|
|
181
|
+
return {
|
|
182
|
+
type: 'blockMath',
|
|
183
|
+
attrs: {
|
|
184
|
+
latex: token.latex,
|
|
185
|
+
},
|
|
186
|
+
}
|
|
187
|
+
},
|
|
188
|
+
|
|
189
|
+
renderMarkdown: node => {
|
|
190
|
+
const latex = node.attrs?.latex || ''
|
|
191
|
+
|
|
192
|
+
const output = ['$$', latex, '$$']
|
|
193
|
+
return output.join('\n')
|
|
194
|
+
},
|
|
195
|
+
|
|
196
|
+
markdownTokenizer: {
|
|
197
|
+
name: 'blockMath',
|
|
198
|
+
level: 'block',
|
|
199
|
+
start: (src: string) => src.indexOf('$$'),
|
|
200
|
+
tokenize: (src: string) => {
|
|
201
|
+
// Match $$latex$$ syntax for block math
|
|
202
|
+
const match = src.match(/^\$\$([^$]+)\$\$/)
|
|
203
|
+
if (!match) {
|
|
204
|
+
return undefined
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
const [fullMatch, latex] = match
|
|
208
|
+
|
|
209
|
+
return {
|
|
210
|
+
type: 'blockMath',
|
|
211
|
+
raw: fullMatch,
|
|
212
|
+
latex: latex.trim(),
|
|
213
|
+
}
|
|
214
|
+
},
|
|
215
|
+
},
|
|
216
|
+
|
|
180
217
|
addInputRules() {
|
|
181
218
|
return [
|
|
182
219
|
new InputRule({
|
|
@@ -181,6 +181,42 @@ export const InlineMath = Node.create<InlineMathOptions>({
|
|
|
181
181
|
return ['span', mergeAttributes(HTMLAttributes, { 'data-type': 'inline-math' })]
|
|
182
182
|
},
|
|
183
183
|
|
|
184
|
+
parseMarkdown: (token: any) => {
|
|
185
|
+
return {
|
|
186
|
+
type: 'inlineMath',
|
|
187
|
+
attrs: {
|
|
188
|
+
latex: token.latex,
|
|
189
|
+
},
|
|
190
|
+
}
|
|
191
|
+
},
|
|
192
|
+
|
|
193
|
+
renderMarkdown: node => {
|
|
194
|
+
const latex = node.attrs?.latex || ''
|
|
195
|
+
|
|
196
|
+
return `$${latex}$`
|
|
197
|
+
},
|
|
198
|
+
|
|
199
|
+
markdownTokenizer: {
|
|
200
|
+
name: 'inlineMath',
|
|
201
|
+
level: 'inline',
|
|
202
|
+
start: (src: string) => src.indexOf('$'),
|
|
203
|
+
tokenize: (src: string) => {
|
|
204
|
+
// Match $latex$ syntax for inline math (but not $$)
|
|
205
|
+
const match = src.match(/^\$([^$]+)\$(?!\$)/)
|
|
206
|
+
if (!match) {
|
|
207
|
+
return undefined
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
const [fullMatch, latex] = match
|
|
211
|
+
|
|
212
|
+
return {
|
|
213
|
+
type: 'inlineMath',
|
|
214
|
+
raw: fullMatch,
|
|
215
|
+
latex: latex.trim(),
|
|
216
|
+
}
|
|
217
|
+
},
|
|
218
|
+
},
|
|
219
|
+
|
|
184
220
|
addInputRules() {
|
|
185
221
|
return [
|
|
186
222
|
new InputRule({
|