@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.d.cts
CHANGED
|
@@ -1,28 +1,298 @@
|
|
|
1
|
-
import { Editor, Extension } from '@tiptap/core';
|
|
2
|
-
import { EditorState, Plugin } from '@tiptap/pm/state';
|
|
3
|
-
import { Node } from '@tiptap/pm/model';
|
|
1
|
+
import { Node as Node$1, Editor, Extension } from '@tiptap/core';
|
|
4
2
|
import { KatexOptions } from 'katex';
|
|
5
|
-
import {
|
|
3
|
+
import { Node } from '@tiptap/pm/model';
|
|
4
|
+
import { Transaction } from '@tiptap/pm/state';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Configuration options for the BlockMath extension.
|
|
8
|
+
*/
|
|
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
|
+
*/
|
|
34
|
+
onClick?: (node: Node, pos: number) => void;
|
|
35
|
+
};
|
|
36
|
+
declare module '@tiptap/core' {
|
|
37
|
+
interface Commands<ReturnType> {
|
|
38
|
+
insertBlockMath: {
|
|
39
|
+
/**
|
|
40
|
+
* Inserts a math block node with LaTeX string.
|
|
41
|
+
* @param options - Options for inserting block math.
|
|
42
|
+
* @returns ReturnType
|
|
43
|
+
*/
|
|
44
|
+
insertBlockMath: (options: {
|
|
45
|
+
latex: string;
|
|
46
|
+
pos?: number;
|
|
47
|
+
}) => ReturnType;
|
|
48
|
+
/**
|
|
49
|
+
* Deletes a block math node.
|
|
50
|
+
* @returns ReturnType
|
|
51
|
+
*/
|
|
52
|
+
deleteBlockMath: (options?: {
|
|
53
|
+
pos?: number;
|
|
54
|
+
}) => ReturnType;
|
|
55
|
+
/**
|
|
56
|
+
* Update block math node with optional LaTeX string.
|
|
57
|
+
* @param options - Options for updating block math.
|
|
58
|
+
* @returns ReturnType
|
|
59
|
+
*/
|
|
60
|
+
updateBlockMath: (options?: {
|
|
61
|
+
latex: string;
|
|
62
|
+
pos?: number;
|
|
63
|
+
}) => ReturnType;
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* BlockMath is a Tiptap extension for rendering block mathematical expressions using KaTeX.
|
|
69
|
+
* It allows users to insert LaTeX formatted math expressions block within text.
|
|
70
|
+
* It supports rendering, input rules for LaTeX syntax, and click handling for interaction.
|
|
71
|
+
*
|
|
72
|
+
* @example
|
|
73
|
+
* ```javascript
|
|
74
|
+
* import { BlockMath } from '@tiptap/extension-mathematics'
|
|
75
|
+
* import { Editor } from '@tiptap/core'
|
|
76
|
+
*
|
|
77
|
+
* const editor = new Editor({
|
|
78
|
+
* extensions: [
|
|
79
|
+
* BlockMath.configure({
|
|
80
|
+
* onClick: (node, pos) => {
|
|
81
|
+
* console.log('Block math clicked:', node.attrs.latex, 'at position:', pos)
|
|
82
|
+
* },
|
|
83
|
+
* }),
|
|
84
|
+
* ],
|
|
85
|
+
* })
|
|
86
|
+
*/
|
|
87
|
+
declare const BlockMath: Node$1<BlockMathOptions, any>;
|
|
6
88
|
|
|
89
|
+
/**
|
|
90
|
+
* Configuration options for the InlineMath extension.
|
|
91
|
+
*/
|
|
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
|
+
*/
|
|
122
|
+
onClick?: (node: Node, pos: number) => void;
|
|
123
|
+
};
|
|
124
|
+
declare module '@tiptap/core' {
|
|
125
|
+
interface Commands<ReturnType> {
|
|
126
|
+
inlineMath: {
|
|
127
|
+
/**
|
|
128
|
+
* Insert a inline math node with LaTeX string.
|
|
129
|
+
* @param options - Options for inserting inline math.
|
|
130
|
+
* @returns ReturnType
|
|
131
|
+
*/
|
|
132
|
+
insertInlineMath: (options: {
|
|
133
|
+
latex: string;
|
|
134
|
+
pos?: number;
|
|
135
|
+
}) => ReturnType;
|
|
136
|
+
/**
|
|
137
|
+
* Delete an inline math node.
|
|
138
|
+
* @returns ReturnType
|
|
139
|
+
*/
|
|
140
|
+
deleteInlineMath: (options?: {
|
|
141
|
+
pos?: number;
|
|
142
|
+
}) => ReturnType;
|
|
143
|
+
/**
|
|
144
|
+
* Update inline math node with optional LaTeX string.
|
|
145
|
+
* @param options - Options for updating inline math.
|
|
146
|
+
* @returns ReturnType
|
|
147
|
+
*/
|
|
148
|
+
updateInlineMath: (options?: {
|
|
149
|
+
latex?: string;
|
|
150
|
+
pos?: number;
|
|
151
|
+
}) => ReturnType;
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* InlineMath is a Tiptap extension for rendering inline mathematical expressions using KaTeX.
|
|
157
|
+
* It allows users to insert LaTeX formatted math expressions inline within text.
|
|
158
|
+
* It supports rendering, input rules for LaTeX syntax, and click handling for interaction.
|
|
159
|
+
*
|
|
160
|
+
* @example
|
|
161
|
+
* ```javascript
|
|
162
|
+
* import { InlineMath } from '@tiptap/extension-mathematics'
|
|
163
|
+
* import { Editor } from '@tiptap/core'
|
|
164
|
+
*
|
|
165
|
+
* const editor = new Editor({
|
|
166
|
+
* extensions: [
|
|
167
|
+
* InlineMath.configure({
|
|
168
|
+
* onClick: (node, pos) => {
|
|
169
|
+
* console.log('Inline math clicked:', node.attrs.latex, 'at position:', pos)
|
|
170
|
+
* },
|
|
171
|
+
* }),
|
|
172
|
+
* ],
|
|
173
|
+
* })
|
|
174
|
+
*/
|
|
175
|
+
declare const InlineMath: Node$1<InlineMathOptions, any>;
|
|
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
|
+
*/
|
|
7
181
|
type MathematicsOptions = {
|
|
8
|
-
|
|
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 */
|
|
9
187
|
katexOptions?: KatexOptions;
|
|
10
|
-
shouldRender: (state: EditorState, pos: number, node: Node) => boolean;
|
|
11
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
|
+
*/
|
|
12
194
|
type MathematicsOptionsWithEditor = MathematicsOptions & {
|
|
13
195
|
editor: Editor;
|
|
14
196
|
};
|
|
15
197
|
|
|
16
|
-
|
|
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
|
+
*/
|
|
17
244
|
declare const Mathematics: Extension<MathematicsOptions, any>;
|
|
18
245
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
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;
|
|
27
297
|
|
|
28
|
-
export { Mathematics, type MathematicsOptions, type MathematicsOptionsWithEditor,
|
|
298
|
+
export { BlockMath, type BlockMathOptions, InlineMath, type InlineMathOptions, Mathematics, type MathematicsOptions, type MathematicsOptionsWithEditor, createMathMigrateTransaction, Mathematics as default, mathMigrationRegex, migrateMathStrings };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,28 +1,298 @@
|
|
|
1
|
-
import { Editor, Extension } from '@tiptap/core';
|
|
2
|
-
import { EditorState, Plugin } from '@tiptap/pm/state';
|
|
3
|
-
import { Node } from '@tiptap/pm/model';
|
|
1
|
+
import { Node as Node$1, Editor, Extension } from '@tiptap/core';
|
|
4
2
|
import { KatexOptions } from 'katex';
|
|
5
|
-
import {
|
|
3
|
+
import { Node } from '@tiptap/pm/model';
|
|
4
|
+
import { Transaction } from '@tiptap/pm/state';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Configuration options for the BlockMath extension.
|
|
8
|
+
*/
|
|
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
|
+
*/
|
|
34
|
+
onClick?: (node: Node, pos: number) => void;
|
|
35
|
+
};
|
|
36
|
+
declare module '@tiptap/core' {
|
|
37
|
+
interface Commands<ReturnType> {
|
|
38
|
+
insertBlockMath: {
|
|
39
|
+
/**
|
|
40
|
+
* Inserts a math block node with LaTeX string.
|
|
41
|
+
* @param options - Options for inserting block math.
|
|
42
|
+
* @returns ReturnType
|
|
43
|
+
*/
|
|
44
|
+
insertBlockMath: (options: {
|
|
45
|
+
latex: string;
|
|
46
|
+
pos?: number;
|
|
47
|
+
}) => ReturnType;
|
|
48
|
+
/**
|
|
49
|
+
* Deletes a block math node.
|
|
50
|
+
* @returns ReturnType
|
|
51
|
+
*/
|
|
52
|
+
deleteBlockMath: (options?: {
|
|
53
|
+
pos?: number;
|
|
54
|
+
}) => ReturnType;
|
|
55
|
+
/**
|
|
56
|
+
* Update block math node with optional LaTeX string.
|
|
57
|
+
* @param options - Options for updating block math.
|
|
58
|
+
* @returns ReturnType
|
|
59
|
+
*/
|
|
60
|
+
updateBlockMath: (options?: {
|
|
61
|
+
latex: string;
|
|
62
|
+
pos?: number;
|
|
63
|
+
}) => ReturnType;
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* BlockMath is a Tiptap extension for rendering block mathematical expressions using KaTeX.
|
|
69
|
+
* It allows users to insert LaTeX formatted math expressions block within text.
|
|
70
|
+
* It supports rendering, input rules for LaTeX syntax, and click handling for interaction.
|
|
71
|
+
*
|
|
72
|
+
* @example
|
|
73
|
+
* ```javascript
|
|
74
|
+
* import { BlockMath } from '@tiptap/extension-mathematics'
|
|
75
|
+
* import { Editor } from '@tiptap/core'
|
|
76
|
+
*
|
|
77
|
+
* const editor = new Editor({
|
|
78
|
+
* extensions: [
|
|
79
|
+
* BlockMath.configure({
|
|
80
|
+
* onClick: (node, pos) => {
|
|
81
|
+
* console.log('Block math clicked:', node.attrs.latex, 'at position:', pos)
|
|
82
|
+
* },
|
|
83
|
+
* }),
|
|
84
|
+
* ],
|
|
85
|
+
* })
|
|
86
|
+
*/
|
|
87
|
+
declare const BlockMath: Node$1<BlockMathOptions, any>;
|
|
6
88
|
|
|
89
|
+
/**
|
|
90
|
+
* Configuration options for the InlineMath extension.
|
|
91
|
+
*/
|
|
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
|
+
*/
|
|
122
|
+
onClick?: (node: Node, pos: number) => void;
|
|
123
|
+
};
|
|
124
|
+
declare module '@tiptap/core' {
|
|
125
|
+
interface Commands<ReturnType> {
|
|
126
|
+
inlineMath: {
|
|
127
|
+
/**
|
|
128
|
+
* Insert a inline math node with LaTeX string.
|
|
129
|
+
* @param options - Options for inserting inline math.
|
|
130
|
+
* @returns ReturnType
|
|
131
|
+
*/
|
|
132
|
+
insertInlineMath: (options: {
|
|
133
|
+
latex: string;
|
|
134
|
+
pos?: number;
|
|
135
|
+
}) => ReturnType;
|
|
136
|
+
/**
|
|
137
|
+
* Delete an inline math node.
|
|
138
|
+
* @returns ReturnType
|
|
139
|
+
*/
|
|
140
|
+
deleteInlineMath: (options?: {
|
|
141
|
+
pos?: number;
|
|
142
|
+
}) => ReturnType;
|
|
143
|
+
/**
|
|
144
|
+
* Update inline math node with optional LaTeX string.
|
|
145
|
+
* @param options - Options for updating inline math.
|
|
146
|
+
* @returns ReturnType
|
|
147
|
+
*/
|
|
148
|
+
updateInlineMath: (options?: {
|
|
149
|
+
latex?: string;
|
|
150
|
+
pos?: number;
|
|
151
|
+
}) => ReturnType;
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* InlineMath is a Tiptap extension for rendering inline mathematical expressions using KaTeX.
|
|
157
|
+
* It allows users to insert LaTeX formatted math expressions inline within text.
|
|
158
|
+
* It supports rendering, input rules for LaTeX syntax, and click handling for interaction.
|
|
159
|
+
*
|
|
160
|
+
* @example
|
|
161
|
+
* ```javascript
|
|
162
|
+
* import { InlineMath } from '@tiptap/extension-mathematics'
|
|
163
|
+
* import { Editor } from '@tiptap/core'
|
|
164
|
+
*
|
|
165
|
+
* const editor = new Editor({
|
|
166
|
+
* extensions: [
|
|
167
|
+
* InlineMath.configure({
|
|
168
|
+
* onClick: (node, pos) => {
|
|
169
|
+
* console.log('Inline math clicked:', node.attrs.latex, 'at position:', pos)
|
|
170
|
+
* },
|
|
171
|
+
* }),
|
|
172
|
+
* ],
|
|
173
|
+
* })
|
|
174
|
+
*/
|
|
175
|
+
declare const InlineMath: Node$1<InlineMathOptions, any>;
|
|
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
|
+
*/
|
|
7
181
|
type MathematicsOptions = {
|
|
8
|
-
|
|
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 */
|
|
9
187
|
katexOptions?: KatexOptions;
|
|
10
|
-
shouldRender: (state: EditorState, pos: number, node: Node) => boolean;
|
|
11
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
|
+
*/
|
|
12
194
|
type MathematicsOptionsWithEditor = MathematicsOptions & {
|
|
13
195
|
editor: Editor;
|
|
14
196
|
};
|
|
15
197
|
|
|
16
|
-
|
|
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
|
+
*/
|
|
17
244
|
declare const Mathematics: Extension<MathematicsOptions, any>;
|
|
18
245
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
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;
|
|
27
297
|
|
|
28
|
-
export { Mathematics, type MathematicsOptions, type MathematicsOptionsWithEditor,
|
|
298
|
+
export { BlockMath, type BlockMathOptions, InlineMath, type InlineMathOptions, Mathematics, type MathematicsOptions, type MathematicsOptionsWithEditor, createMathMigrateTransaction, Mathematics as default, mathMigrationRegex, migrateMathStrings };
|