@tiptap/extension-mathematics 2.24.2 → 3.0.0-beta.11

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/LICENSE.md ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025, Tiptap GmbH
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/dist/index.cjs CHANGED
@@ -1,177 +1,208 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- var core = require('@tiptap/core');
6
- var state = require('@tiptap/pm/state');
7
- var view = require('@tiptap/pm/view');
8
- var katex = require('katex');
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
9
29
 
10
- function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e : { default: e }; }
30
+ // src/index.ts
31
+ var index_exports = {};
32
+ __export(index_exports, {
33
+ Mathematics: () => Mathematics,
34
+ MathematicsPlugin: () => MathematicsPlugin,
35
+ default: () => index_default,
36
+ defaultShouldRender: () => defaultShouldRender
37
+ });
38
+ module.exports = __toCommonJS(index_exports);
11
39
 
12
- var katex__default = /*#__PURE__*/_interopDefaultCompat(katex);
40
+ // src/mathematics.ts
41
+ var import_core2 = require("@tiptap/core");
13
42
 
14
- /**
15
- * Get the range of positions that have been affected by a transaction
16
- */
43
+ // src/MathematicsPlugin.ts
44
+ var import_core = require("@tiptap/core");
45
+ var import_state = require("@tiptap/pm/state");
46
+ var import_view = require("@tiptap/pm/view");
47
+ var import_katex = __toESM(require("katex"), 1);
17
48
  function getAffectedRange(newState, previousPluginState, isEditable, tr, state) {
18
- const docSize = newState.doc.nodeSize - 2;
19
- let minFrom = 0;
20
- let maxTo = docSize;
21
- if (previousPluginState.isEditable !== isEditable) {
22
- // When the editable state changes, run on all nodes just to be safe
23
- minFrom = 0;
24
- maxTo = docSize;
25
- }
26
- else if (tr.docChanged) {
27
- // When the document changes, only run on the nodes that have changed
28
- minFrom = docSize;
29
- maxTo = 0;
30
- core.getChangedRanges(tr).forEach(range => {
31
- // Purposefully over scan the range to ensure we catch all decorations
32
- minFrom = Math.min(minFrom, range.newRange.from - 1, range.oldRange.from - 1);
33
- maxTo = Math.max(maxTo, range.newRange.to + 1, range.oldRange.to + 1);
34
- });
35
- }
36
- else if (tr.selectionSet) {
37
- const { $from, $to } = state.selection;
38
- const { $from: $newFrom, $to: $newTo } = newState.selection;
39
- // When the selection changes, run on all the nodes between the old and new selection
40
- minFrom = Math.min(
41
- // Purposefully over scan the range to ensure we catch all decorations
42
- $from.depth === 0 ? 0 : $from.before(), $newFrom.depth === 0 ? 0 : $newFrom.before());
43
- maxTo = Math.max($to.depth === 0 ? maxTo : $to.after(), $newTo.depth === 0 ? maxTo : $newTo.after());
44
- }
45
- return {
46
- minFrom: Math.max(minFrom, 0),
47
- maxTo: Math.min(maxTo, docSize),
48
- };
49
+ const docSize = newState.doc.nodeSize - 2;
50
+ let minFrom = 0;
51
+ let maxTo = docSize;
52
+ if (previousPluginState.isEditable !== isEditable) {
53
+ minFrom = 0;
54
+ maxTo = docSize;
55
+ } else if (tr.docChanged) {
56
+ minFrom = docSize;
57
+ maxTo = 0;
58
+ (0, import_core.getChangedRanges)(tr).forEach((range) => {
59
+ minFrom = Math.min(minFrom, range.newRange.from - 1, range.oldRange.from - 1);
60
+ maxTo = Math.max(maxTo, range.newRange.to + 1, range.oldRange.to + 1);
61
+ });
62
+ } else if (tr.selectionSet) {
63
+ const { $from, $to } = state.selection;
64
+ const { $from: $newFrom, $to: $newTo } = newState.selection;
65
+ minFrom = Math.min(
66
+ // Purposefully over scan the range to ensure we catch all decorations
67
+ $from.depth === 0 ? 0 : $from.before(),
68
+ $newFrom.depth === 0 ? 0 : $newFrom.before()
69
+ );
70
+ maxTo = Math.max($to.depth === 0 ? maxTo : $to.after(), $newTo.depth === 0 ? maxTo : $newTo.after());
71
+ }
72
+ return {
73
+ minFrom: Math.max(minFrom, 0),
74
+ maxTo: Math.min(maxTo, docSize)
75
+ };
49
76
  }
