@tiptap/extension-mathematics 3.0.0-beta.17 → 3.0.0-beta.19
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 +78 -21
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +188 -19
- package/dist/index.d.ts +188 -19
- package/dist/index.js +74 -19
- package/dist/index.js.map +1 -1
- package/package.json +5 -5
- package/src/extensions/BlockMath.ts +55 -14
- package/src/extensions/InlineMath.ts +56 -14
- package/src/index.ts +3 -2
- package/src/mathematics.ts +52 -5
- package/src/types.ts +14 -2
- package/src/utils.ts +101 -0
package/dist/index.cjs
CHANGED
|
@@ -32,9 +32,11 @@ var index_exports = {};
|
|
|
32
32
|
__export(index_exports, {
|
|
33
33
|
BlockMath: () => BlockMath,
|
|
34
34
|
InlineMath: () => InlineMath,
|
|
35
|
-
Math: () => Math2,
|
|
36
35
|
Mathematics: () => Mathematics,
|
|
37
|
-
|
|
36
|
+
createMathMigrateTransaction: () => createMathMigrateTransaction,
|
|
37
|
+
default: () => index_default,
|
|
38
|
+
mathMigrationRegex: () => mathMigrationRegex,
|
|
39
|
+
migrateMathStrings: () => migrateMathStrings
|
|
38
40
|
});
|
|
39
41
|
module.exports = __toCommonJS(index_exports);
|
|
40
42
|
|
|
@@ -50,7 +52,8 @@ var BlockMath = import_core.Node.create({
|
|
|
50
52
|
atom: true,
|
|
51
53
|
addOptions() {
|
|
52
54
|
return {
|
|
53
|
-
onClick: void 0
|
|
55
|
+
onClick: void 0,
|
|
56
|
+
katexOptions: void 0
|
|
54
57
|
};
|
|
55
58
|
},
|
|
56
59
|
addAttributes() {
|
|
@@ -68,14 +71,17 @@ var BlockMath = import_core.Node.create({
|
|
|
68
71
|
},
|
|
69
72
|
addCommands() {
|
|
70
73
|
return {
|
|
71
|
-
|
|
74
|
+
insertBlockMath: (options) => ({ commands, editor }) => {
|
|
72
75
|
const { latex, pos } = options;
|
|
76
|
+
if (!latex) {
|
|
77
|
+
return false;
|
|
78
|
+
}
|
|
73
79
|
return commands.insertContentAt(pos != null ? pos : editor.state.selection.from, {
|
|
74
80
|
type: this.name,
|
|
75
81
|
attrs: { latex }
|
|
76
82
|
});
|
|
77
83
|
},
|
|
78
|
-
|
|
84
|
+
deleteBlockMath: (options) => ({ editor, tr }) => {
|
|
79
85
|
var _a;
|
|
80
86
|
const pos = (_a = options == null ? void 0 : options.pos) != null ? _a : editor.state.selection.$from.pos;
|
|
81
87
|
const node = editor.state.doc.nodeAt(pos);
|
|
@@ -128,17 +134,21 @@ var BlockMath = import_core.Node.create({
|
|
|
128
134
|
];
|
|
129
135
|
},
|
|
130
136
|
addNodeView() {
|
|
137
|
+
const { katexOptions } = this.options;
|
|
131
138
|
return ({ node, getPos }) => {
|
|
132
139
|
const wrapper = document.createElement("div");
|
|
133
140
|
const innerWrapper = document.createElement("div");
|
|
134
|
-
wrapper.className = "
|
|
141
|
+
wrapper.className = "tiptap-mathematics-render";
|
|
142
|
+
if (this.editor.isEditable) {
|
|
143
|
+
wrapper.classList.add("tiptap-mathematics-render--editable");
|
|
144
|
+
}
|
|
135
145
|
innerWrapper.className = "block-math-inner";
|
|
136
146
|
wrapper.dataset.type = "block-math";
|
|
137
147
|
wrapper.setAttribute("data-latex", node.attrs.latex);
|
|
138
148
|
wrapper.appendChild(innerWrapper);
|
|
139
149
|
function renderMath() {
|
|
140
150
|
try {
|
|
141
|
-
import_katex.default.render(node.attrs.latex, innerWrapper);
|
|
151
|
+
import_katex.default.render(node.attrs.latex, innerWrapper, katexOptions);
|
|
142
152
|
wrapper.classList.remove("block-math-error");
|
|
143
153
|
} catch {
|
|
144
154
|
wrapper.textContent = node.attrs.latex;
|
|
@@ -180,7 +190,8 @@ var InlineMath = import_core2.Node.create({
|
|
|
180
190
|
atom: true,
|
|
181
191
|
addOptions() {
|
|
182
192
|
return {
|
|
183
|
-
onClick: void 0
|
|
193
|
+
onClick: void 0,
|
|
194
|
+
katexOptions: void 0
|
|
184
195
|
};
|
|
185
196
|
},
|
|
186
197
|
addAttributes() {
|
|
@@ -198,17 +209,17 @@ var InlineMath = import_core2.Node.create({
|
|
|
198
209
|
},
|
|
199
210
|
addCommands() {
|
|
200
211
|
return {
|
|
201
|
-
|
|
212
|
+
insertInlineMath: (options) => ({ editor, tr }) => {
|
|
202
213
|
var _a;
|
|
203
|
-
const latex = options
|
|
204
|
-
const
|
|
214
|
+
const latex = options.latex;
|
|
215
|
+
const from = (_a = options == null ? void 0 : options.pos) != null ? _a : editor.state.selection.from;
|
|
205
216
|
if (!latex) {
|
|
206
217
|
return false;
|
|
207
218
|
}
|
|
208
|
-
tr.replaceWith(
|
|
219
|
+
tr.replaceWith(from, from, this.type.create({ latex }));
|
|
209
220
|
return true;
|
|
210
221
|
},
|
|
211
|
-
|
|
222
|
+
deleteInlineMath: (options) => ({ editor, tr }) => {
|
|
212
223
|
var _a;
|
|
213
224
|
const pos = (_a = options == null ? void 0 : options.pos) != null ? _a : editor.state.selection.$from.pos;
|
|
214
225
|
const node = editor.state.doc.nodeAt(pos);
|
|
@@ -258,14 +269,18 @@ var InlineMath = import_core2.Node.create({
|
|
|
258
269
|
];
|
|
259
270
|
},
|
|
260
271
|
addNodeView() {
|
|
272
|
+
const { katexOptions } = this.options;
|
|
261
273
|
return ({ node, getPos }) => {
|
|
262
274
|
const wrapper = document.createElement("span");
|
|
263
|
-
wrapper.className = "
|
|
275
|
+
wrapper.className = "tiptap-mathematics-render";
|
|
276
|
+
if (this.editor.isEditable) {
|
|
277
|
+
wrapper.classList.add("tiptap-mathematics-render--editable");
|
|
278
|
+
}
|
|
264
279
|
wrapper.dataset.type = "inline-math";
|
|
265
280
|
wrapper.setAttribute("data-latex", node.attrs.latex);
|
|
266
281
|
function renderMath() {
|
|
267
282
|
try {
|
|
268
|
-
import_katex2.default.render(node.attrs.latex, wrapper);
|
|
283
|
+
import_katex2.default.render(node.attrs.latex, wrapper, katexOptions);
|
|
269
284
|
wrapper.classList.remove("inline-math-error");
|
|
270
285
|
} catch {
|
|
271
286
|
wrapper.textContent = node.attrs.latex;
|
|
@@ -298,7 +313,7 @@ var InlineMath = import_core2.Node.create({
|
|
|
298
313
|
});
|
|
299
314
|
|
|
300
315
|
// src/mathematics.ts
|
|
301
|
-
var
|
|
316
|
+
var Mathematics = import_core3.Extension.create({
|
|
302
317
|
name: "Mathematics",
|
|
303
318
|
addOptions() {
|
|
304
319
|
return {
|
|
@@ -308,18 +323,60 @@ var Math2 = import_core3.Extension.create({
|
|
|
308
323
|
};
|
|
309
324
|
},
|
|
310
325
|
addExtensions() {
|
|
311
|
-
return [
|
|
326
|
+
return [
|
|
327
|
+
BlockMath.configure({ ...this.options.blockOptions, katexOptions: this.options.katexOptions }),
|
|
328
|
+
InlineMath.configure({ ...this.options.inlineOptions, katexOptions: this.options.katexOptions })
|
|
329
|
+
];
|
|
312
330
|
}
|
|
313
331
|
});
|
|
314
|
-
|
|
332
|
+
|
|
333
|
+
// src/utils.ts
|
|
334
|
+
var mathMigrationRegex = /(?<!\d)\$(?!\$)(?:[^$\n]|\\\$)*?(?<!\\)\$(?!\d)/g;
|
|
335
|
+
function createMathMigrateTransaction(editor, tr, regex = mathMigrationRegex) {
|
|
336
|
+
tr.doc.descendants((node, pos) => {
|
|
337
|
+
if (!node.isText || !node.text || !node.text.includes("$")) {
|
|
338
|
+
return;
|
|
339
|
+
}
|
|
340
|
+
const { text } = node;
|
|
341
|
+
const match = node.text.match(regex);
|
|
342
|
+
if (!match) {
|
|
343
|
+
return;
|
|
344
|
+
}
|
|
345
|
+
match.forEach((mathMatch) => {
|
|
346
|
+
const start = text.indexOf(mathMatch);
|
|
347
|
+
const end = start + mathMatch.length;
|
|
348
|
+
const from = tr.mapping.map(pos + start);
|
|
349
|
+
const $from = tr.doc.resolve(from);
|
|
350
|
+
const parent = $from.parent;
|
|
351
|
+
const index = $from.index();
|
|
352
|
+
const { inlineMath } = editor.schema.nodes;
|
|
353
|
+
if (!parent.canReplaceWith(index, index + 1, inlineMath)) {
|
|
354
|
+
return;
|
|
355
|
+
}
|
|
356
|
+
tr.replaceWith(
|
|
357
|
+
tr.mapping.map(pos + start),
|
|
358
|
+
tr.mapping.map(pos + end),
|
|
359
|
+
inlineMath.create({ latex: mathMatch.slice(1, -1) })
|
|
360
|
+
);
|
|
361
|
+
});
|
|
362
|
+
});
|
|
363
|
+
tr.setMeta("addToHistory", false);
|
|
364
|
+
return tr;
|
|
365
|
+
}
|
|
366
|
+
function migrateMathStrings(editor, regex = mathMigrationRegex) {
|
|
367
|
+
const tr = createMathMigrateTransaction(editor, editor.state.tr, regex);
|
|
368
|
+
editor.view.dispatch(tr);
|
|
369
|
+
}
|
|
315
370
|
|
|
316
371
|
// src/index.ts
|
|
317
|
-
var index_default =
|
|
372
|
+
var index_default = Mathematics;
|
|
318
373
|
// Annotate the CommonJS export names for ESM import in node:
|
|
319
374
|
0 && (module.exports = {
|
|
320
375
|
BlockMath,
|
|
321
376
|
InlineMath,
|
|
322
|
-
|
|
323
|
-
|
|
377
|
+
Mathematics,
|
|
378
|
+
createMathMigrateTransaction,
|
|
379
|
+
mathMigrationRegex,
|
|
380
|
+
migrateMathStrings
|
|
324
381
|
});
|
|
325
382
|
//# sourceMappingURL=index.cjs.map
|
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"],"sourcesContent":["import { Math } from './mathematics.js'\n\nexport * from './extensions/index.js'\nexport * from './mathematics.js'\nexport * from './types.js'\n\nexport default Math\n","import { Extension } from '@tiptap/core'\n\nimport { BlockMath, InlineMath } from './extensions/index.js'\nimport type { MathematicsOptions } from './types.js'\n\nexport const Math = 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 [BlockMath.configure(this.options.blockOptions), InlineMath.configure(this.options.inlineOptions)]\n },\n})\n\nexport const Mathematics = Math\n\nexport default Math\n","import { InputRule, mergeAttributes, Node } from '@tiptap/core'\nimport type { Node as PMNode } from '@tiptap/pm/model'\nimport katex from 'katex'\n\nexport type BlockMathOptions = {\n onClick?: (node: PMNode, pos: number) => void\n}\n\ndeclare module '@tiptap/core' {\n interface Commands<ReturnType> {\n blockMath: {\n /**\n * Set block math node with LaTeX string.\n * @param options - Options for setting block math.\n * @returns ReturnType\n */\n setBlockMath: (options: { latex: string; pos?: number }) => ReturnType\n\n /**\n * Unset block math node.\n * @returns ReturnType\n */\n unsetBlockMath: (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 'your-extension-path'\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({\n name: 'blockMath',\n\n group: 'block',\n\n atom: true,\n\n addOptions() {\n return {\n onClick: 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 setBlockMath:\n options =>\n ({ commands, editor }) => {\n const { latex, pos } = options\n return commands.insertContentAt(pos ?? editor.state.selection.from, {\n type: this.name,\n attrs: { latex },\n })\n },\n\n unsetBlockMath:\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 return ({ node, getPos }) => {\n const wrapper = document.createElement('div')\n const innerWrapper = document.createElement('div')\n wrapper.className = 'Tiptap-mathematics-render Tiptap-mathematics-render--editable'\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)\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 from 'katex'\n\nexport type InlineMathOptions = {\n onClick?: (node: PMNode, pos: number) => void\n}\n\ndeclare module '@tiptap/core' {\n interface Commands<ReturnType> {\n inlineMath: {\n /**\n * Set inline math node with LaTeX string.\n * @param options - Options for setting inline math.\n * @returns ReturnType\n */\n setInlineMath: (options: { latex: string; pos?: number }) => ReturnType\n\n /**\n * Unset inline math node.\n * @returns ReturnType\n */\n unsetInlineMath: (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 'your-extension-path'\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 }\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 setInlineMath:\n options =>\n ({ editor, tr }) => {\n const latex = options?.latex\n const pos = options?.pos ?? editor.state.selection.$from.pos\n\n if (!latex) {\n return false\n }\n\n tr.replaceWith(pos, pos, this.type.create({ latex }))\n return true\n },\n\n unsetInlineMath:\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\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 return ({ node, getPos }) => {\n const wrapper = document.createElement('span')\n wrapper.className = 'Tiptap-mathematics-render Tiptap-mathematics-render--editable'\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)\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: wr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AAO,uBAAU,OAA2B;AAAA,EACvD,MAAM;AAAA,EAEN,aAAa;AACX,WAAO;AAAA,MACL,eAAe;AAAA,MACf,cAAc;AAAA,MACd,cAAc;AAAA,IAChB;AAAA,EACF;AAAA,EAEA,gBAAgB;AACd,WAAO,CAAC,UAAU,UAAU,KAAK,QAAQ,YAAY,GAAG,WAAW,UAAU,KAAK,QAAQ,aAAa,CAAC;AAAA,EAC1G;AACF,CAAC;AAEM,IAAM,cAAcA;;;ADf3B,IAAO,gBAAQC;","names":["Math","import_core","katex","import_core","import_katex","katex","Math","Math"]}
|
|
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\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)\\$(?!\\$)(?:[^$\\n]|\\\\\\$)*?(?<!\\\\)\\$(?!\\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;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IAAO,gBAAQ;","names":["import_core","katex","import_core","import_katex","katex"]}
|
package/dist/index.d.cts
CHANGED
|
@@ -1,27 +1,55 @@
|
|
|
1
1
|
import { Node as Node$1, Editor, Extension } from '@tiptap/core';
|
|
2
2
|
import { KatexOptions } from 'katex';
|
|
3
3
|
import { Node } from '@tiptap/pm/model';
|
|
4
|
+
import { Transaction } from '@tiptap/pm/state';
|
|
4
5
|
|
|
6
|
+
/**
|
|
7
|
+
* Configuration options for the BlockMath extension.
|
|
8
|
+
*/
|
|
5
9
|
type BlockMathOptions = {
|
|
10
|
+
/**
|
|
11
|
+
* KaTeX specific options
|
|
12
|
+
* @see https://katex.org/docs/options.html
|
|
13
|
+
* @example
|
|
14
|
+
* ```ts
|
|
15
|
+
* katexOptions: {
|
|
16
|
+
* displayMode: true,
|
|
17
|
+
* throwOnError: false,
|
|
18
|
+
* },
|
|
19
|
+
*/
|
|
20
|
+
katexOptions?: KatexOptions;
|
|
21
|
+
/**
|
|
22
|
+
* Optional click handler for block math nodes.
|
|
23
|
+
* Called when a user clicks on a block math expression in the editor.
|
|
24
|
+
*
|
|
25
|
+
* @param node - The ProseMirror node representing the block math element
|
|
26
|
+
* @param pos - The position of the node within the document
|
|
27
|
+
* @example
|
|
28
|
+
* ```ts
|
|
29
|
+
* onClick: (node, pos) => {
|
|
30
|
+
* console.log('Block math clicked:', node.attrs.latex, 'at position:', pos)
|
|
31
|
+
* },
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
6
34
|
onClick?: (node: Node, pos: number) => void;
|
|
7
35
|
};
|
|
8
36
|
declare module '@tiptap/core' {
|
|
9
37
|
interface Commands<ReturnType> {
|
|
10
|
-
|
|
38
|
+
insertBlockMath: {
|
|
11
39
|
/**
|
|
12
|
-
*
|
|
13
|
-
* @param options - Options for
|
|
40
|
+
* Inserts a math block node with LaTeX string.
|
|
41
|
+
* @param options - Options for inserting block math.
|
|
14
42
|
* @returns ReturnType
|
|
15
43
|
*/
|
|
16
|
-
|
|
44
|
+
insertBlockMath: (options: {
|
|
17
45
|
latex: string;
|
|
18
46
|
pos?: number;
|
|
19
47
|
}) => ReturnType;
|
|
20
48
|
/**
|
|
21
|
-
*
|
|
49
|
+
* Deletes a block math node.
|
|
22
50
|
* @returns ReturnType
|
|
23
51
|
*/
|
|
24
|
-
|
|
52
|
+
deleteBlockMath: (options?: {
|
|
25
53
|
pos?: number;
|
|
26
54
|
}) => ReturnType;
|
|
27
55
|
/**
|
|
@@ -30,7 +58,7 @@ declare module '@tiptap/core' {
|
|
|
30
58
|
* @returns ReturnType
|
|
31
59
|
*/
|
|
32
60
|
updateBlockMath: (options?: {
|
|
33
|
-
latex
|
|
61
|
+
latex: string;
|
|
34
62
|
pos?: number;
|
|
35
63
|
}) => ReturnType;
|
|
36
64
|
};
|
|
@@ -43,7 +71,7 @@ declare module '@tiptap/core' {
|
|
|
43
71
|
*
|
|
44
72
|
* @example
|
|
45
73
|
* ```javascript
|
|
46
|
-
* import { BlockMath } from '
|
|
74
|
+
* import { BlockMath } from '@tiptap/extension-mathematics'
|
|
47
75
|
* import { Editor } from '@tiptap/core'
|
|
48
76
|
*
|
|
49
77
|
* const editor = new Editor({
|
|
@@ -56,28 +84,60 @@ declare module '@tiptap/core' {
|
|
|
56
84
|
* ],
|
|
57
85
|
* })
|
|
58
86
|
*/
|
|
59
|
-
declare const BlockMath: Node$1<
|
|
87
|
+
declare const BlockMath: Node$1<BlockMathOptions, any>;
|
|
60
88
|
|
|
89
|
+
/**
|
|
90
|
+
* Configuration options for the InlineMath extension.
|
|
91
|
+
*/
|
|
61
92
|
type InlineMathOptions = {
|
|
93
|
+
/**
|
|
94
|
+
* KaTeX specific options
|
|
95
|
+
* @see https://katex.org/docs/options.html
|
|
96
|
+
* @example
|
|
97
|
+
* ```ts
|
|
98
|
+
* katexOptions: {
|
|
99
|
+
* displayMode: false,
|
|
100
|
+
* throwOnError: false,
|
|
101
|
+
* macros: {
|
|
102
|
+
* '\\RR': '\\mathbb{R}',
|
|
103
|
+
* '\\ZZ': '\\mathbb{Z}'
|
|
104
|
+
* }
|
|
105
|
+
* }
|
|
106
|
+
* ```
|
|
107
|
+
*/
|
|
108
|
+
katexOptions?: KatexOptions;
|
|
109
|
+
/**
|
|
110
|
+
* Optional click handler for inline math nodes.
|
|
111
|
+
* Called when a user clicks on an inline math expression in the editor.
|
|
112
|
+
*
|
|
113
|
+
* @param node - The ProseMirror node representing the inline math element
|
|
114
|
+
* @param pos - The position of the node within the document
|
|
115
|
+
* @example
|
|
116
|
+
* ```ts
|
|
117
|
+
* onClick: (node, pos) => {
|
|
118
|
+
* console.log('Inline math clicked:', node.attrs.latex, 'at position:', pos)
|
|
119
|
+
* }
|
|
120
|
+
* ```
|
|
121
|
+
*/
|
|
62
122
|
onClick?: (node: Node, pos: number) => void;
|
|
63
123
|
};
|
|
64
124
|
declare module '@tiptap/core' {
|
|
65
125
|
interface Commands<ReturnType> {
|
|
66
126
|
inlineMath: {
|
|
67
127
|
/**
|
|
68
|
-
*
|
|
69
|
-
* @param options - Options for
|
|
128
|
+
* Insert a inline math node with LaTeX string.
|
|
129
|
+
* @param options - Options for inserting inline math.
|
|
70
130
|
* @returns ReturnType
|
|
71
131
|
*/
|
|
72
|
-
|
|
132
|
+
insertInlineMath: (options: {
|
|
73
133
|
latex: string;
|
|
74
134
|
pos?: number;
|
|
75
135
|
}) => ReturnType;
|
|
76
136
|
/**
|
|
77
|
-
*
|
|
137
|
+
* Delete an inline math node.
|
|
78
138
|
* @returns ReturnType
|
|
79
139
|
*/
|
|
80
|
-
|
|
140
|
+
deleteInlineMath: (options?: {
|
|
81
141
|
pos?: number;
|
|
82
142
|
}) => ReturnType;
|
|
83
143
|
/**
|
|
@@ -99,7 +159,7 @@ declare module '@tiptap/core' {
|
|
|
99
159
|
*
|
|
100
160
|
* @example
|
|
101
161
|
* ```javascript
|
|
102
|
-
* import { InlineMath } from '
|
|
162
|
+
* import { InlineMath } from '@tiptap/extension-mathematics'
|
|
103
163
|
* import { Editor } from '@tiptap/core'
|
|
104
164
|
*
|
|
105
165
|
* const editor = new Editor({
|
|
@@ -114,16 +174,125 @@ declare module '@tiptap/core' {
|
|
|
114
174
|
*/
|
|
115
175
|
declare const InlineMath: Node$1<InlineMathOptions, any>;
|
|
116
176
|
|
|
177
|
+
/**
|
|
178
|
+
* Configuration options for the Mathematics extension.
|
|
179
|
+
* This type defines the available customization options for both inline and block math rendering.
|
|
180
|
+
*/
|
|
117
181
|
type MathematicsOptions = {
|
|
118
|
-
|
|
119
|
-
|
|
182
|
+
/** Configuration options specific to inline math nodes */
|
|
183
|
+
inlineOptions?: Omit<InlineMathOptions, 'katexOptions'>;
|
|
184
|
+
/** Configuration options specific to block math nodes */
|
|
185
|
+
blockOptions?: Omit<BlockMathOptions, 'katexOptions'>;
|
|
186
|
+
/** KaTeX-specific rendering options passed to the KaTeX library */
|
|
120
187
|
katexOptions?: KatexOptions;
|
|
121
188
|
};
|
|
189
|
+
/**
|
|
190
|
+
* Extended mathematics options that include an editor instance.
|
|
191
|
+
* This type combines the base mathematics options with an editor reference,
|
|
192
|
+
* typically used internally by the extension for operations that require editor access.
|
|
193
|
+
*/
|
|
122
194
|
type MathematicsOptionsWithEditor = MathematicsOptions & {
|
|
123
195
|
editor: Editor;
|
|
124
196
|
};
|
|
125
197
|
|
|
126
|
-
|
|
198
|
+
/**
|
|
199
|
+
* Mathematics extension for Tiptap that provides both inline and block math support using KaTeX.
|
|
200
|
+
* This extension combines the InlineMath and BlockMath extensions to provide a complete
|
|
201
|
+
* mathematical expression solution for rich text editing. It supports LaTeX syntax,
|
|
202
|
+
* custom rendering options, and interactive math nodes.
|
|
203
|
+
*
|
|
204
|
+
* @example
|
|
205
|
+
* ```typescript
|
|
206
|
+
* import { Editor } from '@tiptap/core'
|
|
207
|
+
* import { Mathematics } from '@tiptap/extension-mathematics'
|
|
208
|
+
* import { migrateMathStrings } from '@tiptap/extension-mathematics/utils'
|
|
209
|
+
*
|
|
210
|
+
* const editor = new Editor({
|
|
211
|
+
* extensions: [
|
|
212
|
+
* Mathematics.configure({
|
|
213
|
+
* inlineOptions: {
|
|
214
|
+
* onClick: (node, pos) => {
|
|
215
|
+
* console.log('Inline math clicked:', node.attrs.latex)
|
|
216
|
+
* }
|
|
217
|
+
* },
|
|
218
|
+
* blockOptions: {
|
|
219
|
+
* onClick: (node, pos) => {
|
|
220
|
+
* console.log('Block math clicked:', node.attrs.latex)
|
|
221
|
+
* }
|
|
222
|
+
* },
|
|
223
|
+
* katexOptions: {
|
|
224
|
+
* displayMode: false,
|
|
225
|
+
* throwOnError: false,
|
|
226
|
+
* macros: {
|
|
227
|
+
* '\\RR': '\\mathbb{R}',
|
|
228
|
+
* '\\ZZ': '\\mathbb{Z}'
|
|
229
|
+
* }
|
|
230
|
+
* }
|
|
231
|
+
* })
|
|
232
|
+
* ],
|
|
233
|
+
* content: `
|
|
234
|
+
* <p>Inline math: $E = mc^2$</p>
|
|
235
|
+
* <div data-type="block-math" data-latex="\\sum_{i=1}^{n} x_i = X"></div>
|
|
236
|
+
* `,
|
|
237
|
+
* onCreate({ editor }) {
|
|
238
|
+
* // Optional: Migrate existing math strings to math nodes
|
|
239
|
+
* migrateMathStrings(editor)
|
|
240
|
+
* }
|
|
241
|
+
* })
|
|
242
|
+
* ```
|
|
243
|
+
*/
|
|
127
244
|
declare const Mathematics: Extension<MathematicsOptions, any>;
|
|
128
245
|
|
|
129
|
-
|
|
246
|
+
/**
|
|
247
|
+
* Regular expression to match LaTeX math strings wrapped in single dollar signs.
|
|
248
|
+
* This should not catch dollar signs which are not part of a math expression,
|
|
249
|
+
* like those used for currency or other purposes.
|
|
250
|
+
* It ensures that the dollar signs are not preceded or followed by digits,
|
|
251
|
+
* allowing for proper identification of inline math expressions.
|
|
252
|
+
*
|
|
253
|
+
* - `$x^2 + y^2 = z^2$` will match
|
|
254
|
+
* - `This is $inline math$ in text.` will match
|
|
255
|
+
* - `This is $100$ dollars.` will not match (as it is not a math expression)
|
|
256
|
+
* - `This is $x^2 + y^2 = z^2$ and $100$ dollars.` will match both math expressions
|
|
257
|
+
*/
|
|
258
|
+
declare const mathMigrationRegex: RegExp;
|
|
259
|
+
/**
|
|
260
|
+
* Creates a transaction that migrates existing math strings in the document to new math nodes.
|
|
261
|
+
* This function traverses the document and replaces LaTeX math syntax (wrapped in single dollar signs)
|
|
262
|
+
* with proper inline math nodes, preserving the mathematical content.
|
|
263
|
+
*
|
|
264
|
+
* @param editor - The editor instance containing the schema and configuration
|
|
265
|
+
* @param tr - The transaction to modify with the migration operations
|
|
266
|
+
* @returns The modified transaction with math string replacements
|
|
267
|
+
*
|
|
268
|
+
* @example
|
|
269
|
+
* ```typescript
|
|
270
|
+
* const editor = new Editor({ ... })
|
|
271
|
+
* const tr = editor.state.tr
|
|
272
|
+
* const updatedTr = createMathMigrateTransaction(editor, tr)
|
|
273
|
+
* editor.view.dispatch(updatedTr)
|
|
274
|
+
* ```
|
|
275
|
+
*/
|
|
276
|
+
declare function createMathMigrateTransaction(editor: Editor, tr: Transaction, regex?: RegExp): Transaction;
|
|
277
|
+
/**
|
|
278
|
+
* Migrates existing math strings in the editor document to math nodes.
|
|
279
|
+
* This function creates and dispatches a transaction that converts LaTeX math syntax
|
|
280
|
+
* (text wrapped in single dollar signs) into proper inline math nodes. The migration
|
|
281
|
+
* happens immediately and is not added to the editor's history.
|
|
282
|
+
*
|
|
283
|
+
* @param editor - The editor instance to perform the migration on
|
|
284
|
+
*
|
|
285
|
+
* @example
|
|
286
|
+
* ```typescript
|
|
287
|
+
* const editor = new Editor({
|
|
288
|
+
* extensions: [Mathematics],
|
|
289
|
+
* content: 'This is inline math: $x^2 + y^2 = z^2$ in text.'
|
|
290
|
+
* })
|
|
291
|
+
*
|
|
292
|
+
* // Math strings will be automatically migrated to math nodes
|
|
293
|
+
* migrateMathStrings(editor)
|
|
294
|
+
* ```
|
|
295
|
+
*/
|
|
296
|
+
declare function migrateMathStrings(editor: Editor, regex?: RegExp): void;
|
|
297
|
+
|
|
298
|
+
export { BlockMath, type BlockMathOptions, InlineMath, type InlineMathOptions, Mathematics, type MathematicsOptions, type MathematicsOptionsWithEditor, createMathMigrateTransaction, Mathematics as default, mathMigrationRegex, migrateMathStrings };
|