@blocknote/core 0.53.0 → 0.54.1

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.
Files changed (219) hide show
  1. package/dist/BlockNoteExtension-BJCLnwrE.cjs +2 -0
  2. package/dist/BlockNoteExtension-BJCLnwrE.cjs.map +1 -0
  3. package/dist/BlockNoteExtension-CbvDA0F7.js +80 -0
  4. package/dist/BlockNoteExtension-CbvDA0F7.js.map +1 -0
  5. package/dist/{UserStore-QbbPFj0z.js → UserStore-C4TzkGEn.js} +3 -3
  6. package/dist/{UserStore-DCG6lsla.cjs.map → UserStore-C4TzkGEn.js.map} +1 -1
  7. package/dist/{UserStore-DCG6lsla.cjs → UserStore-mj3fXW4D.cjs} +2 -2
  8. package/dist/UserStore-mj3fXW4D.cjs.map +1 -0
  9. package/dist/blocknote.cjs +1 -1
  10. package/dist/blocknote.js +6 -6
  11. package/dist/blocks-CTB_5Vyj.cjs +12 -0
  12. package/dist/blocks-CTB_5Vyj.cjs.map +1 -0
  13. package/dist/{blocks-BEJW9OM_.js → blocks-Dol85hpH.js} +680 -670
  14. package/dist/blocks-Dol85hpH.js.map +1 -0
  15. package/dist/blocks.cjs +1 -1
  16. package/dist/blocks.js +2 -2
  17. package/dist/comments.cjs +1 -1
  18. package/dist/comments.cjs.map +1 -1
  19. package/dist/comments.js +7 -7
  20. package/dist/comments.js.map +1 -1
  21. package/dist/en-BuzSuuTq.cjs +2 -0
  22. package/dist/en-BuzSuuTq.cjs.map +1 -0
  23. package/dist/{en-CGKXfWma.js → en-uzHzj6eH.js} +10 -1
  24. package/dist/en-uzHzj6eH.js.map +1 -0
  25. package/dist/extensions-B7Rsp_Ft.cjs +58 -0
  26. package/dist/extensions-B7Rsp_Ft.cjs.map +1 -0
  27. package/dist/{extensions-BR5hkAio.js → extensions-D6JcJCwd.js} +735 -457
  28. package/dist/extensions-D6JcJCwd.js.map +1 -0
  29. package/dist/extensions.cjs +1 -1
  30. package/dist/extensions.js +4 -4
  31. package/dist/locales.cjs +1 -1
  32. package/dist/locales.cjs.map +1 -1
  33. package/dist/locales.js +199 -1
  34. package/dist/locales.js.map +1 -1
  35. package/dist/{src-BUXut68d.js → src-Buuo5l7X.js} +924 -851
  36. package/dist/src-Buuo5l7X.js.map +1 -0
  37. package/dist/src-C54Ok9E0.cjs +23 -0
  38. package/dist/src-C54Ok9E0.cjs.map +1 -0
  39. package/dist/style.css +1 -1
  40. package/dist/webpack-stats.json +1 -1
  41. package/dist/y.cjs +1 -1
  42. package/dist/y.cjs.map +1 -1
  43. package/dist/y.js +245 -242
  44. package/dist/y.js.map +1 -1
  45. package/dist/yjs.cjs +1 -1
  46. package/dist/yjs.cjs.map +1 -1
  47. package/dist/yjs.js +79 -67
  48. package/dist/yjs.js.map +1 -1
  49. package/package.json +17 -18
  50. package/src/api/blockManipulation/commands/updateBlock/__snapshots__/updateBlock.test.ts.snap +2505 -763
  51. package/src/api/blockManipulation/commands/updateBlock/updateBlock.test.ts +42 -0
  52. package/src/api/blockManipulation/commands/updateBlock/updateBlock.ts +16 -4
  53. package/src/api/exporters/markdown/htmlToMarkdown.ts +45 -10
  54. package/src/api/getBlocksChangedByTransaction.test.ts +211 -0
  55. package/src/api/getBlocksChangedByTransaction.ts +48 -12
  56. package/src/api/getChangedRange.ts +54 -0
  57. package/src/api/nodeConversions/blockToNode.ts +5 -1
  58. package/src/api/nodeConversions/nodeToBlock.ts +3 -0
  59. package/src/api/pmUtil.ts +16 -8
  60. package/src/blocks/Code/CodeBlockOptions.ts +83 -0
  61. package/src/blocks/Code/block.test.ts +1 -1
  62. package/src/blocks/Code/block.ts +29 -281
  63. package/src/blocks/Code/helpers/extensions/CodeKeyboardShortcutsExtension.ts +120 -0
  64. package/src/blocks/Code/helpers/parse/parsePreCode.ts +45 -0
  65. package/src/blocks/Code/helpers/render/createCodeBlock.ts +98 -0
  66. package/src/blocks/Code/helpers/toExternalHTML/createPreCode.ts +14 -0
  67. package/src/blocks/index.ts +3 -1
  68. package/src/comments/extension.ts +2 -4
  69. package/src/editor/Block.css +273 -2
  70. package/src/editor/BlockNoteEditor.ts +25 -4
  71. package/src/editor/BlockNoteExtension.ts +1 -1
  72. package/src/editor/managers/ExtensionManager/extensions.ts +6 -0
  73. package/src/editor/performance.test.ts +45 -0
  74. package/src/exporter/ExportImage.ts +50 -0
  75. package/src/exporter/Exporter.test.ts +186 -0
  76. package/src/exporter/Exporter.ts +66 -14
  77. package/src/exporter/index.ts +1 -0
  78. package/src/exporter/mapping.ts +4 -1
  79. package/src/extensions/FormattingToolbar/FormattingToolbar.ts +10 -8
  80. package/src/extensions/InlineContentBoundaryEdit/InlineContentBoundaryEdit.ts +109 -0
  81. package/src/extensions/PreviousBlockType/PreviousBlockType.test.ts +21 -0
  82. package/src/extensions/PreviousBlockType/PreviousBlockType.ts +5 -3
  83. package/src/extensions/SourceBlockWithPreview/SourceBlockWithPreview.ts +192 -0
  84. package/src/extensions/SourceInlineContentWithPreview/SourceInlineContentWithPreview.ts +142 -0
  85. package/src/extensions/SyntaxHighlighting/SyntaxHighlighting.test.ts +120 -0
  86. package/src/extensions/SyntaxHighlighting/SyntaxHighlighting.ts +89 -0
  87. package/src/extensions/SyntaxHighlighting/inlineHighlight.test.ts +135 -0
  88. package/src/extensions/SyntaxHighlighting/shiki.ts +116 -0
  89. package/src/extensions/TableHandles/TableHandles.browser.test.ts +305 -0
  90. package/src/extensions/TableHandles/TableHandles.ts +82 -41
  91. package/src/extensions/Versioning/inMemoryVersioning.ts +13 -3
  92. package/src/extensions/index.ts +4 -0
  93. package/src/extensions/tiptap-extensions/KeyboardShortcuts/KeyboardShortcutsExtension.test.ts +51 -2
  94. package/src/extensions/tiptap-extensions/KeyboardShortcuts/KeyboardShortcutsExtension.ts +22 -2
  95. package/src/fonts/inter.css +9 -9
  96. package/src/i18n/locales/ar.ts +9 -0
  97. package/src/i18n/locales/de.ts +9 -0
  98. package/src/i18n/locales/en.ts +9 -0
  99. package/src/i18n/locales/es.ts +9 -0
  100. package/src/i18n/locales/fa.ts +9 -0
  101. package/src/i18n/locales/fr.ts +9 -0
  102. package/src/i18n/locales/he.ts +9 -0
  103. package/src/i18n/locales/hr.ts +9 -0
  104. package/src/i18n/locales/is.ts +9 -0
  105. package/src/i18n/locales/it.ts +9 -0
  106. package/src/i18n/locales/ja.ts +9 -0
  107. package/src/i18n/locales/ko.ts +9 -0
  108. package/src/i18n/locales/nl.ts +9 -0
  109. package/src/i18n/locales/no.ts +9 -0
  110. package/src/i18n/locales/pl.ts +9 -0
  111. package/src/i18n/locales/pt.ts +9 -0
  112. package/src/i18n/locales/ru.ts +9 -0
  113. package/src/i18n/locales/sk.ts +9 -0
  114. package/src/i18n/locales/uk.ts +9 -0
  115. package/src/i18n/locales/uz.ts +9 -0
  116. package/src/i18n/locales/vi.ts +9 -0
  117. package/src/i18n/locales/zh-tw.ts +9 -0
  118. package/src/i18n/locales/zh.ts +9 -0
  119. package/src/index.ts +5 -1
  120. package/src/schema/blocks/createSpec.ts +5 -7
  121. package/src/schema/blocks/types.ts +53 -3
  122. package/src/schema/contentTypePropagation.test.ts +1 -1
  123. package/src/schema/inlineContent/createSpec.test.ts +161 -0
  124. package/src/schema/inlineContent/createSpec.ts +175 -17
  125. package/src/schema/inlineContent/internal.ts +21 -2
  126. package/src/schema/inlineContent/types.ts +35 -5
  127. package/src/schema/nodeViewMutations.test.ts +81 -56
  128. package/src/schema/nodeViewMutations.ts +30 -27
  129. package/src/schema/schema.test.ts +110 -0
  130. package/src/schema/schema.ts +13 -4
  131. package/src/user/UserStore.ts +1 -1
  132. package/src/util/Store.test.ts +132 -0
  133. package/src/util/Store.ts +135 -0
  134. package/src/y/extensions/AttributionExtension.test.ts +18 -9
  135. package/src/y/extensions/Versioning.test.ts +20 -7
  136. package/src/y/versioning/__test__/yhub.test.ts +92 -59
  137. package/src/y/versioning/yhub.ts +35 -29
  138. package/src/yjs/comments/RESTYjsThreadStore.test.ts +85 -0
  139. package/src/yjs/comments/RESTYjsThreadStore.ts +7 -1
  140. package/src/yjs/extensions/ForkYDoc.test.ts +37 -0
  141. package/src/yjs/extensions/ForkYDoc.ts +26 -1
  142. package/src/yjs/extensions/RelativePositionMapping.ts +8 -2
  143. package/src/yjs/extensions/Versioning.test.ts +17 -5
  144. package/types/src/api/getChangedRange.d.ts +14 -0
  145. package/types/src/api/pmUtil.d.ts +7 -7
  146. package/types/src/blocks/Code/CodeBlockOptions.d.ts +61 -0
  147. package/types/src/blocks/Code/block.d.ts +3 -53
  148. package/types/src/blocks/Code/helpers/extensions/CodeKeyboardShortcutsExtension.d.ts +35 -0
  149. package/types/src/blocks/Code/helpers/parse/parsePreCode.d.ts +8 -0
  150. package/types/src/blocks/Code/helpers/render/createCodeBlock.d.ts +18 -0
  151. package/types/src/blocks/Code/helpers/toExternalHTML/createPreCode.d.ts +5 -0
  152. package/types/src/blocks/index.d.ts +3 -1
  153. package/types/src/comments/extension.d.ts +1 -15
  154. package/types/src/editor/BlockNoteEditor.d.ts +3 -3
  155. package/types/src/editor/BlockNoteExtension.d.ts +1 -1
  156. package/types/src/exporter/ExportImage.d.ts +36 -0
  157. package/types/src/exporter/Exporter.d.ts +30 -0
  158. package/types/src/exporter/Exporter.test.d.ts +1 -0
  159. package/types/src/exporter/index.d.ts +1 -0
  160. package/types/src/exporter/mapping.d.ts +1 -1
  161. package/types/src/extensions/FilePanel/FilePanel.d.ts +1 -1
  162. package/types/src/extensions/FormattingToolbar/FormattingToolbar.d.ts +1 -1
  163. package/types/src/extensions/InlineContentBoundaryEdit/InlineContentBoundaryEdit.d.ts +5 -0
  164. package/types/src/extensions/ShowSelection/ShowSelection.d.ts +1 -5
  165. package/types/src/extensions/SideMenu/SideMenu.d.ts +1 -1
  166. package/types/src/extensions/SourceBlockWithPreview/SourceBlockWithPreview.d.ts +45 -0
  167. package/types/src/extensions/SourceInlineContentWithPreview/SourceInlineContentWithPreview.d.ts +49 -0
  168. package/types/src/extensions/SuggestionMenu/SuggestionMenu.d.ts +1 -11
  169. package/types/src/extensions/SyntaxHighlighting/SyntaxHighlighting.d.ts +44 -0
  170. package/types/src/extensions/SyntaxHighlighting/SyntaxHighlighting.test.d.ts +1 -0
  171. package/types/src/extensions/SyntaxHighlighting/inlineHighlight.test.d.ts +1 -0
  172. package/types/src/extensions/SyntaxHighlighting/shiki.d.ts +13 -0
  173. package/types/src/extensions/TableHandles/TableHandles.browser.test.d.ts +1 -0
  174. package/types/src/extensions/TableHandles/TableHandles.d.ts +4 -1
  175. package/types/src/extensions/Versioning/Versioning.d.ts +1 -35
  176. package/types/src/extensions/index.d.ts +4 -0
  177. package/types/src/i18n/locales/en.d.ts +9 -0
  178. package/types/src/i18n/locales/fa.d.ts +9 -0
  179. package/types/src/i18n/locales/sk.d.ts +9 -0
  180. package/types/src/index.d.ts +2 -1
  181. package/types/src/schema/blocks/types.d.ts +36 -3
  182. package/types/src/schema/inlineContent/createSpec.d.ts +35 -3
  183. package/types/src/schema/inlineContent/createSpec.test.d.ts +1 -0
  184. package/types/src/schema/inlineContent/internal.d.ts +3 -2
  185. package/types/src/schema/inlineContent/types.d.ts +22 -3
  186. package/types/src/schema/nodeViewMutations.d.ts +2 -2
  187. package/types/src/schema/schema.test.d.ts +1 -0
  188. package/types/src/user/UserStore.d.ts +1 -1
  189. package/types/src/util/Store.d.ts +45 -0
  190. package/types/src/util/Store.test.d.ts +1 -0
  191. package/types/src/y/extensions/AttributionExtension.d.ts +1 -1
  192. package/types/src/y/extensions/DiffVersioningExtension.d.ts +1 -1
  193. package/types/src/y/extensions/ForkYDoc.d.ts +1 -5
  194. package/types/src/y/extensions/index.d.ts +2 -10
  195. package/types/src/y/versioning/yhub.d.ts +3 -2
  196. package/types/src/yjs/comments/RESTYjsThreadStore.test.d.ts +1 -0
  197. package/types/src/yjs/extensions/ForkYDoc.d.ts +1 -5
  198. package/types/src/yjs/extensions/index.d.ts +1 -5
  199. package/dist/BlockNoteExtension-B_IEJHS8.js +0 -38
  200. package/dist/BlockNoteExtension-B_IEJHS8.js.map +0 -1
  201. package/dist/BlockNoteExtension-M0znTzxP.cjs +0 -2
  202. package/dist/BlockNoteExtension-M0znTzxP.cjs.map +0 -1
  203. package/dist/UserStore-QbbPFj0z.js.map +0 -1
  204. package/dist/blocks-BEJW9OM_.js.map +0 -1
  205. package/dist/blocks-BPmJglFo.cjs +0 -12
  206. package/dist/blocks-BPmJglFo.cjs.map +0 -1
  207. package/dist/en-C5Z4Ag9_.cjs +0 -2
  208. package/dist/en-C5Z4Ag9_.cjs.map +0 -1
  209. package/dist/en-CGKXfWma.js.map +0 -1
  210. package/dist/extensions-AitWbYdJ.cjs +0 -52
  211. package/dist/extensions-AitWbYdJ.cjs.map +0 -1
  212. package/dist/extensions-BR5hkAio.js.map +0 -1
  213. package/dist/src-BHPuOo7W.cjs +0 -21
  214. package/dist/src-BHPuOo7W.cjs.map +0 -1
  215. package/dist/src-BUXut68d.js.map +0 -1
  216. package/src/blocks/Code/shiki.ts +0 -73
  217. package/src/blocks/File/helpers/uploadToTmpFilesDotOrg_DEV_ONLY.ts +0 -20
  218. package/types/src/blocks/Code/shiki.d.ts +0 -4
  219. package/types/src/blocks/File/helpers/uploadToTmpFilesDotOrg_DEV_ONLY.d.ts +0 -6