50
- const MathematicsPlugin = (options) => {
51
- const { regex, katexOptions = {}, editor, shouldRender, } = options;
52
- return new state.Plugin({
53
- key: new state.PluginKey('mathematics'),
54
- state: {
55
- init() {
56
- return { decorations: undefined, isEditable: undefined };
57
- },
58
- apply(tr, previousPluginState, state, newState) {
59
- if (!tr.docChanged && !tr.selectionSet && previousPluginState.decorations) {
60
- // Just reuse the existing decorations, since nothing should have changed
61
- return previousPluginState;
77
+ var MathematicsPlugin = (options) => {
78
+ const { regex, katexOptions = {}, editor, shouldRender } = options;
79
+ return new import_state.Plugin({
80
+ key: new import_state.PluginKey("mathematics"),
81
+ state: {
82
+ init() {
83
+ return { decorations: void 0, isEditable: void 0 };
84
+ },
85
+ apply(tr, previousPluginState, state, newState) {
86
+ if (!tr.docChanged && !tr.selectionSet && previousPluginState.decorations) {
87
+ return previousPluginState;
88
+ }
89
+ const nextDecorationSet = (previousPluginState.decorations || import_view.DecorationSet.empty).map(tr.mapping, tr.doc);
90
+ const { selection } = newState;
91
+ const isEditable = editor.isEditable;
92
+ const decorationsToAdd = [];
93
+ const { minFrom, maxTo } = getAffectedRange(newState, previousPluginState, isEditable, tr, state);
94
+ newState.doc.nodesBetween(minFrom, maxTo, (node, pos) => {
95
+ const enabled = shouldRender(newState, pos, node);
96
+ if (node.isText && node.text && enabled) {
97
+ let match;
98
+ while (match = regex.exec(node.text)) {
99
+ const from = pos + match.index;
100
+ const to = from + match[0].length;
101
+ const content = match.slice(1).find(Boolean);
102
+ if (content) {
103
+ const selectionSize = selection.from - selection.to;
104
+ const anchorIsInside = selection.anchor >= from && selection.anchor <= to;
105
+ const rangeIsInside = selection.from >= from && selection.to <= to;
106
+ const isEditing = selectionSize === 0 && anchorIsInside || rangeIsInside;
107
+ if (
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;
62
116
  }
63
- const nextDecorationSet = (previousPluginState.decorations || view.DecorationSet.empty).map(tr.mapping, tr.doc);
64
- const { selection } = newState;
65
- const isEditable = editor.isEditable;
66
- const decorationsToAdd = [];
67
- const { minFrom, maxTo } = getAffectedRange(newState, previousPluginState, isEditable, tr, state);
68
- newState.doc.nodesBetween(minFrom, maxTo, (node, pos) => {
69
- const enabled = shouldRender(newState, pos, node);
70
- if (node.isText && node.text && enabled) {
71
- let match;
72
- // eslint-disable-next-line no-cond-assign
73
- while ((match = regex.exec(node.text))) {
74
- const from = pos + match.index;
75
- const to = from + match[0].length;
76
- const content = match.slice(1).find(Boolean);
77
- if (content) {
78
- const selectionSize = selection.from - selection.to;
79
- const anchorIsInside = selection.anchor >= from && selection.anchor <= to;
80
- const rangeIsInside = selection.from >= from && selection.to <= to;
81
- const isEditing = (selectionSize === 0 && anchorIsInside) || rangeIsInside;
82
- if (
83
- // Are the decorations already present?
84
- nextDecorationSet.find(from, to, (deco) => isEditing === deco.isEditing
85
- && content === deco.content
86
- && isEditable === deco.isEditable
87
- && katexOptions === deco.katexOptions).length) {
88
- // Decoration exists in set, no need to add it again
89
- continue;
90
- }
91
- // Use an inline decoration to either hide original (preview is showing) or show it (editing "mode")
92
- decorationsToAdd.push(view.Decoration.inline(from, to, {
93
- class: isEditing && isEditable
94
- ? 'Tiptap-mathematics-editor'
95
- : 'Tiptap-mathematics-editor Tiptap-mathematics-editor--hidden',
96
- style: !isEditing || !isEditable
97
- ? 'display: inline-block; height: 0; opacity: 0; overflow: hidden; position: absolute; width: 0;'
98
- : undefined,
99
- }, {
100
- content,
101
- isEditable,
102
- isEditing,
103
- katexOptions,
104
- }));
105
- if (!isEditable || !isEditing) {
106
- // Create decoration widget and add KaTeX preview if selection is not within the math-editor
107
- decorationsToAdd.push(view.Decoration.widget(from, () => {
108
- const element = document.createElement('span');
109
- // TODO: changeable class names
110
- element.classList.add('Tiptap-mathematics-render');
111
- if (isEditable) {
112
- element.classList.add('Tiptap-mathematics-render--editable');
113
- }
114
- try {
115
- katex__default.default.render(content, element, katexOptions);
116
- }
117
- catch {
118
- element.innerHTML = content;
119
- }
120
- return element;
121
- }, {
122
- content,
123
- isEditable,
124
- isEditing,
125
- katexOptions,
126
- }));
127
- }
128
- }
129
- }
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
130
  }
131
- });
132
- // Remove any decorations that exist at the same position, they will be replaced by the new decorations
133
- const decorationsToRemove = decorationsToAdd.flatMap(deco => nextDecorationSet.find(deco.from, deco.to));
134
- return {
135
- decorations: nextDecorationSet
136
- // Remove existing decorations that are going to be replaced
137
- .remove(decorationsToRemove)
138
- // Add any new decorations
139
- .add(tr.doc, decorationsToAdd),
140
- isEditable,
141
- };
142
- },
143
- },
144
- props: {
145
- decorations(state) {
146
- var _a, _b;
147
- return (_b = (_a = this.getState(state)) === null || _a === void 0 ? void 0 : _a.decorations) !== null && _b !== void 0 ? _b : view.DecorationSet.empty;
148
- },
149
- },
150
- });
151
- };
152
-
153
- const defaultShouldRender = (state, pos) => {
154
- const $pos = state.doc.resolve(pos);
155
- const isInCodeBlock = $pos.parent.type.name === 'codeBlock';
156
- return !isInCodeBlock;
157
- };
158
- const Mathematics = core.Extension.create({
159
- name: 'Mathematics',
160
- addOptions() {
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
+ }
162
+ });
163
+ const decorationsToRemove = decorationsToAdd.flatMap((deco) => nextDecorationSet.find(deco.from, deco.to));
161
164
  return {
162
- // eslint-disable-next-line no-useless-escape
163
- regex: /\$([^\$]*)\$/gi,
164
- katexOptions: undefined,
165
- shouldRender: defaultShouldRender,
165
+ decorations: nextDecorationSet.remove(decorationsToRemove).add(tr.doc, decorationsToAdd),
166
+ isEditable
166
167
  };
168
+ }
167
169
  },
168
- addProseMirrorPlugins() {
169
- return [MathematicsPlugin({ ...this.options, editor: this.editor })];
170
- },
170
+ props: {
171
+ decorations(state) {
172
+ var _a, _b;
173
+ return (_b = (_a = this.getState(state)) == null ? void 0 : _a.decorations) != null ? _b : import_view.DecorationSet.empty;
174
+ }
175
+ }
176
+ });
177
+ };
178
+
179
+ // src/mathematics.ts
180
+ var defaultShouldRender = (state, pos) => {
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({
186
+ name: "Mathematics",
187
+ addOptions() {
188
+ return {
189
+ // eslint-disable-next-line no-useless-escape
190
+ regex: /\$([^\$]*)\$/gi,
191
+ katexOptions: void 0,
192
+ shouldRender: defaultShouldRender
193
+ };
194
+ },
195
+ addProseMirrorPlugins() {
196
+ return [MathematicsPlugin({ ...this.options, editor: this.editor })];
197
+ }
171
198
  });
172
199
 
173
- exports.Mathematics = Mathematics;
174
- exports.MathematicsPlugin = MathematicsPlugin;
175
- exports.default = Mathematics;
176
- exports.defaultShouldRender = defaultShouldRender;
177
- //# sourceMappingURL=index.cjs.map
200
+ // src/index.ts
201
+ var index_default = Mathematics;
202
+ // Annotate the CommonJS export names for ESM import in node:
203
+ 0 && (module.exports = {
204
+ Mathematics,
205
+ MathematicsPlugin,
206
+ defaultShouldRender
207
+ });
208
+ //# sourceMappingURL=index.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.cjs","sources":["../src/MathematicsPlugin.ts","../src/mathematics.ts"],"sourcesContent":["import { getChangedRanges } from '@tiptap/core'\nimport {\n EditorState, Plugin, PluginKey, Transaction,\n} from '@tiptap/pm/state'\nimport { Decoration, DecorationSet } from '@tiptap/pm/view'\nimport katex from 'katex'\n\nimport { 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(newState: EditorState, previousPluginState: PluginState, isEditable: boolean, tr: Transaction, state: EditorState) {\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(\n $to.depth === 0 ? maxTo : $to.after(),\n $newTo.depth === 0 ? maxTo : $newTo.after(),\n )\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 {\n regex, katexOptions = {}, editor, shouldRender,\n } = 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\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(\n tr.mapping,\n tr.doc,\n )\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) => 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","import { Extension } from '@tiptap/core'\nimport { EditorState } from '@tiptap/pm/state'\n\nimport { MathematicsPlugin } from './MathematicsPlugin.js'\nimport { 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"],"names":["getChangedRanges","Plugin","PluginKey","DecorationSet","Decoration","katex","Extension"],"mappings":";;;;;;;;;;;;;AAsBA;;AAEG;AACH,SAAS,gBAAgB,CAAC,QAAqB,EAAE,mBAAgC,EAAE,UAAmB,EAAE,EAAe,EAAE,KAAkB,EAAA;IACzI,MAAM,OAAO,GAAG,QAAQ,CAAC,GAAG,CAAC,QAAQ,GAAG,CAAC;IACzC,IAAI,OAAO,GAAG,CAAC;IACf,IAAI,KAAK,GAAG,OAAO;AAEnB,IAAA,IAAI,mBAAmB,CAAC,UAAU,KAAK,UAAU,EAAE;;QAEjD,OAAO,GAAG,CAAC;QACX,KAAK,GAAG,OAAO;;AACV,SAAA,IAAI,EAAE,CAAC,UAAU,EAAE;;QAExB,OAAO,GAAG,OAAO;QACjB,KAAK,GAAG,CAAC;QAETA,qBAAgB,CAAC,EAAE,CAAC,CAAC,OAAO,CAAC,KAAK,IAAG;;YAEnC,OAAO,GAAG,IAAI,CAAC,GAAG,CAAC,OAAO,EAAE,KAAK,CAAC,QAAQ,CAAC,IAAI,GAAG,CAAC,EAAE,KAAK,CAAC,QAAQ,CAAC,IAAI,GAAG,CAAC,CAAC;YAC7E,KAAK,GAAG,IAAI,CAAC,GAAG,CAAC,KAAK,EAAE,KAAK,CAAC,QAAQ,CAAC,EAAE,GAAG,CAAC,EAAE,KAAK,CAAC,QAAQ,CAAC,EAAE,GAAG,CAAC,CAAC;AACvE,SAAC,CAAC;;AACG,SAAA,IAAI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AAA,KAAK,EAAE,gBAAgB;AACvB,YAAA,YAAY,EAAE,SAAS;AACvB,YAAA,YAAY,EAAE,mBAAmB;SAClC;KACF;IAED,qBAAqB,GAAA;AACnB,QAAA,OAAO,CAAC,iBAAiB,CAAC,EAAE,GAAG,IAAI,CAAC,OAAO,EAAE,MAAM,EAAE,IAAI,CAAC,MAAM,EAAE,CAAC,CAAC;KACrE;AACF,CAAA;;;;;;;"}
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,oBAAoB,eAAe,YAAY;AAEjD,cAAU;AACV,YAAQ;AAAA,EACV,WAAW,GAAG,YAAY;AAExB,cAAU;AACV,YAAQ;AAER,sCAAiB,EAAE,EAAE,QAAQ,WAAS;AAEpC,gBAAU,KAAK,IAAI,SAAS,MAAM,SAAS,OAAO,GAAG,MAAM,SAAS,OAAO,CAAC;AAC5E,cAAQ,KAAK,IAAI,OAAO,MAAM,SAAS,KAAK,GAAG,MAAM,SAAS,KAAK,CAAC;AAAA,IACtE,CAAC;AAAA,EACH,WAAW,GAAG,cAAc;AAC1B,UAAM,EAAE,OAAO,IAAI,IAAI,MAAM;AAC7B,UAAM,EAAE,OAAO,UAAU,KAAK,OAAO,IAAI,SAAS;AAGlD,cAAU,KAAK;AAAA;AAAA,MAEb,MAAM,UAAU,IAAI,IAAI,MAAM,OAAO;AAAA,MACrC,SAAS,UAAU,IAAI,IAAI,SAAS,OAAO;AAAA,IAC7C;AACA,YAAQ,KAAK,IAA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AAE,CAAC;AAEvG,eAAO;AAAA,UACL,aAAa,kBAEV,OAAO,mBAAmB,EAE1B,IAAI,GAAG,KAAK,gBAAgB;AAAA,UAC/B;AAAA,QACF;AAAA,MACF;AAAA,IACF;AAAA,IAEA,OAAO;AAAA,MACL,YAAY,OAAO;AAvMzB;AAwMQ,gBAAO,gBAAK,SAAS,KAAK,MAAnB,mBAAsB,gBAAtB,YAAqC,0BAAc;AAAA,MAC5D;AAAA,IACF;AAAA,EACF,CAAC;AACH;;;ADtMO,IAAM,sBAAsB,CAAC,OAAoB,QAAgB;AACtE,QAAM,OAAO,MAAM,IAAI,QAAQ,GAAG;AAClC,QAAM,gBAAgB,KAAK,OAAO,KAAK,SAAS;AAEhD,SAAO,CAAC;AACV;AAEO,IAAM,cAAc,uBAAU,OAA2B;AAAA,EAC9D,MAAM;AAAA,EAEN,aAAa;AACX,WAAO;AAAA;AAAA,MAEL,OAAO;AAAA,MACP,cAAc;AAAA,MACd,cAAc;AAAA,IAChB;AAAA,EACF;AAAA,EAEA,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"]}
@@ -0,0 +1,28 @@
1
+ import { Editor, Extension } from '@tiptap/core';
2
+ import { EditorState, Plugin } from '@tiptap/pm/state';
3
+ import { Node } from '@tiptap/pm/model';
4
+ import { KatexOptions } from 'katex';
5
+ import { DecorationSet } from '@tiptap/pm/view';
6
+
7
+ type MathematicsOptions = {
8
+ regex: RegExp;
9
+ katexOptions?: KatexOptions;
10
+ shouldRender: (state: EditorState, pos: number, node: Node) => boolean;
11
+ };
12
+ type MathematicsOptionsWithEditor = MathematicsOptions & {
13
+ editor: Editor;
14
+ };
15
+
16
+ declare const defaultShouldRender: (state: EditorState, pos: number) => boolean;
17
+ declare const Mathematics: Extension<MathematicsOptions, any>;
18
+
19
+ type PluginState = {
20
+ decorations: DecorationSet;
21
+ isEditable: boolean;
22
+ } | {
23
+ decorations: undefined;
24
+ isEditable: undefined;
25
+ };
26
+ declare const MathematicsPlugin: (options: MathematicsOptionsWithEditor) => Plugin<PluginState>;
27
+
28
+ export { Mathematics, type MathematicsOptions, type MathematicsOptionsWithEditor, MathematicsPlugin, Mathematics as default, defaultShouldRender };
package/dist/index.d.ts CHANGED
@@ -1,6 +1,28 @@
1
- import { Mathematics } from './mathematics.js';
2
- export * from './mathematics.js';
3
- export * from './MathematicsPlugin.js';
4
- export * from './types.js';
5
- export default Mathematics;
6
- //# sourceMappingURL=index.d.ts.map
1
+ import { Editor, Extension } from '@tiptap/core';
2
+ import { EditorState, Plugin } from '@tiptap/pm/state';
3
+ import { Node } from '@tiptap/pm/model';
4
+ import { KatexOptions } from 'katex';
5
+ import { DecorationSet } from '@tiptap/pm/view';
6
+
7
+ type MathematicsOptions = {
8
+ regex: RegExp;
9
+ katexOptions?: KatexOptions;
10
+ shouldRender: (state: EditorState, pos: number, node: Node) => boolean;
11
+ };
12
+ type MathematicsOptionsWithEditor = MathematicsOptions & {
13
+ editor: Editor;
14
+ };
15
+
16
+ declare const defaultShouldRender: (state: EditorState, pos: number) => boolean;
17
+ declare const Mathematics: Extension<MathematicsOptions, any>;
18
+
19
+ type PluginState = {
20
+ decorations: DecorationSet;
21
+ isEditable: boolean;
22
+ } | {
23
+ decorations: undefined;
24
+ isEditable: undefined;
25
+ };
26
+ declare const MathematicsPlugin: (options: MathematicsOptionsWithEditor) => Plugin<PluginState>;
27
+
28
+ export { Mathematics, type MathematicsOptions, type MathematicsOptionsWithEditor, MathematicsPlugin, Mathematics as default, defaultShouldRender };