prosemirror-math 0.2.0 → 0.2.2

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2023 ocavue
3
+ Copyright (c) 2026 ocavue
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/README.md CHANGED
@@ -1,6 +1,8 @@
1
1
  # prosemirror-math
2
2
 
3
- Math editing extensions for [ProseMirror](https://prosemirror.net/). Provides node specs, node views, input rules, and plugins for inline and block math.
3
+ [![NPM version](https://img.shields.io/npm/v/prosemirror-math?color=a1b858&label=)](https://www.npmjs.com/package/prosemirror-math)
4
+
5
+ Rendering math expressions in [ProseMirror](https://prosemirror.net/).
4
6
 
5
7
  ## Installation
6
8
 
@@ -23,7 +25,12 @@ import { Schema } from 'prosemirror-model'
23
25
  const schema = new Schema({
24
26
  nodes: {
25
27
  doc: { content: 'block+' },
26
- paragraph: { content: 'inline*', group: 'block', parseDOM: [{ tag: 'p' }], toDOM: () => ['p', 0] },
28
+ paragraph: {
29
+ content: 'inline*',
30
+ group: 'block',
31
+ parseDOM: [{ tag: 'p' }],
32
+ toDOM: () => ['p', 0],
33
+ },
27
34
  text: { group: 'inline' },
28
35
  mathBlock: { ...mathBlockSpec },
29
36
  mathInline: { ...mathInlineSpec },
@@ -43,12 +50,22 @@ import Temml from 'temml'
43
50
  const view = new EditorView(document.body, {
44
51
  state,
45
52
  nodeViews: {
46
- mathBlock: (node, view, getPos, decorations) => createMathBlockView((text, element) => {
47
- Temml.render(text, element, { displayMode: true })
48
- }, node, decorations),
49
- mathInline: (node, view, getPos, decorations) => createMathInlineView((text, element) => {
50
- Temml.render(text, element, { displayMode: false })
51
- }, node, decorations),
53
+ mathBlock: (node, view, getPos, decorations) =>
54
+ createMathBlockView(
55
+ (text, element) => {
56
+ Temml.render(text, element, { displayMode: true })
57
+ },
58
+ node,
59
+ decorations,
60
+ ),
61
+ mathInline: (node, view, getPos, decorations) =>
62
+ createMathInlineView(
63
+ (text, element) => {
64
+ Temml.render(text, element, { displayMode: false })
65
+ },
66
+ node,
67
+ decorations,
68
+ ),
52
69
  },
53
70
  })
54
71
  ```
@@ -96,4 +96,5 @@ type RenderMathInline = (text: string, element: HTMLElement) => void;
96
96
  */
97
97
  declare function createMathInlineView(renderMathInline: RenderMathInline, node: Node, decorations: readonly Decoration[]): NodeView;
98
98
  //#endregion
99
- export { type RenderMathBlock, type RenderMathInline, createCursorInsidePlugin, createMathBlockView, createMathInlineInputRule, createMathInlineView, mathBlockEnterRule, mathBlockSpec, mathInlineSpec };
99
+ export { type RenderMathBlock, type RenderMathInline, createCursorInsidePlugin, createMathBlockView, createMathInlineInputRule, createMathInlineView, mathBlockEnterRule, mathBlockSpec, mathInlineSpec };
100
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","names":[],"sources":["../src/cursor-inside-plugin.ts","../src/math-block-enter-rule.ts","../src/math-block-spec.ts","../src/math-block-view.ts","../src/math-inline-input-rule.ts","../src/math-inline-spec.ts","../src/math-inline-view.ts"],"mappings":";;;;;;;;AA2CA;;;;;;;;iBAAgB,wBAAA,CAAA,GAA4B,MAAA;;;;;;;;AAA5C;cC9Ba,kBAAA,EAAoB,SAAA;;;;;;;;cCNpB,aAAA,EAAe,QAAA;;;;;;;;AFoC5B;KG/BY,eAAA,IAAmB,IAAA,UAAc,OAAA,EAAS,WAAA;;;;;;;AFCtD;;;;;;iBEagB,mBAAA,CACd,eAAA,EAAiB,eAAA,EACjB,IAAA,EAAM,IAAA,EACN,WAAA,WAAsB,UAAA,KACrB,QAAA;;;;;;;;AHaH;;;iBIfgB,yBAAA,CAA0B,QAAA,WAAmB,SAAA;;;;;;;;cCrBhD,cAAA,EAAgB,QAAA;;;;;;;;ALoC7B;KM/BY,gBAAA,IAAoB,IAAA,UAAc,OAAA,EAAS,WAAA;;;;;;;ALCvD;;;;;;iBKagB,oBAAA,CACd,gBAAA,EAAkB,gBAAA,EAClB,IAAA,EAAM,IAAA,EACN,WAAA,WAAsB,UAAA,KACrB,QAAA"}
@@ -252,4 +252,5 @@ function createMathInlineView(renderMathInline, node, decorations) {
252
252
  }
253
253
 
254
254
  //#endregion
255
- export { createCursorInsidePlugin, createMathBlockView, createMathInlineInputRule, createMathInlineView, mathBlockEnterRule, mathBlockSpec, mathInlineSpec };
255
+ export { createCursorInsidePlugin, createMathBlockView, createMathInlineInputRule, createMathInlineView, mathBlockEnterRule, mathBlockSpec, mathInlineSpec };
256
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","names":[],"sources":["../src/cursor-inside-plugin.ts","../src/math-block-enter-rule.ts","../src/math-block-spec.ts","../src/create-element.ts","../src/math-view-render.ts","../src/math-block-view.ts","../src/math-inline-input-rule.ts","../src/math-inline-spec.ts","../src/math-inline-view.ts"],"sourcesContent":["import { Plugin, PluginKey, type EditorState } from 'prosemirror-state'\nimport { Decoration, DecorationSet } from 'prosemirror-view'\n\nconst DECORATION_SPEC = 'MATH_CURSOR_INSIDE'\n\nfunction createCursorInsideDecoration(\n state: EditorState,\n): DecorationSet | undefined {\n const { $head } = state.selection\n const node = $head.parent\n\n if (!node.type.isInGroup('math')) return\n\n const before = $head.before()\n const deco = Decoration.node(\n before,\n before + node.nodeSize,\n { class: 'prosemirror-math-head-inside' },\n DECORATION_SPEC,\n )\n return DecorationSet.create(state.doc, [deco])\n}\n\n/**\n * @internal\n */\nexport function hasCursorInsideDecoration(\n decorations: readonly Decoration[],\n): boolean {\n return decorations.some((deco) => deco.spec === DECORATION_SPEC)\n}\n\ntype PluginState = DecorationSet | undefined\n\n/**\n * Creates a plugin that adds a `prosemirror-math-head-inside` CSS class to math\n * nodes when the text selection head is inside them. This is useful for styling\n * math nodes differently while they are being edited.\n *\n * The plugin automatically detects nodes in the `math` group.\n *\n * @public\n */\nexport function createCursorInsidePlugin(): Plugin {\n const key = new PluginKey<PluginState>('prosemirror-math-cursor-inside')\n return new Plugin<PluginState>({\n key,\n state: {\n init(): PluginState {\n return undefined\n },\n apply(tr, oldValue, oldState, newState): PluginState {\n if (\n oldState.selection.head === newState.selection.head &&\n !tr.docChanged\n ) {\n return oldValue\n }\n return createCursorInsideDecoration(newState)\n },\n },\n props: {\n decorations(state: EditorState): PluginState | undefined {\n return key.getState(state)\n },\n },\n })\n}\n","import {\n createTextBlockEnterRule,\n type EnterRule,\n} from 'prosemirror-enter-rules'\n\nexport const MATH_BLOCK_ENTER_REGEXP: RegExp = /^\\$\\$$/\n\n/**\n * An {@link EnterRule} that converts a textblock node that only contains `$$` into a math\n * block node when Enter is pressed.\n *\n * @public\n */\nexport const mathBlockEnterRule: EnterRule =\n /* @__PURE__ */ createTextBlockEnterRule({\n regex: MATH_BLOCK_ENTER_REGEXP,\n type: 'mathBlock',\n })\n","import type { NodeSpec } from 'prosemirror-model'\n\n/**\n * A {@link NodeSpec} for a block-level math node.\n *\n * @public\n */\nexport const mathBlockSpec: NodeSpec = {\n atom: false,\n group: 'block math',\n content: 'text*',\n code: true,\n toDOM() {\n return [\n 'div',\n {\n class: 'prosemirror-math-block',\n },\n ['pre', ['code', 0]],\n ]\n },\n parseDOM: [\n {\n tag: 'div.prosemirror-math-block',\n\n // skip the `<pre>` wrapper so that the node `codeBlock` won't match the content.\n // TODO: add test to verify it\n contentElement: 'code',\n },\n ],\n}\n","export function createElement<Tag extends keyof HTMLElementTagNameMap>(\n tag: Tag,\n className?: string,\n ...children: HTMLElement[]\n): HTMLElementTagNameMap[Tag] {\n const element: HTMLElementTagNameMap[Tag] = document.createElement(tag)\n if (className) {\n element.className = className\n }\n if (children.length > 0) {\n element.append(...children)\n }\n return element\n}\n","import type { Node } from 'prosemirror-model'\nimport type { Decoration } from 'prosemirror-view'\n\nimport { hasCursorInsideDecoration } from './cursor-inside-plugin.ts'\n\ntype RenderMath = (text: string, element: HTMLElement) => void\n\nexport function createMathViewRender(\n renderMath: RenderMath,\n source: HTMLElement,\n display: HTMLElement,\n inline: boolean,\n) {\n let prevNode: Node | undefined\n let prevText: string | undefined\n let prevSelected: boolean | undefined\n\n function updateDisplay(node: Node) {\n if (node === prevNode) return\n prevNode = node\n\n const text = node.textContent\n if (text === prevText) return\n prevText = text\n\n renderMath(text, display)\n }\n\n function updateStyle(decorations: readonly Decoration[]): void {\n const selected = hasCursorInsideDecoration(decorations)\n if (selected === prevSelected) return\n prevSelected = selected\n\n // When the math node is selected, show the source code.\n // Otherwise, show the rendered result.\n display.style.display = selected ? 'none' : ''\n if (!inline) {\n source.style.display = selected ? '' : 'none'\n } else {\n // For inline source code, we don't use `display: none` because we need\n // the source text rendered in the DOM to ensure the text cursor can be\n // placed correctly.\n Object.assign(\n source.style,\n selected ? visibleInlineSourceStyle : hiddenInlineSourceStyle,\n )\n }\n }\n\n return function updateMathView(\n node: Node,\n decorations: readonly Decoration[],\n ): void {\n updateDisplay(node)\n updateStyle(decorations)\n }\n}\n\nconst hiddenInlineSourceStyle: Partial<CSSStyleDeclaration> = {\n display: 'inline-flex',\n opacity: '0',\n pointerEvents: 'none',\n maxWidth: '0',\n maxHeight: '0',\n overflow: 'hidden',\n}\n\nconst visibleInlineSourceStyle: Partial<CSSStyleDeclaration> = {\n display: 'inline-flex',\n opacity: '1',\n pointerEvents: '',\n maxWidth: '',\n maxHeight: '',\n overflow: '',\n}\n","import type { Node as ProseMirrorNode } from 'prosemirror-model'\nimport type { Decoration, NodeView } from 'prosemirror-view'\n\nimport { createElement } from './create-element.ts'\nimport { createMathViewRender } from './math-view-render.ts'\n\n/**\n * The function to render a math block.\n *\n * @param text - The text of the math block. For example, a TeX expression.\n * @param element - A `<div>` element to render the math block.\n */\nexport type RenderMathBlock = (text: string, element: HTMLElement) => void\n\n/**\n * Creates a {@link NodeView} for a block-level math node. The view will show a\n * source editor or a rendered display area based on the text cursor position.\n *\n * @param renderMathBlock - A function that renders math text (e.g. TeX) into\n * the display element. You can use libraries like\n * [Temml](https://temml.org/) or [KaTeX](https://katex.org/).\n * @param node - The ProseMirror node to render.\n * @param decorations - The decorations applied to the node.\n *\n * @public\n */\nexport function createMathBlockView(\n renderMathBlock: RenderMathBlock,\n node: ProseMirrorNode,\n decorations: readonly Decoration[],\n): NodeView {\n const code = createElement('code')\n const source = createElement('pre', 'prosemirror-math-source', code)\n const display = createElement('div', 'prosemirror-math-display')\n const dom = createElement('div', 'prosemirror-math-block', source, display)\n\n const render = createMathViewRender(renderMathBlock, source, display, false)\n\n render(node, decorations)\n\n return {\n dom,\n contentDOM: code,\n update: (node, decorations) => {\n render(node, decorations)\n return true\n },\n }\n}\n","/* eslint-disable unicorn/prefer-string-raw -- Don't use String.raw here for better bundler minification */\n\nimport { supportsRegexLookbehind } from '@ocavue/utils'\nimport { InputRule } from 'prosemirror-inputrules'\n\n// Matches text wrapped in `$` or `$$`, e.g. `$x^2$` or `$$x^2$$`.\nexport const MATH_INPUT_REGEXP: string =\n // Don't allow $ before the opening delimiter\n (supportsRegexLookbehind() ? '(?<!\\\\$)' : '') +\n // capture group 1: opening delimiter (`$` or `$$`)\n '(\\\\$\\\\$?)' +\n // capture group 2: the math content\n // - a single non-whitespace, non-`$` character\n // - optionally followed by any non-`$` characters and a final non-whitespace, non-`$` character\n '([^\\\\s$](?:[^$]*[^\\\\s$])?)' +\n // backreference: closing delimiter must match the opening\n '\\\\1' +\n // end of input (required by ProseMirror InputRule)\n '$'\n\n/**\n * Creates a ProseMirror {@link InputRule} that converts text wrapped in `$` or\n * `$$` (e.g. `$x^2$`) into an inline math node.\n *\n * @param nodeType - The name of the inline math node type in your schema.\n *\n * @public\n */\nexport function createMathInlineInputRule(nodeType: string): InputRule {\n return new InputRule(\n new RegExp(MATH_INPUT_REGEXP),\n (state, match, start, end) => {\n const { tr, schema } = state\n const mathText = match[2]\n if (!mathText) return null\n\n const type = schema.nodes[nodeType]\n if (!type) return null\n\n const node = type.create(null, schema.text(mathText))\n tr.replaceWith(start, end, node)\n return tr\n },\n )\n}\n","import type { NodeSpec } from 'prosemirror-model'\n\n/**\n * A {@link NodeSpec} for an inline math node.\n *\n * @public\n */\nexport const mathInlineSpec: NodeSpec = {\n atom: false,\n inline: true,\n group: 'inline math',\n content: 'text*',\n selectable: false,\n code: true,\n toDOM() {\n return [\n 'span',\n {\n class: 'prosemirror-math-inline',\n },\n ['code', 0],\n ]\n },\n parseDOM: [\n {\n tag: 'span.prosemirror-math-inline',\n contentElement: 'code',\n },\n ],\n}\n","import type { Node as ProseMirrorNode } from 'prosemirror-model'\nimport type { Decoration, NodeView } from 'prosemirror-view'\n\nimport { createElement } from './create-element.ts'\nimport { createMathViewRender } from './math-view-render.ts'\n\n/**\n * The function to render a math inline.\n *\n * @param text - The text of the math inline. For example, a TeX expression.\n * @param element - A `<span>` element to render the math inline.\n */\nexport type RenderMathInline = (text: string, element: HTMLElement) => void\n\n/**\n * Creates a {@link NodeView} for an inline math node. The view will show a\n * source editor or a rendered display area based on the text cursor position.\n *\n * @param renderMathInline - A function that renders math text (e.g. TeX) into\n * the display element. You can use libraries like [Temml](https://temml.org/)\n * or [KaTeX](https://katex.org/).\n * @param node - The ProseMirror node to render.\n * @param decorations - The decorations applied to the node.\n *\n * @public\n */\nexport function createMathInlineView(\n renderMathInline: RenderMathInline,\n node: ProseMirrorNode,\n decorations: readonly Decoration[],\n): NodeView {\n const code = createElement('code')\n const source = createElement('span', 'prosemirror-math-source', code)\n const display = createElement('span', 'prosemirror-math-display')\n const dom = createElement('span', 'prosemirror-math-inline', source, display)\n\n const render = createMathViewRender(renderMathInline, source, display, true)\n\n render(node, decorations)\n\n return {\n dom,\n contentDOM: code,\n update: (node, decorations) => {\n render(node, decorations)\n return true\n },\n }\n}\n"],"mappings":";;;;;;;AAGA,MAAM,kBAAkB;AAExB,SAAS,6BACP,OAC2B;CAC3B,MAAM,EAAE,UAAU,MAAM;CACxB,MAAM,OAAO,MAAM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AM,EAAE,IAAI,WAAW;EACvB,MAAM,WAAW,MAAM;AACvB,MAAI,CAAC,SAAU,QAAO;EAEtB,MAAM,OAAO,OAAO,MAAM;AAC1B,MAAI,CAAC,KAAM,QAAO;EAElB,MAAM,OAAO,KAAK,OAAO,MAAM,OAAO,KAAK,SAAS,CAAC;AACrD,KAAG,YAAY,OAAO,KAAK,KAAK;AAChC,SAAO;GAEV;;;;;;;;;;ACpCH,MAAa,iBAA2B;CACtC,MAAM;CACN,QAAQ;CACR,OAAO;CACP,SAAS;CACT,YAAY;CACZ,MAAM;CACN,QAAQ;AACN,SAAO;GACL;GACA,EACE,OAAO,2BACR;GACD,CAAC,QAAQ,EAAE;GACZ;;CAEH,UAAU,CACR;EACE,KAAK;EACL,gBAAgB;EACjB,CACF;CACF;;;;;;;;;;;;;;;;ACHD,SAAgB,qBACd,kBACA,MACA,aACU;CACV,MAAM,OAAO,cAAc,OAAO;CAClC,MAAM,SAAS,cAAc,QAAQ,2BAA2B,KAAK;CACrE,MAAM,UAAU,cAAc,QAAQ,2BAA2B;CACjE,MAAM,MAAM,cAAc,QAAQ,2BAA2B,QAAQ,QAAQ;CAE7E,MAAM,SAAS,qBAAqB,kBAAkB,QAAQ,SAAS,KAAK;AAE5E,QAAO,MAAM,YAAY;AAEzB,QAAO;EACL;EACA,YAAY;EACZ,SAAS,MAAM,gBAAgB;AAC7B,UAAO,MAAM,YAAY;AACzB,UAAO;;EAEV"}
package/package.json CHANGED
@@ -1,34 +1,36 @@
1
1
  {
2
2
  "name": "prosemirror-math",
3
3
  "type": "module",
4
- "version": "0.2.0",
5
- "private": false,
6
- "description": "Math extensions for ProseMirror",
7
- "author": {
8
- "name": "ocavue",
9
- "email": "ocavue@gmail.com"
10
- },
4
+ "version": "0.2.2",
5
+ "description": "Rendering math expressions in ProseMirror",
6
+ "author": "ocavue <ocavue@gmail.com>",
11
7
  "license": "MIT",
12
8
  "funding": "https://github.com/sponsors/ocavue",
13
- "homepage": "https://github.com/prosekit/prosekit#readme",
9
+ "homepage": "https://github.com/ocavue/prosemirror-math#readme",
14
10
  "repository": {
15
11
  "type": "git",
16
- "url": "git+https://github.com/prosekit/prosekit.git",
17
- "directory": "packages/prosemirror-math"
18
- },
19
- "bugs": {
20
- "url": "https://github.com/prosekit/prosekit/issues"
12
+ "url": "https://github.com/ocavue/prosemirror-math.git"
21
13
  },
14
+ "bugs": "https://github.com/ocavue/prosemirror-math/issues",
22
15
  "keywords": [
23
16
  "ProseMirror"
24
17
  ],
25
18
  "sideEffects": false,
26
- "main": "./dist/prosemirror-math.js",
27
- "module": "./dist/prosemirror-math.js",
19
+ "main": "./dist/index.js",
20
+ "module": "./dist/index.js",
21
+ "types": "./dist/index.d.ts",
28
22
  "exports": {
29
23
  ".": {
30
- "types": "./dist/prosemirror-math.d.ts",
31
- "default": "./dist/prosemirror-math.js"
24
+ "types": "./dist/index.d.ts",
25
+ "import": "./dist/index.js"
26
+ }
27
+ },
28
+ "typesVersions": {
29
+ "*": {
30
+ "*": [
31
+ "./dist/*",
32
+ "./dist/index.d.ts"
33
+ ]
32
34
  }
33
35
  },
34
36
  "files": [
@@ -37,43 +39,39 @@
37
39
  ],
38
40
  "dependencies": {
39
41
  "@ocavue/utils": "^1.5.0",
40
- "prosemirror-inputrules": "^1.5.1",
41
- "prosemirror-model": "^1.25.4",
42
- "prosemirror-state": "^1.4.4",
43
- "prosemirror-view": "^1.41.6",
44
- "prosemirror-enter-rules": "^0.1.3"
42
+ "prosemirror-enter-rules": "^0.1.5",
43
+ "prosemirror-inputrules": "^1.0.0",
44
+ "prosemirror-model": "^1.0.0",
45
+ "prosemirror-state": "^1.0.0",
46
+ "prosemirror-view": "^1.0.0"
45
47
  },
46
48
  "devDependencies": {
47
49
  "@mathjax/src": "^4.1.0",
50
+ "@ocavue/eslint-config": "^4.1.0",
48
51
  "@ocavue/tsconfig": "^0.6.3",
52
+ "@prosekit/core": "^0.10.0",
53
+ "@types/node": "^20.19.10",
54
+ "@vitest/browser": "^4.0.18",
55
+ "@vitest/browser-playwright": "^4.0.18",
49
56
  "diffable-html-snapshot": "^0.2.0",
57
+ "eslint": "^10.0.1",
50
58
  "katex": "^0.16.28",
59
+ "playwright": "^1.58.2",
60
+ "prettier": "^3.8.1",
51
61
  "temml": "^0.13.1",
52
62
  "tsdown": "^0.20.3",
53
- "typescript": "~5.9.3",
63
+ "typescript": "^5.9.3",
64
+ "vite": "^7.3.1",
54
65
  "vitest": "^4.0.18",
55
- "vitest-browser-commands": "^0.2.0",
56
- "@prosekit/config-vitest": "0.0.0",
57
- "@prosekit/core": "^0.10.0"
58
- },
59
- "publishConfig": {
60
- "dev": {}
61
- },
62
- "dev": {
63
- "entry": {
64
- "prosemirror-math": "./src/index.ts"
65
- }
66
+ "vitest-browser-commands": "^0.2.0"
66
67
  },
67
68
  "scripts": {
68
- "build:tsc": "tsc -b tsconfig.json",
69
- "build:tsdown": "tsdown"
70
- },
71
- "types": "./dist/prosemirror-math.d.ts",
72
- "typesVersions": {
73
- "*": {
74
- ".": [
75
- "./dist/prosemirror-math.d.ts"
76
- ]
77
- }
69
+ "build": "tsdown",
70
+ "dev": "tsdown --watch",
71
+ "lint": "eslint .",
72
+ "fix": "eslint --fix . && prettier --write .",
73
+ "test:install": "playwright install chromium",
74
+ "test": "vitest",
75
+ "typecheck": "tsc -b"
78
76
  }
79
77
  }
@@ -10,13 +10,13 @@ describe('cursorInsidePlugin', () => {
10
10
 
11
11
  // Place cursor inside the math block
12
12
  const { state } = editor.view
13
- const tr = state.tr.setSelection(
14
- TextSelection.near(state.doc.resolve(2)),
15
- )
13
+ const tr = state.tr.setSelection(TextSelection.near(state.doc.resolve(2)))
16
14
  editor.view.dispatch(tr)
17
15
 
18
16
  const mathBlock = editor.view.dom.querySelector('.prosemirror-math-block')
19
- expect(mathBlock?.classList.contains('prosemirror-math-head-inside')).toBe(true)
17
+ expect(mathBlock?.classList.contains('prosemirror-math-head-inside')).toBe(
18
+ true,
19
+ )
20
20
  })
21
21
 
22
22
  it('applies decoration when cursor is inside mathInline', () => {
@@ -25,13 +25,13 @@ describe('cursorInsidePlugin', () => {
25
25
 
26
26
  // Place cursor inside the math inline node
27
27
  const { state } = editor.view
28
- const tr = state.tr.setSelection(
29
- TextSelection.near(state.doc.resolve(2)),
30
- )
28
+ const tr = state.tr.setSelection(TextSelection.near(state.doc.resolve(2)))
31
29
  editor.view.dispatch(tr)
32
30
 
33
31
  const mathInline = editor.view.dom.querySelector('.prosemirror-math-inline')
34
- expect(mathInline?.classList.contains('prosemirror-math-head-inside')).toBe(true)
32
+ expect(mathInline?.classList.contains('prosemirror-math-head-inside')).toBe(
33
+ true,
34
+ )
35
35
  })
36
36
 
37
37
  it('does not apply decoration when cursor is outside math nodes', () => {
@@ -40,12 +40,12 @@ describe('cursorInsidePlugin', () => {
40
40
 
41
41
  // Place cursor inside the paragraph (position 2 should be inside "hello")
42
42
  const { state } = editor.view
43
- const tr = state.tr.setSelection(
44
- TextSelection.near(state.doc.resolve(2)),
45
- )
43
+ const tr = state.tr.setSelection(TextSelection.near(state.doc.resolve(2)))
46
44
  editor.view.dispatch(tr)
47
45
 
48
46
  const mathBlock = editor.view.dom.querySelector('.prosemirror-math-block')
49
- expect(mathBlock?.classList.contains('prosemirror-math-head-inside')).toBe(false)
47
+ expect(mathBlock?.classList.contains('prosemirror-math-head-inside')).toBe(
48
+ false,
49
+ )
50
50
  })
51
51
  })
@@ -24,8 +24,10 @@ function createCursorInsideDecoration(
24
24
  /**
25
25
  * @internal
26
26
  */
27
- export function hasCursorInsideDecoration(decorations: readonly Decoration[]): boolean {
28
- return decorations.some(deco => deco.spec === DECORATION_SPEC)
27
+ export function hasCursorInsideDecoration(
28
+ decorations: readonly Decoration[],
29
+ ): boolean {
30
+ return decorations.some((deco) => deco.spec === DECORATION_SPEC)
29
31
  }
30
32
 
31
33
  type PluginState = DecorationSet | undefined
@@ -49,8 +51,8 @@ export function createCursorInsidePlugin(): Plugin {
49
51
  },
50
52
  apply(tr, oldValue, oldState, newState): PluginState {
51
53
  if (
52
- oldState.selection.head === newState.selection.head
53
- && !tr.docChanged
54
+ oldState.selection.head === newState.selection.head &&
55
+ !tr.docChanged
54
56
  ) {
55
57
  return oldValue
56
58
  }
package/src/index.ts CHANGED
@@ -4,4 +4,7 @@ export { mathBlockSpec } from './math-block-spec.ts'
4
4
  export { createMathBlockView, type RenderMathBlock } from './math-block-view.ts'
5
5
  export { createMathInlineInputRule } from './math-inline-input-rule.ts'
6
6
  export { mathInlineSpec } from './math-inline-spec.ts'
7
- export { createMathInlineView, type RenderMathInline } from './math-inline-view.ts'
7
+ export {
8
+ createMathInlineView,
9
+ type RenderMathInline,
10
+ } from './math-inline-view.ts'
@@ -26,9 +26,7 @@ describe('defineMathBlockEnterRule', () => {
26
26
  editor.set(n.doc(n.p('<a>')))
27
27
 
28
28
  await userEvent.keyboard('$$')
29
- expect(editor.view.state.doc.toJSON()).toEqual(
30
- n.doc(n.p('$$')).toJSON(),
31
- )
29
+ expect(editor.view.state.doc.toJSON()).toEqual(n.doc(n.p('$$')).toJSON())
32
30
 
33
31
  await userEvent.keyboard('{Enter}')
34
32
  expect(editor.view.state.doc.toJSON()).toEqual(
@@ -1,4 +1,7 @@
1
- import { createTextBlockEnterRule, type EnterRule } from 'prosemirror-enter-rules'
1
+ import {
2
+ createTextBlockEnterRule,
3
+ type EnterRule,
4
+ } from 'prosemirror-enter-rules'
2
5
 
3
6
  export const MATH_BLOCK_ENTER_REGEXP: RegExp = /^\$\$$/
4
7
 
@@ -8,7 +11,8 @@ export const MATH_BLOCK_ENTER_REGEXP: RegExp = /^\$\$$/
8
11
  *
9
12
  * @public
10
13
  */
11
- export const mathBlockEnterRule: EnterRule = /* @__PURE__ */ createTextBlockEnterRule({
12
- regex: MATH_BLOCK_ENTER_REGEXP,
13
- type: 'mathBlock',
14
- })
14
+ export const mathBlockEnterRule: EnterRule =
15
+ /* @__PURE__ */ createTextBlockEnterRule({
16
+ regex: MATH_BLOCK_ENTER_REGEXP,
17
+ type: 'mathBlock',
18
+ })
@@ -1,7 +1,13 @@
1
1
  import { formatHTML } from 'diffable-html-snapshot'
2
2
  import { describe, expect, it } from 'vitest'
3
3
 
4
- import { katexRenderer, mathjaxRenderer, renderers, setupTest, temmlRenderer } from './testing.ts'
4
+ import {
5
+ katexRenderer,
6
+ mathjaxRenderer,
7
+ renderers,
8
+ setupTest,
9
+ temmlRenderer,
10
+ } from './testing.ts'
5
11
 
6
12
  describe.each(Object.keys(renderers))('createMathBlockView (%s)', (name) => {
7
13
  const renderer = renderers[name as keyof typeof renderers]
@@ -24,7 +24,11 @@ export type RenderMathBlock = (text: string, element: HTMLElement) => void
24
24
  *
25
25
  * @public
26
26
  */
27
- export function createMathBlockView(renderMathBlock: RenderMathBlock, node: ProseMirrorNode, decorations: readonly Decoration[]): NodeView {
27
+ export function createMathBlockView(
28
+ renderMathBlock: RenderMathBlock,
29
+ node: ProseMirrorNode,
30
+ decorations: readonly Decoration[],
31
+ ): NodeView {
28
32
  const code = createElement('code')
29
33
  const source = createElement('pre', 'prosemirror-math-source', code)
30
34
  const display = createElement('div', 'prosemirror-math-display')
@@ -7,7 +7,9 @@ import { setupTest } from './testing.ts'
7
7
  describe('MATH_INPUT_REGEXP', () => {
8
8
  const regexp = new RegExp(MATH_INPUT_REGEXP)
9
9
 
10
- const cases: Array<[input: string, delimiter: string | null, captured: string | null]> = [
10
+ const cases: Array<
11
+ [input: string, delimiter: string | null, captured: string | null]
12
+ > = [
11
13
  // Single dollar: inline math
12
14
  ['$x$', '$', 'x'],
13
15
  ['$x^2$', '$', 'x^2'],
@@ -46,13 +48,16 @@ describe('MATH_INPUT_REGEXP', () => {
46
48
  ['hello', null, null],
47
49
  ]
48
50
 
49
- it.each(cases)('should handle %s', (input, expectedDelimiter, expectedContent) => {
50
- const match = regexp.exec(input)
51
- const delimiter = match?.[1] ?? null
52
- const captured = match?.[2] ?? null
53
- expect(delimiter).toEqual(expectedDelimiter)
54
- expect(captured).toEqual(expectedContent)
55
- })
51
+ it.each(cases)(
52
+ 'should handle %s',
53
+ (input, expectedDelimiter, expectedContent) => {
54
+ const match = regexp.exec(input)
55
+ const delimiter = match?.[1] ?? null
56
+ const captured = match?.[2] ?? null
57
+ expect(delimiter).toEqual(expectedDelimiter)
58
+ expect(captured).toEqual(expectedContent)
59
+ },
60
+ )
56
61
  })
57
62
 
58
63
  describe('defineMathInlineInputRule', () => {
@@ -62,9 +67,7 @@ describe('defineMathInlineInputRule', () => {
62
67
  editor.set(n.doc(n.p('<a>')))
63
68
 
64
69
  await userEvent.keyboard('$x^2')
65
- expect(editor.view.state.doc.toJSON()).toEqual(
66
- n.doc(n.p('$x^2')).toJSON(),
67
- )
70
+ expect(editor.view.state.doc.toJSON()).toEqual(n.doc(n.p('$x^2')).toJSON())
68
71
 
69
72
  await userEvent.keyboard('$')
70
73
  expect(editor.view.state.doc.toJSON()).toEqual(
@@ -99,9 +102,7 @@ describe('defineMathInlineInputRule', () => {
99
102
  editor.set(n.doc(n.p('<a>')))
100
103
 
101
104
  await userEvent.keyboard('$$')
102
- expect(editor.view.state.doc.toJSON()).toEqual(
103
- n.doc(n.p('$$')).toJSON(),
104
- )
105
+ expect(editor.view.state.doc.toJSON()).toEqual(n.doc(n.p('$$')).toJSON())
105
106
  })
106
107
 
107
108
  it('should not trigger with spaces at boundaries', async () => {
@@ -4,20 +4,19 @@ import { supportsRegexLookbehind } from '@ocavue/utils'
4
4
  import { InputRule } from 'prosemirror-inputrules'
5
5
 
6
6
  // Matches text wrapped in `$` or `$$`, e.g. `$x^2$` or `$$x^2$$`.
7
- export const MATH_INPUT_REGEXP: string = (
7
+ export const MATH_INPUT_REGEXP: string =
8
8
  // Don't allow $ before the opening delimiter
9
- (supportsRegexLookbehind() ? '(?<!\\$)' : '')
9
+ (supportsRegexLookbehind() ? '(?<!\\$)' : '') +
10
10
  // capture group 1: opening delimiter (`$` or `$$`)
11
- + '(\\$\\$?)'
11
+ '(\\$\\$?)' +
12
12
  // capture group 2: the math content
13
13
  // - a single non-whitespace, non-`$` character
14
14
  // - optionally followed by any non-`$` characters and a final non-whitespace, non-`$` character
15
- + '([^\\s$](?:[^$]*[^\\s$])?)'
15
+ '([^\\s$](?:[^$]*[^\\s$])?)' +
16
16
  // backreference: closing delimiter must match the opening
17
- + '\\1'
17
+ '\\1' +
18
18
  // end of input (required by ProseMirror InputRule)
19
- + '$'
20
- )
19
+ '$'
21
20
 
22
21
  /**
23
22
  * Creates a ProseMirror {@link InputRule} that converts text wrapped in `$` or
@@ -27,19 +26,20 @@ export const MATH_INPUT_REGEXP: string = (
27
26
  *
28
27
  * @public
29
28
  */
30
- export function createMathInlineInputRule(
31
- nodeType: string,
32
- ): InputRule {
33
- return new InputRule(new RegExp(MATH_INPUT_REGEXP), (state, match, start, end) => {
34
- const { tr, schema } = state
35
- const mathText = match[2]
36
- if (!mathText) return null
29
+ export function createMathInlineInputRule(nodeType: string): InputRule {
30
+ return new InputRule(
31
+ new RegExp(MATH_INPUT_REGEXP),
32
+ (state, match, start, end) => {
33
+ const { tr, schema } = state
34
+ const mathText = match[2]
35
+ if (!mathText) return null
37
36
 
38
- const type = schema.nodes[nodeType]
39
- if (!type) return null
37
+ const type = schema.nodes[nodeType]
38
+ if (!type) return null
40
39
 
41
- const node = type.create(null, schema.text(mathText))
42
- tr.replaceWith(start, end, node)
43
- return tr
44
- })
40
+ const node = type.create(null, schema.text(mathText))
41
+ tr.replaceWith(start, end, node)
42
+ return tr
43
+ },
44
+ )
45
45
  }
@@ -75,7 +75,9 @@ describe('mathInlineSpec', () => {
75
75
 
76
76
  it('can be mixed with regular text', () => {
77
77
  const { editor, n } = setupTest()
78
- editor.set(n.doc(n.paragraph('The formula ', n.mathInline('E=mc^2'), ' is famous.')))
78
+ editor.set(
79
+ n.doc(n.paragraph('The formula ', n.mathInline('E=mc^2'), ' is famous.')),
80
+ )
79
81
  const json = editor.getDocJSON()
80
82
  expect(json).toMatchInlineSnapshot(`
81
83
  {
@@ -1,7 +1,13 @@
1
1
  import { formatHTML } from 'diffable-html-snapshot'
2
2
  import { describe, expect, it } from 'vitest'
3
3
 
4
- import { katexRenderer, mathjaxRenderer, renderers, setupTest, temmlRenderer } from './testing.ts'
4
+ import {
5
+ katexRenderer,
6
+ mathjaxRenderer,
7
+ renderers,
8
+ setupTest,
9
+ temmlRenderer,
10
+ } from './testing.ts'
5
11
 
6
12
  describe.each(Object.keys(renderers))('createMathInlineView (%s)', (name) => {
7
13
  const renderer = renderers[name as keyof typeof renderers]
package/src/temml.ts CHANGED
@@ -1,9 +1,17 @@
1
1
  import Temml from 'temml'
2
2
 
3
3
  export function renderTemmlMathBlock(text: string, element: HTMLElement) {
4
- Temml.render(text, element, { displayMode: true, annotate: true, throwOnError: false })
4
+ Temml.render(text, element, {
5
+ displayMode: true,
6
+ annotate: true,
7
+ throwOnError: false,
8
+ })
5
9
  }
6
10
 
7
11
  export function renderTemmlMathInline(text: string, element: HTMLElement) {
8
- Temml.render(text, element, { displayMode: false, annotate: true, throwOnError: false })
12
+ Temml.render(text, element, {
13
+ displayMode: false,
14
+ annotate: true,
15
+ throwOnError: false,
16
+ })
9
17
  }
package/src/testing.ts CHANGED
@@ -135,11 +135,7 @@ function defineMathBlockView(renderer: MathRenderer): Extension {
135
135
  return defineNodeView({
136
136
  name: 'mathBlock',
137
137
  constructor: (node, _view, _getPos, decorations) => {
138
- return createMathBlockView(
139
- renderer.renderBlock,
140
- node,
141
- decorations,
142
- )
138
+ return createMathBlockView(renderer.renderBlock, node, decorations)
143
139
  },
144
140
  })
145
141
  }
@@ -148,11 +144,7 @@ function defineMathInlineView(renderer: MathRenderer): Extension {
148
144
  return defineNodeView({
149
145
  name: 'mathInline',
150
146
  constructor: (node, _view, _getPos, decorations) => {
151
- return createMathInlineView(
152
- renderer.renderInline,
153
- node,
154
- decorations,
155
- )
147
+ return createMathInlineView(renderer.renderInline, node, decorations)
156
148
  },
157
149
  })
158
150
  }
@@ -173,7 +165,9 @@ function defineTestExtension(renderer: MathRenderer) {
173
165
  defineMathBlockView(renderer),
174
166
  defineMathInlineView(renderer),
175
167
  defineEnterRule([mathBlockEnterRule]),
176
- definePlugin(inputRules({ rules: [createMathInlineInputRule('mathInline')] })),
168
+ definePlugin(
169
+ inputRules({ rules: [createMathInlineInputRule('mathInline')] }),
170
+ ),
177
171
  definePlugin(createCursorInsidePlugin()),
178
172
  defineBaseCommands(),
179
173
  defineBaseKeymap(),