@lofcz/platejs-math 52.3.6 → 53.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -17,6 +17,20 @@ declare const BaseInlineEquationPlugin: platejs3.SlatePlugin<platejs3.PluginConf
17
17
  };
18
18
  }, {}>>;
19
19
  //#endregion
20
+ //#region src/lib/MathRules.d.ts
21
+ declare const MathRules: {
22
+ markdown: (options: {
23
+ variant: "$";
24
+ enabled?: ((context: platejs3.SelectionInputRuleContext<SlateEditor> | platejs3.InsertTextInputRuleContext<SlateEditor> | platejs3.InsertBreakInputRuleContext<SlateEditor> | platejs3.InsertDataInputRuleContext<SlateEditor>) => boolean) | undefined;
25
+ priority?: number | undefined;
26
+ } | {
27
+ on: "break" | "match";
28
+ variant: "$$";
29
+ enabled?: ((context: platejs3.SelectionInputRuleContext<SlateEditor> | platejs3.InsertTextInputRuleContext<SlateEditor> | platejs3.InsertBreakInputRuleContext<SlateEditor> | platejs3.InsertDataInputRuleContext<SlateEditor>) => boolean) | undefined;
30
+ priority?: number | undefined;
31
+ }) => platejs3.AnyInputRule<unknown>;
32
+ };
33
+ //#endregion
20
34
  //#region src/lib/transforms/insertEquation.d.ts
21
35
  declare const insertEquation: (editor: SlateEditor, options?: InsertNodesOptions) => void;
22
36
  //#endregion
@@ -32,4 +46,4 @@ declare const getEquationHtml: ({
32
46
  options?: KatexOptions;
33
47
  }) => string;
34
48
  //#endregion
35
- export { BaseEquationPlugin, BaseInlineEquationPlugin, getEquationHtml, insertEquation, insertInlineEquation };
49
+ export { BaseEquationPlugin, BaseInlineEquationPlugin, MathRules, getEquationHtml, insertEquation, insertInlineEquation };
package/dist/index.js CHANGED
@@ -1,8 +1,65 @@
1
1
  import { i as insertEquation, n as BaseEquationPlugin, r as insertInlineEquation, t as BaseInlineEquationPlugin } from "./BaseInlineEquationPlugin-1MXNHk1G.js";
2
+ import { KEYS, createRuleFactory, matchDelimitedInline } from "platejs";
2
3
  import katex from "katex";
3
4
 
5
+ //#region src/lib/MathRules.ts
6
+ const INLINE_BOUNDARY_RE = /[\s([{'"`]/;
7
+ const INLINE_FOLLOW_RE = /[\s)\]}:;,.!?'"`]/;
8
+ const isEquationInputBlocked = (editor) => editor.api.some({ match: { type: [
9
+ editor.getType(KEYS.codeBlock),
10
+ editor.getType(KEYS.equation),
11
+ editor.getType(KEYS.inlineEquation)
12
+ ] } });
13
+ const getInlineEquationMatch = (context) => {
14
+ const match = matchDelimitedInline(context, {
15
+ boundaryRe: INLINE_BOUNDARY_RE,
16
+ followRe: INLINE_FOLLOW_RE,
17
+ open: "$",
18
+ requireClosingDelimiter: false,
19
+ trim: "reject"
20
+ });
21
+ if (!match) return;
22
+ return {
23
+ deleteRange: match.deleteRange,
24
+ texExpression: match.content
25
+ };
26
+ };
27
+ const MathRules = { markdown: createRuleFactory((options) => options.variant === "$$" ? {
28
+ type: "blockFence",
29
+ fence: "$$",
30
+ block: KEYS.p,
31
+ on: options.on,
32
+ enabled: ({ editor }) => !isEquationInputBlocked(editor),
33
+ priority: 100,
34
+ apply: ({ editor }, match) => {
35
+ const blockMatch = match;
36
+ editor.tf.removeNodes({ at: blockMatch.path });
37
+ insertEquation(editor, {
38
+ at: blockMatch.path,
39
+ select: true
40
+ });
41
+ return true;
42
+ }
43
+ } : {
44
+ type: "insertText",
45
+ enabled: ({ editor }) => !isEquationInputBlocked(editor),
46
+ trigger: "$",
47
+ resolve: (context) => {
48
+ if (context.text !== "$" || context.options?.at) return;
49
+ return getInlineEquationMatch(context);
50
+ },
51
+ apply: ({ editor }, match) => {
52
+ const inlineMatch = match;
53
+ editor.tf.delete({ at: inlineMatch.deleteRange });
54
+ editor.tf.select(inlineMatch.deleteRange.anchor);
55
+ insertInlineEquation(editor, inlineMatch.texExpression);
56
+ return true;
57
+ }
58
+ }) };
59
+
60
+ //#endregion
4
61
  //#region src/lib/utils/getEquationHtml.ts
5
62
  const getEquationHtml = ({ element, options }) => katex.renderToString(element.texExpression, options);
6
63
 
7
64
  //#endregion
8
- export { BaseEquationPlugin, BaseInlineEquationPlugin, getEquationHtml, insertEquation, insertInlineEquation };
65
+ export { BaseEquationPlugin, BaseInlineEquationPlugin, MathRules, getEquationHtml, insertEquation, insertInlineEquation };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lofcz/platejs-math",
3
- "version": "52.3.6",
3
+ "version": "53.0.0",
4
4
  "description": "Math plugin for Plate",
5
5
  "keywords": [
6
6
  "plate",
@@ -35,10 +35,10 @@
35
35
  "devDependencies": {
36
36
  "@types/katex": "0.16.7",
37
37
  "@plate/scripts": "1.0.0",
38
- "platejs": "npm:@lofcz/platejs@52.3.6"
38
+ "platejs": "npm:@lofcz/platejs@53.0.0"
39
39
  },
40
40
  "peerDependencies": {
41
- "platejs": ">=52.0.11",
41
+ "platejs": ">=53.0.0",
42
42
  "react": ">=18.0.0",
43
43
  "react-dom": ">=18.0.0"
44
44
  },