@tiptap/extension-mathematics 3.0.0-beta.16 → 3.0.0-beta.18
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 +320 -146
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +286 -16
- package/dist/index.d.ts +286 -16
- package/dist/index.js +314 -143
- package/dist/index.js.map +1 -1
- package/package.json +5 -5
- package/src/extensions/BlockMath.ts +251 -0
- package/src/extensions/InlineMath.ts +252 -0
- package/src/extensions/index.ts +2 -0
- package/src/index.ts +2 -1
- package/src/mathematics.ts +54 -14
- package/src/types.ts +16 -4
- package/src/utils.ts +101 -0
- package/src/MathematicsPlugin.ts +0 -205
package/dist/index.cjs
CHANGED
|
@@ -30,179 +30,353 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
30
30
|
// src/index.ts
|
|
31
31
|
var index_exports = {};
|
|
32
32
|
__export(index_exports, {
|
|
33
|
+
BlockMath: () => BlockMath,
|
|
34
|
+
InlineMath: () => InlineMath,
|
|
33
35
|
Mathematics: () => Mathematics,
|
|
34
|
-
|
|
36
|
+
createMathMigrateTransaction: () => createMathMigrateTransaction,
|
|
35
37
|
default: () => index_default,
|
|
36
|
-
|
|
38
|
+
mathMigrationRegex: () => mathMigrationRegex,
|
|
39
|
+
migrateMathStrings: () => migrateMathStrings
|
|
37
40
|
});
|
|
38
41
|
module.exports = __toCommonJS(index_exports);
|
|
39
42
|
|
|
40
43
|
// src/mathematics.ts
|
|
41
|
-
var
|
|
44
|
+
var import_core3 = require("@tiptap/core");
|
|
42
45
|
|
|
43
|
-
// src/
|
|
46
|
+
// src/extensions/BlockMath.ts
|
|
44
47
|
var import_core = require("@tiptap/core");
|
|
45
|
-
var import_state = require("@tiptap/pm/state");
|
|
46
|
-
var import_view = require("@tiptap/pm/view");
|
|
47
48
|
var import_katex = __toESM(require("katex"), 1);
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
init() {
|
|
83
|
-
return { decorations: void 0, isEditable: void 0 };
|
|
49
|
+
var BlockMath = import_core.Node.create({
|
|
50
|
+
name: "blockMath",
|
|
51
|
+
group: "block",
|
|
52
|
+
atom: true,
|
|
53
|
+
addOptions() {
|
|
54
|
+
return {
|
|
55
|
+
onClick: void 0,
|
|
56
|
+
katexOptions: void 0
|
|
57
|
+
};
|
|
58
|
+
},
|
|
59
|
+
addAttributes() {
|
|
60
|
+
return {
|
|
61
|
+
latex: {
|
|
62
|
+
default: "",
|
|
63
|
+
parseHTML: (element) => element.getAttribute("data-latex"),
|
|
64
|
+
renderHTML: (attributes) => {
|
|
65
|
+
return {
|
|
66
|
+
"data-latex": attributes.latex
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
},
|
|
72
|
+
addCommands() {
|
|
73
|
+
return {
|
|
74
|
+
insertBlockMath: (options) => ({ commands, editor }) => {
|
|
75
|
+
const { latex, pos } = options;
|
|
76
|
+
if (!latex) {
|
|
77
|
+
return false;
|
|
78
|
+
}
|
|
79
|
+
return commands.insertContentAt(pos != null ? pos : editor.state.selection.from, {
|
|
80
|
+
type: this.name,
|
|
81
|
+
attrs: { latex }
|
|
82
|
+
});
|
|
84
83
|
},
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
// Are the decorations already present?
|
|
109
|
-
nextDecorationSet.find(
|
|
110
|
-
from,
|
|
111
|
-
to,
|
|
112
|
-
(deco) => isEditing === deco.isEditing && content === deco.content && isEditable === deco.isEditable && katexOptions === deco.katexOptions
|
|
113
|
-
).length
|
|
114
|
-
) {
|
|
115
|
-
continue;
|
|
116
|
-
}
|
|
117
|
-
decorationsToAdd.push(
|
|
118
|
-
import_view.Decoration.inline(
|
|
119
|
-
from,
|
|
120
|
-
to,
|
|
121
|
-
{
|
|
122
|
-
class: isEditing && isEditable ? "Tiptap-mathematics-editor" : "Tiptap-mathematics-editor Tiptap-mathematics-editor--hidden",
|
|
123
|
-
style: !isEditing || !isEditable ? "display: inline-block; height: 0; opacity: 0; overflow: hidden; position: absolute; width: 0;" : void 0
|
|
124
|
-
},
|
|
125
|
-
{
|
|
126
|
-
content,
|
|
127
|
-
isEditable,
|
|
128
|
-
isEditing,
|
|
129
|
-
katexOptions
|
|
130
|
-
}
|
|
131
|
-
)
|
|
132
|
-
);
|
|
133
|
-
if (!isEditable || !isEditing) {
|
|
134
|
-
decorationsToAdd.push(
|
|
135
|
-
import_view.Decoration.widget(
|
|
136
|
-
from,
|
|
137
|
-
() => {
|
|
138
|
-
const element = document.createElement("span");
|
|
139
|
-
element.classList.add("Tiptap-mathematics-render");
|
|
140
|
-
if (isEditable) {
|
|
141
|
-
element.classList.add("Tiptap-mathematics-render--editable");
|
|
142
|
-
}
|
|
143
|
-
try {
|
|
144
|
-
import_katex.default.render(content, element, katexOptions);
|
|
145
|
-
} catch {
|
|
146
|
-
element.innerHTML = content;
|
|
147
|
-
}
|
|
148
|
-
return element;
|
|
149
|
-
},
|
|
150
|
-
{
|
|
151
|
-
content,
|
|
152
|
-
isEditable,
|
|
153
|
-
isEditing,
|
|
154
|
-
katexOptions
|
|
155
|
-
}
|
|
156
|
-
)
|
|
157
|
-
);
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
}
|
|
84
|
+
deleteBlockMath: (options) => ({ editor, tr }) => {
|
|
85
|
+
var _a;
|
|
86
|
+
const pos = (_a = options == null ? void 0 : options.pos) != null ? _a : editor.state.selection.$from.pos;
|
|
87
|
+
const node = editor.state.doc.nodeAt(pos);
|
|
88
|
+
if (!node || node.type.name !== this.name) {
|
|
89
|
+
return false;
|
|
90
|
+
}
|
|
91
|
+
tr.delete(pos, pos + node.nodeSize);
|
|
92
|
+
return true;
|
|
93
|
+
},
|
|
94
|
+
updateBlockMath: (options) => ({ editor, tr }) => {
|
|
95
|
+
const latex = options == null ? void 0 : options.latex;
|
|
96
|
+
let pos = options == null ? void 0 : options.pos;
|
|
97
|
+
if (pos === void 0) {
|
|
98
|
+
pos = editor.state.selection.$from.pos;
|
|
99
|
+
}
|
|
100
|
+
const node = editor.state.doc.nodeAt(pos);
|
|
101
|
+
if (!node || node.type.name !== this.name) {
|
|
102
|
+
return false;
|
|
103
|
+
}
|
|
104
|
+
tr.setNodeMarkup(pos, this.type, {
|
|
105
|
+
...node.attrs,
|
|
106
|
+
latex: latex || node.attrs.latex
|
|
162
107
|
});
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
108
|
+
return true;
|
|
109
|
+
}
|
|
110
|
+
};
|
|
111
|
+
},
|
|
112
|
+
parseHTML() {
|
|
113
|
+
return [
|
|
114
|
+
{
|
|
115
|
+
tag: 'div[data-type="block-math"]'
|
|
168
116
|
}
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
117
|
+
];
|
|
118
|
+
},
|
|
119
|
+
renderHTML({ HTMLAttributes }) {
|
|
120
|
+
return ["div", (0, import_core.mergeAttributes)(HTMLAttributes, { "data-type": "block-math" })];
|
|
121
|
+
},
|
|
122
|
+
addInputRules() {
|
|
123
|
+
return [
|
|
124
|
+
new import_core.InputRule({
|
|
125
|
+
find: /^\$\$\$([^$]+)\$\$\$$/,
|
|
126
|
+
handler: ({ state, range, match }) => {
|
|
127
|
+
const [, latex] = match;
|
|
128
|
+
const { tr } = state;
|
|
129
|
+
const start = range.from;
|
|
130
|
+
const end = range.to;
|
|
131
|
+
tr.replaceWith(start, end, this.type.create({ latex }));
|
|
132
|
+
}
|
|
133
|
+
})
|
|
134
|
+
];
|
|
135
|
+
},
|
|
136
|
+
addNodeView() {
|
|
137
|
+
const { katexOptions } = this.options;
|
|
138
|
+
return ({ node, getPos }) => {
|
|
139
|
+
const wrapper = document.createElement("div");
|
|
140
|
+
const innerWrapper = document.createElement("div");
|
|
141
|
+
wrapper.className = "tiptap-mathematics-render";
|
|
142
|
+
if (this.editor.isEditable) {
|
|
143
|
+
wrapper.classList.add("tiptap-mathematics-render--editable");
|
|
174
144
|
}
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
145
|
+
innerWrapper.className = "block-math-inner";
|
|
146
|
+
wrapper.dataset.type = "block-math";
|
|
147
|
+
wrapper.setAttribute("data-latex", node.attrs.latex);
|
|
148
|
+
wrapper.appendChild(innerWrapper);
|
|
149
|
+
function renderMath() {
|
|
150
|
+
try {
|
|
151
|
+
import_katex.default.render(node.attrs.latex, innerWrapper, katexOptions);
|
|
152
|
+
wrapper.classList.remove("block-math-error");
|
|
153
|
+
} catch {
|
|
154
|
+
wrapper.textContent = node.attrs.latex;
|
|
155
|
+
wrapper.classList.add("block-math-error");
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
const handleClick = (event) => {
|
|
159
|
+
event.preventDefault();
|
|
160
|
+
event.stopPropagation();
|
|
161
|
+
const pos = getPos();
|
|
162
|
+
if (pos == null) {
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
if (this.options.onClick) {
|
|
166
|
+
this.options.onClick(node, pos);
|
|
167
|
+
}
|
|
168
|
+
};
|
|
169
|
+
if (this.options.onClick) {
|
|
170
|
+
wrapper.addEventListener("click", handleClick);
|
|
171
|
+
}
|
|
172
|
+
renderMath();
|
|
173
|
+
return {
|
|
174
|
+
dom: wrapper,
|
|
175
|
+
destroy() {
|
|
176
|
+
wrapper.removeEventListener("click", handleClick);
|
|
177
|
+
}
|
|
178
|
+
};
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
// src/extensions/InlineMath.ts
|
|
184
|
+
var import_core2 = require("@tiptap/core");
|
|
185
|
+
var import_katex2 = __toESM(require("katex"), 1);
|
|
186
|
+
var InlineMath = import_core2.Node.create({
|
|
187
|
+
name: "inlineMath",
|
|
188
|
+
group: "inline",
|
|
189
|
+
inline: true,
|
|
190
|
+
atom: true,
|
|
191
|
+
addOptions() {
|
|
192
|
+
return {
|
|
193
|
+
onClick: void 0,
|
|
194
|
+
katexOptions: void 0
|
|
195
|
+
};
|
|
196
|
+
},
|
|
197
|
+
addAttributes() {
|
|
198
|
+
return {
|
|
199
|
+
latex: {
|
|
200
|
+
default: "",
|
|
201
|
+
parseHTML: (element) => element.getAttribute("data-latex"),
|
|
202
|
+
renderHTML: (attributes) => {
|
|
203
|
+
return {
|
|
204
|
+
"data-latex": attributes.latex
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
};
|
|
209
|
+
},
|
|
210
|
+
addCommands() {
|
|
211
|
+
return {
|
|
212
|
+
insertInlineMath: (options) => ({ editor, tr }) => {
|
|
213
|
+
var _a;
|
|
214
|
+
const latex = options.latex;
|
|
215
|
+
const from = (_a = options == null ? void 0 : options.pos) != null ? _a : editor.state.selection.from;
|
|
216
|
+
if (!latex) {
|
|
217
|
+
return false;
|
|
218
|
+
}
|
|
219
|
+
tr.replaceWith(from, from, this.type.create({ latex }));
|
|
220
|
+
return true;
|
|
221
|
+
},
|
|
222
|
+
deleteInlineMath: (options) => ({ editor, tr }) => {
|
|
223
|
+
var _a;
|
|
224
|
+
const pos = (_a = options == null ? void 0 : options.pos) != null ? _a : editor.state.selection.$from.pos;
|
|
225
|
+
const node = editor.state.doc.nodeAt(pos);
|
|
226
|
+
if (!node || node.type.name !== this.name) {
|
|
227
|
+
return false;
|
|
228
|
+
}
|
|
229
|
+
tr.delete(pos, pos + node.nodeSize);
|
|
230
|
+
return true;
|
|
231
|
+
},
|
|
232
|
+
updateInlineMath: (options) => ({ editor, tr }) => {
|
|
233
|
+
const latex = options == null ? void 0 : options.latex;
|
|
234
|
+
let pos = options == null ? void 0 : options.pos;
|
|
235
|
+
if (pos === void 0) {
|
|
236
|
+
pos = editor.state.selection.$from.pos;
|
|
237
|
+
}
|
|
238
|
+
const node = editor.state.doc.nodeAt(pos);
|
|
239
|
+
if (!node || node.type.name !== this.name) {
|
|
240
|
+
return false;
|
|
241
|
+
}
|
|
242
|
+
tr.setNodeMarkup(pos, this.type, { ...node.attrs, latex });
|
|
243
|
+
return true;
|
|
244
|
+
}
|
|
245
|
+
};
|
|
246
|
+
},
|
|
247
|
+
parseHTML() {
|
|
248
|
+
return [
|
|
249
|
+
{
|
|
250
|
+
tag: 'span[data-type="inline-math"]'
|
|
251
|
+
}
|
|
252
|
+
];
|
|
253
|
+
},
|
|
254
|
+
renderHTML({ HTMLAttributes }) {
|
|
255
|
+
return ["span", (0, import_core2.mergeAttributes)(HTMLAttributes, { "data-type": "inline-math" })];
|
|
256
|
+
},
|
|
257
|
+
addInputRules() {
|
|
258
|
+
return [
|
|
259
|
+
new import_core2.InputRule({
|
|
260
|
+
find: /(?<!\$)\$\$([^$\n]+)\$\$(?!\$)$/,
|
|
261
|
+
handler: ({ state, range, match }) => {
|
|
262
|
+
const [, latex] = match;
|
|
263
|
+
const { tr } = state;
|
|
264
|
+
const start = range.from;
|
|
265
|
+
const end = range.to;
|
|
266
|
+
tr.replaceWith(start, end, this.type.create({ latex }));
|
|
267
|
+
}
|
|
268
|
+
})
|
|
269
|
+
];
|
|
270
|
+
},
|
|
271
|
+
addNodeView() {
|
|
272
|
+
const { katexOptions } = this.options;
|
|
273
|
+
return ({ node, getPos }) => {
|
|
274
|
+
const wrapper = document.createElement("span");
|
|
275
|
+
wrapper.className = "tiptap-mathematics-render";
|
|
276
|
+
if (this.editor.isEditable) {
|
|
277
|
+
wrapper.classList.add("tiptap-mathematics-render--editable");
|
|
278
|
+
}
|
|
279
|
+
wrapper.dataset.type = "inline-math";
|
|
280
|
+
wrapper.setAttribute("data-latex", node.attrs.latex);
|
|
281
|
+
function renderMath() {
|
|
282
|
+
try {
|
|
283
|
+
import_katex2.default.render(node.attrs.latex, wrapper, katexOptions);
|
|
284
|
+
wrapper.classList.remove("inline-math-error");
|
|
285
|
+
} catch {
|
|
286
|
+
wrapper.textContent = node.attrs.latex;
|
|
287
|
+
wrapper.classList.add("inline-math-error");
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
const handleClick = (event) => {
|
|
291
|
+
event.preventDefault();
|
|
292
|
+
event.stopPropagation();
|
|
293
|
+
const pos = getPos();
|
|
294
|
+
if (pos == null) {
|
|
295
|
+
return;
|
|
296
|
+
}
|
|
297
|
+
if (this.options.onClick) {
|
|
298
|
+
this.options.onClick(node, pos);
|
|
299
|
+
}
|
|
300
|
+
};
|
|
301
|
+
if (this.options.onClick) {
|
|
302
|
+
wrapper.addEventListener("click", handleClick);
|
|
303
|
+
}
|
|
304
|
+
renderMath();
|
|
305
|
+
return {
|
|
306
|
+
dom: wrapper,
|
|
307
|
+
destroy() {
|
|
308
|
+
wrapper.removeEventListener("click", handleClick);
|
|
309
|
+
}
|
|
310
|
+
};
|
|
311
|
+
};
|
|
312
|
+
}
|
|
313
|
+
});
|
|
178
314
|
|
|
179
315
|
// src/mathematics.ts
|
|
180
|
-
var
|
|
181
|
-
const $pos = state.doc.resolve(pos);
|
|
182
|
-
const isInCodeBlock = $pos.parent.type.name === "codeBlock";
|
|
183
|
-
return !isInCodeBlock;
|
|
184
|
-
};
|
|
185
|
-
var Mathematics = import_core2.Extension.create({
|
|
316
|
+
var Mathematics = import_core3.Extension.create({
|
|
186
317
|
name: "Mathematics",
|
|
187
318
|
addOptions() {
|
|
188
319
|
return {
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
katexOptions: void 0
|
|
192
|
-
shouldRender: defaultShouldRender
|
|
320
|
+
inlineOptions: void 0,
|
|
321
|
+
blockOptions: void 0,
|
|
322
|
+
katexOptions: void 0
|
|
193
323
|
};
|
|
194
324
|
},
|
|
195
|
-
|
|
196
|
-
return [
|
|
325
|
+
addExtensions() {
|
|
326
|
+
return [
|
|
327
|
+
BlockMath.configure({ ...this.options.blockOptions, katexOptions: this.options.katexOptions }),
|
|
328
|
+
InlineMath.configure({ ...this.options.inlineOptions, katexOptions: this.options.katexOptions })
|
|
329
|
+
];
|
|
197
330
|
}
|
|
198
331
|
});
|
|
199
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
|
+
}
|
|
370
|
+
|
|
200
371
|
// src/index.ts
|
|
201
372
|
var index_default = Mathematics;
|
|
202
373
|
// Annotate the CommonJS export names for ESM import in node:
|
|
203
374
|
0 && (module.exports = {
|
|
375
|
+
BlockMath,
|
|
376
|
+
InlineMath,
|
|
204
377
|
Mathematics,
|
|
205
|
-
|
|
206
|
-
|
|
378
|
+
createMathMigrateTransaction,
|
|
379
|
+
mathMigrationRegex,
|
|
380
|
+
migrateMathStrings
|
|
207
381
|
});
|
|
208
382
|
//# sourceMappingURL=index.cjs.map
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts","../src/mathematics.ts","../src/MathematicsPlugin.ts"],"sourcesContent":["import { Mathematics } from './mathematics.js'\n\nexport * from './mathematics.js'\nexport * from './MathematicsPlugin.js'\nexport * from './types.js'\n\nexport default Mathematics\n","import { Extension } from '@tiptap/core'\nimport type { EditorState } from '@tiptap/pm/state'\n\nimport { MathematicsPlugin } from './MathematicsPlugin.js'\nimport type { MathematicsOptions } from './types.js'\n\nexport const defaultShouldRender = (state: EditorState, pos: number) => {\n const $pos = state.doc.resolve(pos)\n const isInCodeBlock = $pos.parent.type.name === 'codeBlock'\n\n return !isInCodeBlock\n}\n\nexport const Mathematics = Extension.create<MathematicsOptions>({\n name: 'Mathematics',\n\n addOptions() {\n return {\n // eslint-disable-next-line no-useless-escape\n regex: /\\$([^\\$]*)\\$/gi,\n katexOptions: undefined,\n shouldRender: defaultShouldRender,\n }\n },\n\n addProseMirrorPlugins() {\n return [MathematicsPlugin({ ...this.options, editor: this.editor })]\n },\n})\n\nexport default Mathematics\n","import { getChangedRanges } from '@tiptap/core'\nimport type { EditorState, Transaction } from '@tiptap/pm/state'\nimport { Plugin, PluginKey } from '@tiptap/pm/state'\nimport { Decoration, DecorationSet } from '@tiptap/pm/view'\nimport katex from 'katex'\n\nimport type { MathematicsOptionsWithEditor } from './types.js'\n\ntype DecoSpec = {\n isEditable: boolean\n isEditing: boolean\n katexOptions: MathematicsOptionsWithEditor['katexOptions']\n content: string\n}\n\ntype Deco = Omit<Decoration, 'spec'> & { spec: DecoSpec }\n\ntype PluginState =\n | { decorations: DecorationSet; isEditable: boolean }\n | { decorations: undefined; isEditable: undefined }\n\n/**\n * Get the range of positions that have been affected by a transaction\n */\nfunction getAffectedRange(\n newState: EditorState,\n previousPluginState: PluginState,\n isEditable: boolean,\n tr: Transaction,\n state: EditorState,\n) {\n const docSize = newState.doc.nodeSize - 2\n let minFrom = 0\n let maxTo = docSize\n\n if (previousPluginState.isEditable !== isEditable) {\n // When the editable state changes, run on all nodes just to be safe\n minFrom = 0\n maxTo = docSize\n } else if (tr.docChanged) {\n // When the document changes, only run on the nodes that have changed\n minFrom = docSize\n maxTo = 0\n\n getChangedRanges(tr).forEach(range => {\n // Purposefully over scan the range to ensure we catch all decorations\n minFrom = Math.min(minFrom, range.newRange.from - 1, range.oldRange.from - 1)\n maxTo = Math.max(maxTo, range.newRange.to + 1, range.oldRange.to + 1)\n })\n } else if (tr.selectionSet) {\n const { $from, $to } = state.selection\n const { $from: $newFrom, $to: $newTo } = newState.selection\n\n // When the selection changes, run on all the nodes between the old and new selection\n minFrom = Math.min(\n // Purposefully over scan the range to ensure we catch all decorations\n $from.depth === 0 ? 0 : $from.before(),\n $newFrom.depth === 0 ? 0 : $newFrom.before(),\n )\n maxTo = Math.max($to.depth === 0 ? maxTo : $to.after(), $newTo.depth === 0 ? maxTo : $newTo.after())\n }\n\n return {\n minFrom: Math.max(minFrom, 0),\n maxTo: Math.min(maxTo, docSize),\n }\n}\n\nexport const MathematicsPlugin = (options: MathematicsOptionsWithEditor) => {\n const { regex, katexOptions = {}, editor, shouldRender } = options\n\n return new Plugin<PluginState>({\n key: new PluginKey('mathematics'),\n\n state: {\n init() {\n return { decorations: undefined, isEditable: undefined }\n },\n apply(tr, previousPluginState, state, newState) {\n if (!tr.docChanged && !tr.selectionSet && previousPluginState.decorations) {\n // Just reuse the existing decorations, since nothing should have changed\n return previousPluginState\n }\n\n const nextDecorationSet = (previousPluginState.decorations || DecorationSet.empty).map(tr.mapping, tr.doc)\n const { selection } = newState\n const isEditable = editor.isEditable\n const decorationsToAdd = [] as Deco[]\n const { minFrom, maxTo } = getAffectedRange(newState, previousPluginState, isEditable, tr, state)\n\n newState.doc.nodesBetween(minFrom, maxTo, (node, pos) => {\n const enabled = shouldRender(newState, pos, node)\n\n if (node.isText && node.text && enabled) {\n let match: RegExpExecArray | null\n\n // eslint-disable-next-line no-cond-assign\n while ((match = regex.exec(node.text))) {\n const from = pos + match.index\n const to = from + match[0].length\n const content = match.slice(1).find(Boolean)\n\n if (content) {\n const selectionSize = selection.from - selection.to\n const anchorIsInside = selection.anchor >= from && selection.anchor <= to\n const rangeIsInside = selection.from >= from && selection.to <= to\n const isEditing = (selectionSize === 0 && anchorIsInside) || rangeIsInside\n\n if (\n // Are the decorations already present?\n nextDecorationSet.find(\n from,\n to,\n (deco: DecoSpec) =>\n isEditing === deco.isEditing &&\n content === deco.content &&\n isEditable === deco.isEditable &&\n katexOptions === deco.katexOptions,\n ).length\n ) {\n // Decoration exists in set, no need to add it again\n continue\n }\n // Use an inline decoration to either hide original (preview is showing) or show it (editing \"mode\")\n decorationsToAdd.push(\n Decoration.inline(\n from,\n to,\n {\n class:\n isEditing && isEditable\n ? 'Tiptap-mathematics-editor'\n : 'Tiptap-mathematics-editor Tiptap-mathematics-editor--hidden',\n style:\n !isEditing || !isEditable\n ? 'display: inline-block; height: 0; opacity: 0; overflow: hidden; position: absolute; width: 0;'\n : undefined,\n },\n {\n content,\n isEditable,\n isEditing,\n katexOptions,\n } satisfies DecoSpec,\n ),\n )\n\n if (!isEditable || !isEditing) {\n // Create decoration widget and add KaTeX preview if selection is not within the math-editor\n decorationsToAdd.push(\n Decoration.widget(\n from,\n () => {\n const element = document.createElement('span')\n\n // TODO: changeable class names\n element.classList.add('Tiptap-mathematics-render')\n\n if (isEditable) {\n element.classList.add('Tiptap-mathematics-render--editable')\n }\n\n try {\n katex.render(content!, element, katexOptions)\n } catch {\n element.innerHTML = content!\n }\n\n return element\n },\n {\n content,\n isEditable,\n isEditing,\n katexOptions,\n } satisfies DecoSpec,\n ),\n )\n }\n }\n }\n }\n })\n\n // Remove any decorations that exist at the same position, they will be replaced by the new decorations\n const decorationsToRemove = decorationsToAdd.flatMap(deco => nextDecorationSet.find(deco.from, deco.to))\n\n return {\n decorations: nextDecorationSet\n // Remove existing decorations that are going to be replaced\n .remove(decorationsToRemove)\n // Add any new decorations\n .add(tr.doc, decorationsToAdd),\n isEditable,\n }\n },\n },\n\n props: {\n decorations(state) {\n return this.getState(state)?.decorations ?? DecorationSet.empty\n },\n },\n })\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,IAAAA,eAA0B;;;ACA1B,kBAAiC;AAEjC,mBAAkC;AAClC,kBAA0C;AAC1C,mBAAkB;AAoBlB,SAAS,iBACP,UACA,qBACA,YACA,IACA,OACA;AACA,QAAM,UAAU,SAAS,IAAI,WAAW;AACxC,MAAI,UAAU;AACd,MAAI,QAAQ;AAEZ,MAAI,oBA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AEA,wBAAwB;AACtB,WAAO,CAAC,kBAAkB,EAAE,GAAG,KAAK,SAAS,QAAQ,KAAK,OAAO,CAAC,CAAC;AAAA,EACrE;AACF,CAAC;;;ADtBD,IAAO,gBAAQ;","names":["import_core","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 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;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,SA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