@@ -0,0 +1,120 @@
1
+ import { describe, expect, it } from "vite-plus/test";
2
+ import {
3
+ collectHighlightNodeTypes,
4
+ SyntaxHighlightingExtension,
5
+ } from "./SyntaxHighlighting.js";
6
+
7
+ /**
8
+ * @vitest-environment jsdom
9
+ */
10
+
11
+ describe("SyntaxHighlightingExtension", () => {
12
+ // The extension only reads `editor.schema.blockSpecs` and
13
+ // `inlineContentSpecs`, so a minimal stub is enough.
14
+ const fakeEditor = () =>
15
+ ({
16
+ schema: {
17
+ blockSpecs: {
18
+ paragraph: { config: { type: "paragraph", content: "inline" } },
19
+ codeBlock: { config: { type: "codeBlock", content: "plain" } },
20
+ image: { config: { type: "image", content: "none" } },
21
+ },
22
+ inlineContentSpecs: {},
23
+ },
24
+ }) as any;
25
+
26
+ const pluginsFor = (options: any) =>
27
+ SyntaxHighlightingExtension(options)({ editor: fakeEditor() })
28
+ .prosemirrorPlugins;
29
+
30
+ // Whether highlighting is enabled at all is decided by the user (they choose
31
+ // to add this extension to the editor's `extensions`), so the extension
32
+ // itself always installs the plugin once created.
33
+ it("installs a highlight plugin when a highlighter is configured", () => {
34
+ const plugins = pluginsFor({ createHighlighter: async () => ({}) as any });
35
+
36
+ expect(plugins).toHaveLength(1);
37
+ });
38
+
39
+ it("installs the plugin even without a highlighter (it no-ops at parse time)", () => {
40
+ expect(pluginsFor({})).toHaveLength(1);
41
+ });
42
+ });
43
+
44
+ describe("collectHighlightNodeTypes", () => {
45
+ const highlight = () => "latex";
46
+
47
+ it("includes blocks with `content: plain` and a `meta.highlight`", () => {
48
+ const types = collectHighlightNodeTypes({
49
+ blockSpecs: {
50
+ // Highlightable: plain content + a highlight callback.
51
+ mathBlock: {
52
+ config: { type: "mathBlock", content: "plain" },
53
+ implementation: { meta: { highlight } },
54
+ },
55
+ // Not highlightable: no highlight callback.
56
+ paragraph: {
57
+ config: { type: "paragraph", content: "inline" },
58
+ implementation: { meta: {} },
59
+ },
60
+ // Not highlightable: `content: none` holds no editable text.
61
+ image: {
62
+ config: { type: "image", content: "none" },
63
+ implementation: { meta: { highlight } },
64
+ },
65
+ },
66
+ inlineContentSpecs: {},
67
+ });
68
+
69
+ expect(types).toEqual(["mathBlock"]);
70
+ });
71
+
72
+ it("includes inline content with `content: plain` and a `meta.highlight`", () => {
73
+ const types = collectHighlightNodeTypes({
74
+ blockSpecs: {},
75
+ inlineContentSpecs: {
76
+ // Highlightable: plain (editable plain text) + a highlight callback.
77
+ math: {
78
+ config: { type: "math", content: "plain" },
79
+ implementation: { meta: { highlight } },
80
+ },
81
+ // Not highlightable: no highlight callback.
82
+ mention: {
83
+ config: { type: "mention", content: "plain" },
84
+ implementation: { meta: {} },
85
+ },
86
+ // Not highlightable: `content: none` holds no editable text.
87
+ tag: {
88
+ config: { type: "tag", content: "none" },
89
+ implementation: { meta: { highlight } },
90
+ },
91
+ // Built-in `text`/`link` specs have string configs, not objects.
92
+ text: { config: "text", implementation: undefined },
93
+ link: { config: "link", implementation: undefined },
94
+ },
95
+ });
96
+
97
+ expect(types).toEqual(["math"]);
98
+ });
99
+
100
+ it("collects both block and inline-content highlight types together", () => {
101
+ const types = collectHighlightNodeTypes({
102
+ blockSpecs: {
103
+ mathBlock: {
104
+ config: { type: "mathBlock", content: "plain" },
105
+ implementation: { meta: { highlight } },
106
+ },
107
+ },
108
+ inlineContentSpecs: {
109
+ math: {
110
+ config: { type: "math", content: "plain" },
111
+ implementation: { meta: { highlight } },
112
+ },
113
+ },
114
+ });
115
+
116
+ expect(types).toContain("mathBlock");
117
+ expect(types).toContain("math");
118
+ expect(types).toHaveLength(2);
119
+ });
120
+ });
@@ -0,0 +1,89 @@
1
+ import type { HighlighterGeneric } from "@shikijs/types";
2
+ import {
3
+ createExtension,
4
+ ExtensionOptions,
5
+ } from "../../editor/BlockNoteExtension.js";
6
+ import { lazyShikiPlugin } from "./shiki.js";
7
+ import {
8
+ CustomInlineContentConfig,
9
+ InlineContentSpec,
10
+ LooseBlockSpec,
11
+ } from "../../schema/index.js";
12
+
13
+ export type SyntaxHighlightingOptions = {
14
+ /**
15
+ * Creates the Shiki highlighter used for syntax highlighting. Can be
16
+ * asynchronous, so the highlighter is only loaded once it's first needed.
17
+ *
18
+ * When omitted, content renders without syntax highlighting.
19
+ */
20
+ createHighlighter: () => Promise<HighlighterGeneric<any, any>>;
21
+ };
22
+
23
+ /**
24
+ * Collects the node type names that should be syntax-highlighted from a schema's
25
+ * block and inline-content specs.
26
+ *
27
+ * A spec is a candidate when it has a `meta.highlight` callback (which decides
28
+ * the language) AND the node actually holds editable text. Block and
29
+ * inline-content specs use different `content` value spaces, so "editable text"
30
+ * means `content === "plain"` for both blocks (code/math blocks) and inline
31
+ * content (inline math) - both hold plain text - hence the two are filtered
32
+ * separately.
33
+ *
34
+ * Inline content (e.g. inline math) is highlighted too: `prosemirror-highlight`
35
+ * collects nodes by `node.inlineContent` since v0.15.3
36
+ * (https://github.com/ocavue/prosemirror-highlight/pull/137), so inline nodes
37
+ * holding inline content are visited alongside text blocks.
38
+ */
39
+ export function collectHighlightNodeTypes(schema: {
40
+ blockSpecs: Record<string, unknown>;
41
+ inlineContentSpecs: Record<string, unknown>;
42
+ }): string[] {
43
+ const blockNodeTypes = Object.values(schema.blockSpecs)
44
+ .filter(
45
+ (blockSpec): blockSpec is LooseBlockSpec =>
46
+ typeof (blockSpec as LooseBlockSpec)?.config === "object" &&
47
+ (blockSpec as LooseBlockSpec).config.content === "plain" &&
48
+ !!(blockSpec as LooseBlockSpec).implementation?.meta?.highlight,
49
+ )
50
+ .map((blockSpec) => blockSpec.config.type);
51
+
52
+ const inlineContentNodeTypes = Object.values(schema.inlineContentSpecs)
53
+ .filter(
54
+ (
55
+ inlineContentSpec,
56
+ ): inlineContentSpec is InlineContentSpec<CustomInlineContentConfig> =>
57
+ typeof (
58
+ inlineContentSpec as InlineContentSpec<CustomInlineContentConfig>
59
+ )?.config === "object" &&
60
+ (inlineContentSpec as InlineContentSpec<CustomInlineContentConfig>)
61
+ .config.content === "plain" &&
62
+ !!(inlineContentSpec as InlineContentSpec<CustomInlineContentConfig>)
63
+ .implementation?.meta?.highlight,
64
+ )
65
+ .map((inlineContentSpec) => inlineContentSpec.config.type);
66
+
67
+ return [...blockNodeTypes, ...inlineContentNodeTypes];
68
+ }
69
+
70
+ /**
71
+ * A single editor-wide extension that syntax-highlights block and inline-content
72
+ * content. Which nodes get highlighted (and as which language) is decided by
73
+ * each spec's `meta.highlight` callback, so individual specs declare their own
74
+ * language rather than the extension configuring them.
75
+ *
76
+ * Highlighting is opt-in: the user adds this extension to the editor's
77
+ * `extensions` (configured with a `createHighlighter`) to enable it. When it's
78
+ * absent, content renders as plain text.
79
+ */
80
+ export const SyntaxHighlightingExtension = createExtension(
81
+ ({ editor, options }: ExtensionOptions<SyntaxHighlightingOptions>) => {
82
+ const nodeTypes = collectHighlightNodeTypes(editor.schema);
83
+
84
+ return {
85
+ key: "syntaxHighlighting",
86
+ prosemirrorPlugins: [lazyShikiPlugin(options, nodeTypes, editor.schema)],
87
+ };
88
+ },
89
+ );
@@ -0,0 +1,135 @@
1
+ import { Schema } from "prosemirror-model";
2
+ import { EditorState, PluginKey } from "prosemirror-state";
3
+ import { Decoration } from "prosemirror-view";
4
+ import { createHighlightPlugin } from "prosemirror-highlight";
5
+ import { describe, expect, it } from "vite-plus/test";
6
+
7
+ /**
8
+ * @vitest-environment jsdom
9
+ */
10
+
11
+ // Regression coverage for inline syntax highlighting. `prosemirror-highlight`
12
+ // used to collect only text-block nodes, so inline nodes (e.g. inline math)
13
+ // were never highlighted. Since v0.15.3 it collects nodes by
14
+ // `node.inlineContent` (https://github.com/ocavue/prosemirror-highlight/pull/137),
15
+ // so inline content with a `meta.highlight` is highlighted too. This suite
16
+ // guards that behavior. It drives the plugin directly against a hand-built
17
+ // ProseMirror doc, so it needs neither Shiki nor a browser.
18
+ describe("inline syntax highlighting", () => {
19
+ // A minimal schema with an *inline* node type that holds inline content
20
+ // (`content: "text*"`), mirroring how inline content like inline math is
21
+ // structured, plus an *atom* inline node that holds no content.
22
+ const schema = new Schema({
23
+ nodes: {
24
+ text: { group: "inline" },
25
+ inlineCode: {
26
+ group: "inline",
27
+ inline: true,
28
+ content: "text*",
29
+ toDOM: () => ["span", 0],
30
+ },
31
+ // An atom inline node (no inline content) - like a mention. It should
32
+ // never be collected for highlighting even if named in `nodeTypes`, since
33
+ // it holds no editable text. This is why the library keys off
34
+ // `node.inlineContent` rather than merely `node.isInline`.
35
+ mention: {
36
+ group: "inline",
37
+ inline: true,
38
+ atom: true,
39
+ toDOM: () => ["span", "@x"],
40
+ },
41
+ paragraph: {
42
+ group: "block",
43
+ content: "inline*",
44
+ toDOM: () => ["p", 0],
45
+ },
46
+ doc: { content: "block+" },
47
+ },
48
+ });
49
+
50
+ const docWithInline = schema.node("doc", null, [
51
+ schema.node("paragraph", null, [
52
+ schema.text("before "),
53
+ schema.node("inlineCode", null, [schema.text("const x = 1")]),
54
+ schema.text(" "),
55
+ schema.node("mention"),
56
+ schema.text(" after"),
57
+ ]),
58
+ ]);
59
+
60
+ it("calls the parser with the inline node's content and decorates it", () => {
61
+ const seen: { content: string; language?: string }[] = [];
62
+
63
+ const plugin = createHighlightPlugin({
64
+ // Synchronous stub parser mirroring how the real Shiki parser emits token
65
+ // decorations: `Decoration.inline` over positions *inside* the node,
66
+ // starting at `pos + 1` (the node's content starts after its opening
67
+ // boundary). Node-spanning decorations aren't valid over an inline node,
68
+ // which is fine - the token decorations are what actually color the text.
69
+ parser: ({ content, language, pos }) => {
70
+ seen.push({ content, language });
71
+ return [
72
+ Decoration.inline(pos + 1, pos + 1 + content.length, {
73
+ class: "hl",
74
+ }),
75
+ ];
76
+ },
77
+ nodeTypes: ["inlineCode"],
78
+ languageExtractor: () => "javascript",
79
+ });
80
+
81
+ const state = EditorState.create({
82
+ schema,
83
+ doc: docWithInline,
84
+ plugins: [plugin],
85
+ });
86
+
87
+ const key = (plugin as any).spec.key as PluginKey;
88
+ const pluginState = key.getState(state);
89
+
90
+ // The inline node's text reached the parser - proving inline nodes are
91
+ // collected (with the pre-0.15.3 library, `seen` would be empty).
92
+ expect(seen).toHaveLength(1);
93
+ expect(seen[0].content).toBe("const x = 1");
94
+ expect(seen[0].language).toBe("javascript");
95
+
96
+ // And a decoration was produced for it.
97
+ expect(pluginState.decorations).toBeDefined();
98
+ expect(pluginState.decorations.find().length).toBeGreaterThan(0);
99
+ });
100
+
101
+ it("still leaves non-matching inline nodes untouched", () => {
102
+ const seen: string[] = [];
103
+ const plugin = createHighlightPlugin({
104
+ parser: ({ content }) => {
105
+ seen.push(content);
106
+ return [];
107
+ },
108
+ nodeTypes: ["somethingElse"],
109
+ languageExtractor: () => "javascript",
110
+ });
111
+
112
+ EditorState.create({ schema, doc: docWithInline, plugins: [plugin] });
113
+
114
+ expect(seen).toHaveLength(0);
115
+ });
116
+
117
+ it("excludes atom inline nodes even when named in `nodeTypes`", () => {
118
+ const seen: string[] = [];
119
+ const plugin = createHighlightPlugin({
120
+ parser: ({ content }) => {
121
+ seen.push(content);
122
+ return [];
123
+ },
124
+ // `mention` is an atom inline node with no inline content, so it holds no
125
+ // text to highlight. The library keys off `node.inlineContent`, not
126
+ // `node.isInline`, so it's correctly skipped.
127
+ nodeTypes: ["mention"],
128
+ languageExtractor: () => "javascript",
129
+ });
130
+
131
+ EditorState.create({ schema, doc: docWithInline, plugins: [plugin] });
132
+
133
+ expect(seen).toHaveLength(0);
134
+ });
135
+ });
@@ -0,0 +1,116 @@
1
+ import type { HighlighterGeneric } from "@shikijs/types";
2
+ import { Parser, createHighlightPlugin } from "prosemirror-highlight";
3
+ import { createParser } from "prosemirror-highlight/shiki";
4
+ import type { SyntaxHighlightingOptions } from "./SyntaxHighlighting.js";
5
+ import { CustomBlockNoteSchema } from "../../schema/schema.js";
6
+
7
+ export const shikiParserSymbol = Symbol.for("blocknote.shikiParser");
8
+ export const shikiHighlighterPromiseSymbol = Symbol.for(
9
+ "blocknote.shikiHighlighterPromise",
10
+ );
11
+
12
+ // Languages that represent "no highlighting" - skipped without asking Shiki to
13
+ // load a grammar for them.
14
+ const PLAIN_TEXT_LANGUAGES = ["text", "none", "plaintext", "txt"];
15
+
16
+ /**
17
+ * Creates the syntax highlighting plugin for the given block types, lazily
18
+ * loading the highlighter on first use.
19
+ *
20
+ * Each spec's `meta.highlight` callback resolves a node to a language, which is
21
+ * passed straight to Shiki - it resolves aliases and loads the grammar from its
22
+ * bundle, so any language the provided highlighter bundles can be highlighted.
23
+ */
24
+ export function lazyShikiPlugin(
25
+ options: SyntaxHighlightingOptions,
26
+ nodeTypes: string[],
27
+ schema: CustomBlockNoteSchema<any, any, any>,
28
+ ) {
29
+ const globalThisForShiki = globalThis as {
30
+ [shikiHighlighterPromiseSymbol]?: Promise<HighlighterGeneric<any, any>>;
31
+ [shikiParserSymbol]?: Parser;
32
+ };
33
+
34
+ let highlighter: HighlighterGeneric<any, any> | undefined;
35
+ let parser: Parser | undefined;
36
+ // Languages the highlighter failed to load (e.g. not in its bundle). Tracked
37
+ // so we don't keep retrying - and re-triggering re-highlights - forever.
38
+ const unsupportedLanguages = new Set<string>();
39
+ const lazyParser: Parser = (parserOptions) => {
40
+ if (!options.createHighlighter) {
41
+ return [];
42
+ }
43
+ if (!highlighter) {
44
+ globalThisForShiki[shikiHighlighterPromiseSymbol] =
45
+ globalThisForShiki[shikiHighlighterPromiseSymbol] ||
46
+ options.createHighlighter();
47
+
48
+ return globalThisForShiki[shikiHighlighterPromiseSymbol].then(
49
+ (createdHighlighter) => {
50
+ highlighter = createdHighlighter;
51
+ },
52
+ );
53
+ }
54
+ const language = parserOptions.language;
55
+
56
+ if (
57
+ !language ||
58
+ PLAIN_TEXT_LANGUAGES.includes(language) ||
59
+ unsupportedLanguages.has(language)
60
+ ) {
61
+ return [];
62
+ }
63
+
64
+ if (!highlighter.getLoadedLanguages().includes(language)) {
65
+ return highlighter.loadLanguage(language as any).catch(() => {
66
+ // The highlighter doesn't bundle this language - give up on it so we
67
+ // don't loop trying to load it on every re-highlight.
68
+ unsupportedLanguages.add(language);
69
+ });
70
+ }
71
+
72
+ if (!parser) {
73
+ parser =
74
+ globalThisForShiki[shikiParserSymbol] ||
75
+ createParser(highlighter as any, pickThemeOptions(highlighter));
76
+ globalThisForShiki[shikiParserSymbol] = parser;
77
+ }
78
+
79
+ return parser(parserOptions);
80
+ };
81
+
82
+ return createHighlightPlugin({
83
+ parser: lazyParser,
84
+ // The highlight plugin only gives us the block content node, so we can only
85
+ // reconstruct the block's `type` and `props` (which is all a spec's
86
+ // `meta.highlight` needs to pick a language).
87
+ languageExtractor: (node) => {
88
+ const nodeShape = {
89
+ type: node.type.name,
90
+ props: node.attrs,
91
+ };
92
+ // search for the node in the blockSpec or inlineContentSpecs
93
+ const spec =
94
+ schema.blockSpecs[nodeShape.type] ||
95
+ schema.inlineContentSpecs[nodeShape.type];
96
+
97
+ return spec?.implementation?.meta?.highlight?.(nodeShape) ?? undefined;
98
+ },
99
+ nodeTypes,
100
+ });
101
+ }
102
+
103
+ // If a light and dark theme is added to the highlighter, this function specifies them in
104
+ // `createParser`. This lets us use `--shiki-light` and `--shiki-dark` CSS variables for correct
105
+ // styling for both light & dark editor themes.
106
+ function pickThemeOptions(highlighter: HighlighterGeneric<any, any>) {
107
+ const themes = highlighter.getLoadedThemes();
108
+ const light = themes.find((t) => /light/i.test(t));
109
+ const dark = themes.find((t) => /dark/i.test(t));
110
+
111
+ if (light && dark) {
112
+ return { themes: { light, dark }, defaultColor: false as const };
113
+ }
114
+
115
+ return undefined;
116
+ }