@blocknote/core 0.53.0 → 0.54.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.
Files changed (197) 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-BEJW9OM_.js → blocks-CzQLehlc.js} +571 -568
  12. package/dist/blocks-CzQLehlc.js.map +1 -0
  13. package/dist/blocks-CzkqclGj.cjs +12 -0
  14. package/dist/blocks-CzkqclGj.cjs.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-CSB9ZvuL.cjs +58 -0
  26. package/dist/extensions-CSB9ZvuL.cjs.map +1 -0
  27. package/dist/{extensions-BR5hkAio.js → extensions-DGuhtOgB.js} +662 -427
  28. package/dist/extensions-DGuhtOgB.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-BkfZVfaO.js} +967 -899
  36. package/dist/src-BkfZVfaO.js.map +1 -0
  37. package/dist/src-DM3syMdf.cjs +23 -0
  38. package/dist/src-DM3syMdf.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 +59 -56
  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 +88 -76
  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/nodeConversions/blockToNode.ts +5 -1
  55. package/src/api/nodeConversions/nodeToBlock.ts +3 -0
  56. package/src/api/pmUtil.ts +16 -8
  57. package/src/blocks/Code/CodeBlockOptions.ts +83 -0
  58. package/src/blocks/Code/block.test.ts +1 -1
  59. package/src/blocks/Code/block.ts +29 -281
  60. package/src/blocks/Code/helpers/extensions/CodeKeyboardShortcutsExtension.ts +120 -0
  61. package/src/blocks/Code/helpers/parse/parsePreCode.ts +45 -0
  62. package/src/blocks/Code/helpers/render/createCodeBlock.ts +98 -0
  63. package/src/blocks/Code/helpers/toExternalHTML/createPreCode.ts +14 -0
  64. package/src/blocks/index.ts +3 -0
  65. package/src/comments/extension.ts +2 -4
  66. package/src/editor/Block.css +273 -2
  67. package/src/editor/BlockNoteEditor.ts +25 -4
  68. package/src/editor/BlockNoteExtension.ts +1 -1
  69. package/src/editor/managers/ExtensionManager/extensions.ts +6 -0
  70. package/src/exporter/ExportImage.ts +50 -0
  71. package/src/exporter/Exporter.test.ts +48 -0
  72. package/src/exporter/Exporter.ts +46 -12
  73. package/src/exporter/index.ts +1 -0
  74. package/src/exporter/mapping.ts +4 -1
  75. package/src/extensions/FormattingToolbar/FormattingToolbar.ts +10 -8
  76. package/src/extensions/InlineContentBoundaryEdit/InlineContentBoundaryEdit.ts +109 -0
  77. package/src/extensions/SourceBlockWithPreview/SourceBlockWithPreview.ts +192 -0
  78. package/src/extensions/SourceInlineContentWithPreview/SourceInlineContentWithPreview.ts +142 -0
  79. package/src/extensions/SyntaxHighlighting/SyntaxHighlighting.test.ts +120 -0
  80. package/src/extensions/SyntaxHighlighting/SyntaxHighlighting.ts +89 -0
  81. package/src/extensions/SyntaxHighlighting/inlineHighlight.test.ts +135 -0
  82. package/src/extensions/SyntaxHighlighting/shiki.ts +116 -0
  83. package/src/extensions/Versioning/inMemoryVersioning.ts +13 -3
  84. package/src/extensions/index.ts +4 -0
  85. package/src/extensions/tiptap-extensions/KeyboardShortcuts/KeyboardShortcutsExtension.test.ts +51 -2
  86. package/src/extensions/tiptap-extensions/KeyboardShortcuts/KeyboardShortcutsExtension.ts +22 -2
  87. package/src/i18n/locales/ar.ts +9 -0
  88. package/src/i18n/locales/de.ts +9 -0
  89. package/src/i18n/locales/en.ts +9 -0
  90. package/src/i18n/locales/es.ts +9 -0
  91. package/src/i18n/locales/fa.ts +9 -0
  92. package/src/i18n/locales/fr.ts +9 -0
  93. package/src/i18n/locales/he.ts +9 -0
  94. package/src/i18n/locales/hr.ts +9 -0
  95. package/src/i18n/locales/is.ts +9 -0
  96. package/src/i18n/locales/it.ts +9 -0
  97. package/src/i18n/locales/ja.ts +9 -0
  98. package/src/i18n/locales/ko.ts +9 -0
  99. package/src/i18n/locales/nl.ts +9 -0
  100. package/src/i18n/locales/no.ts +9 -0
  101. package/src/i18n/locales/pl.ts +9 -0
  102. package/src/i18n/locales/pt.ts +9 -0
  103. package/src/i18n/locales/ru.ts +9 -0
  104. package/src/i18n/locales/sk.ts +9 -0
  105. package/src/i18n/locales/uk.ts +9 -0
  106. package/src/i18n/locales/uz.ts +9 -0
  107. package/src/i18n/locales/vi.ts +9 -0
  108. package/src/i18n/locales/zh-tw.ts +9 -0
  109. package/src/i18n/locales/zh.ts +9 -0
  110. package/src/index.ts +5 -1
  111. package/src/schema/blocks/createSpec.ts +2 -3
  112. package/src/schema/blocks/types.ts +53 -3
  113. package/src/schema/contentTypePropagation.test.ts +1 -1
  114. package/src/schema/inlineContent/createSpec.test.ts +161 -0
  115. package/src/schema/inlineContent/createSpec.ts +171 -12
  116. package/src/schema/inlineContent/internal.ts +21 -2
  117. package/src/schema/inlineContent/types.ts +35 -5
  118. package/src/user/UserStore.ts +1 -1
  119. package/src/util/Store.test.ts +132 -0
  120. package/src/util/Store.ts +135 -0
  121. package/src/y/extensions/AttributionExtension.test.ts +10 -0
  122. package/src/y/extensions/Versioning.test.ts +20 -7
  123. package/src/yjs/comments/RESTYjsThreadStore.test.ts +85 -0
  124. package/src/yjs/comments/RESTYjsThreadStore.ts +7 -1
  125. package/src/yjs/extensions/ForkYDoc.test.ts +37 -0
  126. package/src/yjs/extensions/ForkYDoc.ts +26 -1
  127. package/src/yjs/extensions/RelativePositionMapping.ts +8 -2
  128. package/src/yjs/extensions/Versioning.test.ts +17 -5
  129. package/types/src/api/pmUtil.d.ts +7 -7
  130. package/types/src/blocks/Code/CodeBlockOptions.d.ts +61 -0
  131. package/types/src/blocks/Code/block.d.ts +3 -53
  132. package/types/src/blocks/Code/helpers/extensions/CodeKeyboardShortcutsExtension.d.ts +35 -0
  133. package/types/src/blocks/Code/helpers/parse/parsePreCode.d.ts +8 -0
  134. package/types/src/blocks/Code/helpers/render/createCodeBlock.d.ts +18 -0
  135. package/types/src/blocks/Code/helpers/toExternalHTML/createPreCode.d.ts +5 -0
  136. package/types/src/blocks/index.d.ts +3 -0
  137. package/types/src/comments/extension.d.ts +1 -15
  138. package/types/src/editor/BlockNoteEditor.d.ts +3 -3
  139. package/types/src/editor/BlockNoteExtension.d.ts +1 -1
  140. package/types/src/exporter/ExportImage.d.ts +36 -0
  141. package/types/src/exporter/Exporter.d.ts +21 -0
  142. package/types/src/exporter/Exporter.test.d.ts +1 -0
  143. package/types/src/exporter/index.d.ts +1 -0
  144. package/types/src/exporter/mapping.d.ts +1 -1
  145. package/types/src/extensions/FilePanel/FilePanel.d.ts +1 -1
  146. package/types/src/extensions/FormattingToolbar/FormattingToolbar.d.ts +1 -1
  147. package/types/src/extensions/InlineContentBoundaryEdit/InlineContentBoundaryEdit.d.ts +5 -0
  148. package/types/src/extensions/ShowSelection/ShowSelection.d.ts +1 -5
  149. package/types/src/extensions/SideMenu/SideMenu.d.ts +1 -1
  150. package/types/src/extensions/SourceBlockWithPreview/SourceBlockWithPreview.d.ts +45 -0
  151. package/types/src/extensions/SourceInlineContentWithPreview/SourceInlineContentWithPreview.d.ts +49 -0
  152. package/types/src/extensions/SuggestionMenu/SuggestionMenu.d.ts +1 -11
  153. package/types/src/extensions/SyntaxHighlighting/SyntaxHighlighting.d.ts +44 -0
  154. package/types/src/extensions/SyntaxHighlighting/SyntaxHighlighting.test.d.ts +1 -0
  155. package/types/src/extensions/SyntaxHighlighting/inlineHighlight.test.d.ts +1 -0
  156. package/types/src/extensions/SyntaxHighlighting/shiki.d.ts +13 -0
  157. package/types/src/extensions/TableHandles/TableHandles.d.ts +1 -1
  158. package/types/src/extensions/Versioning/Versioning.d.ts +1 -35
  159. package/types/src/extensions/index.d.ts +4 -0
  160. package/types/src/i18n/locales/en.d.ts +9 -0
  161. package/types/src/i18n/locales/fa.d.ts +9 -0
  162. package/types/src/i18n/locales/sk.d.ts +9 -0
  163. package/types/src/index.d.ts +2 -1
  164. package/types/src/schema/blocks/types.d.ts +36 -3
  165. package/types/src/schema/inlineContent/createSpec.d.ts +35 -3
  166. package/types/src/schema/inlineContent/createSpec.test.d.ts +1 -0
  167. package/types/src/schema/inlineContent/internal.d.ts +3 -2
  168. package/types/src/schema/inlineContent/types.d.ts +22 -3
  169. package/types/src/user/UserStore.d.ts +1 -1
  170. package/types/src/util/Store.d.ts +45 -0
  171. package/types/src/util/Store.test.d.ts +1 -0
  172. package/types/src/y/extensions/AttributionExtension.d.ts +1 -1
  173. package/types/src/y/extensions/DiffVersioningExtension.d.ts +1 -1
  174. package/types/src/y/extensions/ForkYDoc.d.ts +1 -5
  175. package/types/src/y/extensions/index.d.ts +2 -10
  176. package/types/src/yjs/comments/RESTYjsThreadStore.test.d.ts +1 -0
  177. package/types/src/yjs/extensions/ForkYDoc.d.ts +1 -5
  178. package/types/src/yjs/extensions/index.d.ts +1 -5
  179. package/dist/BlockNoteExtension-B_IEJHS8.js +0 -38
  180. package/dist/BlockNoteExtension-B_IEJHS8.js.map +0 -1
  181. package/dist/BlockNoteExtension-M0znTzxP.cjs +0 -2
  182. package/dist/BlockNoteExtension-M0znTzxP.cjs.map +0 -1
  183. package/dist/UserStore-QbbPFj0z.js.map +0 -1
  184. package/dist/blocks-BEJW9OM_.js.map +0 -1
  185. package/dist/blocks-BPmJglFo.cjs +0 -12
  186. package/dist/blocks-BPmJglFo.cjs.map +0 -1
  187. package/dist/en-C5Z4Ag9_.cjs +0 -2
  188. package/dist/en-C5Z4Ag9_.cjs.map +0 -1
  189. package/dist/en-CGKXfWma.js.map +0 -1
  190. package/dist/extensions-AitWbYdJ.cjs +0 -52
  191. package/dist/extensions-AitWbYdJ.cjs.map +0 -1
  192. package/dist/extensions-BR5hkAio.js.map +0 -1
  193. package/dist/src-BHPuOo7W.cjs +0 -21
  194. package/dist/src-BHPuOo7W.cjs.map +0 -1
  195. package/dist/src-BUXut68d.js.map +0 -1
  196. package/src/blocks/Code/shiki.ts +0 -73
  197. package/types/src/blocks/Code/shiki.d.ts +0 -4
@@ -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
+ }
@@ -136,13 +136,23 @@ export function createInMemoryVersioningEndpoints(): VersioningEndpoints<
136
136
  const contents = new Map<string, Block<any, any, any>[]>();
137
137
  let nextId = 1;
138
138
 
139
+ // `Date.now()` only has millisecond resolution, so two snapshots created in
140
+ // the same tick would share a timestamp and `sortSnapshotsNewestFirst` (which
141
+ // has nothing else to order on) could list them oldest-first. Hand out
142
+ // strictly increasing timestamps so creation order is always preserved.
143
+ let lastTimestamp = 0;
144
+ function nextTimestamp() {
145
+ lastTimestamp = Math.max(Date.now(), lastTimestamp + 1);
146
+ return lastTimestamp;
147
+ }
148
+
139
149
  return {
140
150
  async list() {
141
151
  return sortSnapshotsNewestFirst([...snapshots]);
142
152
  },
143
153
 
144
154
  async create(currentDoc, options) {
145
- const now = Date.now();
155
+ const now = nextTimestamp();
146
156
  const id = String(nextId++);
147
157
  const snapshot: VersionSnapshot = {
148
158
  id,
@@ -166,7 +176,7 @@ export function createInMemoryVersioningEndpoints(): VersioningEndpoints<
166
176
 
167
177
  // Create a "Restored from …" snapshot of the current state before
168
178
  // restoring, so the user can undo the restore.
169
- const now = Date.now();
179
+ const now = nextTimestamp();
170
180
  const backupId = String(nextId++);
171
181
  const backup: VersionSnapshot = {
172
182
  id: backupId,
@@ -196,7 +206,7 @@ export function createInMemoryVersioningEndpoints(): VersioningEndpoints<
196
206
  throw new Error(`Snapshot ${String(snapshot.id)} not found`);
197
207
  }
198
208
  stored.name = name;
199
- stored.updatedAt = Date.now();
209
+ stored.updatedAt = nextTimestamp();
200
210
  },
201
211
 
202
212
  async remove(snapshot) {
@@ -3,6 +3,7 @@ export * from "./DropCursor/DropCursor.js";
3
3
  export * from "./FilePanel/FilePanel.js";
4
4
  export * from "./FormattingToolbar/FormattingToolbar.js";
5
5
  export * from "./History/History.js";
6
+ export * from "./InlineContentBoundaryEdit/InlineContentBoundaryEdit.js";
6
7
  export * from "./LinkToolbar/LinkToolbar.js";
7
8
  export * from "./LinkToolbar/protocols.js";
8
9
  export * from "./NodeSelectionKeyboard/NodeSelectionKeyboard.js";
@@ -11,7 +12,10 @@ export * from "./PositionMapping/PositionMapping.js";
11
12
  export * from "./PreviousBlockType/PreviousBlockType.js";
12
13
  export * from "./ShowSelection/ShowSelection.js";
13
14
  export * from "./SideMenu/SideMenu.js";
15
+ export * from "./SourceBlockWithPreview/SourceBlockWithPreview.js";
16
+ export * from "./SourceInlineContentWithPreview/SourceInlineContentWithPreview.js";
14
17
  export * from "./SuggestionMenu/DefaultGridSuggestionItem.js";
18
+ export * from "./SyntaxHighlighting/SyntaxHighlighting.js";
15
19
  export * from "./SuggestionMenu/DefaultSuggestionItem.js";
16
20
  export * from "./SuggestionMenu/getDefaultEmojiPickerItems.js";
17
21
  export * from "./SuggestionMenu/getDefaultSlashMenuItems.js";
@@ -18,15 +18,17 @@ import { createBlockSpec } from "../../../schema/index.js";
18
18
  const createHardBreakTestBlockSpec = <
19
19
  const T extends string,
20
20
  const S extends "shift+enter" | "enter" | "none",
21
+ const C extends "inline" | "plain",
21
22
  >(
22
23
  type: T,
23
24
  hardBreakShortcut: S,
25
+ content: C = "inline" as C,
24
26
  ) =>
25
27
  createBlockSpec(
26
28
  {
27
29
  type,
28
30
  propSchema: {},
29
- content: "inline",
31
+ content,
30
32
  },
31
33
  {
32
34
  meta: {
@@ -47,11 +49,22 @@ const schema = BlockNoteSchema.create({
47
49
  ...defaultBlockSpecs,
48
50
  hardBreakEnter: createHardBreakTestBlockSpec("hardBreakEnter", "enter"),
49
51
  hardBreakNone: createHardBreakTestBlockSpec("hardBreakNone", "none"),
52
+ // "plain" content (`text*`) can't hold a `hardBreak` node, so these blocks
53
+ // insert a literal newline character instead - e.g. code/math/diagram source.
54
+ hardBreakEnterPlain: createHardBreakTestBlockSpec(
55
+ "hardBreakEnterPlain",
56
+ "enter",
57
+ "plain",
58
+ ),
50
59
  },
51
60
  });
52
61
 
53
62
  function createEditor(
54
- blockType: "paragraph" | "hardBreakEnter" | "hardBreakNone",
63
+ blockType:
64
+ | "paragraph"
65
+ | "hardBreakEnter"
66
+ | "hardBreakNone"
67
+ | "hardBreakEnterPlain",
55
68
  ) {
56
69
  const editor = BlockNoteEditor.create({
57
70
  schema,
@@ -87,6 +100,16 @@ function countHardBreaks(editor: BlockNoteEditor<any, any, any>) {
87
100
  return count;
88
101
  }
89
102
 
103
+ function getTextContent(editor: BlockNoteEditor<any, any, any>) {
104
+ let text = "";
105
+ editor._tiptapEditor.state.doc.descendants((node) => {
106
+ if (node.isText) {
107
+ text += node.text;
108
+ }
109
+ });
110
+ return text;
111
+ }
112
+
90
113
  describe("KeyboardShortcutsExtension hardBreakShortcut", () => {
91
114
  it("inserts a hard break on Shift-Enter by default", () => {
92
115
  const editor = createEditor("paragraph");
@@ -152,4 +175,30 @@ describe("KeyboardShortcutsExtension hardBreakShortcut", () => {
152
175
 
153
176
  editor._tiptapEditor.destroy();
154
177
  });
178
+
179
+ it('inserts a newline character on Enter when content is "plain"', () => {
180
+ const editor = createEditor("hardBreakEnterPlain");
181
+
182
+ pressKeys(editor, "Enter");
183
+
184
+ // A "plain" block can't hold a `hardBreak` node, so no node is inserted and
185
+ // the block is not split - a literal newline is added to its text instead.
186
+ expect(countHardBreaks(editor)).toBe(0);
187
+ expect(editor.document.length).toBe(1);
188
+ expect(getTextContent(editor)).toBe("Hello world\n");
189
+
190
+ editor._tiptapEditor.destroy();
191
+ });
192
+
193
+ it('inserts a newline character on Shift-Enter when content is "plain"', () => {
194
+ const editor = createEditor("hardBreakEnterPlain");
195
+
196
+ pressKeys(editor, "Shift-Enter");
197
+
198
+ expect(countHardBreaks(editor)).toBe(0);
199
+ expect(editor.document.length).toBe(1);
200
+ expect(getTextContent(editor)).toBe("Hello world\n");
201
+
202
+ editor._tiptapEditor.destroy();
203
+ });
155
204
  });
@@ -804,9 +804,20 @@ export const KeyboardShortcutsExtension = Extension.create<{
804
804
  commands.command(({ state }) => {
805
805
  const blockInfo = getBlockInfoFromSelection(state);
806
806
 
807
+ const blockSpec =
808
+ this.options.editor.schema.blockSpecs[blockInfo.blockNoteType];
809
+
810
+ // NOTE: This likely doesn't work as intended - `blockSchema[type]`
811
+ // holds the block *config* (type/propSchema/content), which carries
812
+ // no `meta`, so `meta?.hardBreakShortcut` is always `undefined` and
813
+ // this falls back to the default. It should read from the block
814
+ // spec's implementation instead (i.e.
815
+ // `editor.schema.blockSpecs[type].implementation.meta`), the way the
816
+ // syntax-highlighting extension reads `meta.highlight`. Left as-is
817
+ // for a follow-up pass.
807
818
  const blockHardBreakShortcut =
808
- this.options.editor.schema.blockSpecs[blockInfo.blockNoteType]
809
- ?.implementation?.meta?.hardBreakShortcut ?? "shift+enter";
819
+ blockSpec?.implementation?.meta?.hardBreakShortcut ??
820
+ "shift+enter";
810
821
 
811
822
  if (blockHardBreakShortcut === "none") {
812
823
  return false;
@@ -820,6 +831,15 @@ export const KeyboardShortcutsExtension = Extension.create<{
820
831
  // both enter and shift+enter.
821
832
  blockHardBreakShortcut === "enter"
822
833
  ) {
834
+ // "plain" blocks (e.g. code/math/diagram source) hold text only
835
+ // (their content is `text*`), which can't contain a `hardBreak`
836
+ // node - inserting one would split the block into a new one.
837
+ // They represent line breaks as literal newline characters.
838
+ if (blockSpec?.config?.content === "plain") {
839
+ tr.insertText("\n", tr.selection.head);
840
+ return true;
841
+ }
842
+
823
843
  const marks =
824
844
  tr.storedMarks ||
825
845
  tr.selection.$head
@@ -185,6 +185,10 @@ export const ar: Dictionary = {
185
185
  toggle_blocks: {
186
186
  add_block_button: "تبديل فارغ. انقر لإضافة كتلة.",
187
187
  },
188
+ code_block: {
189
+ add_source_button_text: "إضافة كود المصدر",
190
+ ok_button_text: "موافق",
191
+ },
188
192
  // from react package:
189
193
  side_menu: {
190
194
  add_block_label: "إضافة محتوي",
@@ -395,6 +399,11 @@ export const ar: Dictionary = {
395
399
  formatting_change_by: (formats: string, users: string) =>
396
400
  `تغيير التنسيق (${formats}) بواسطة: ${users}`,
397
401
  },
402
+ exporter: {
403
+ open_file: "فتح الملف",
404
+ open_video_file: "فتح الفيديو",
405
+ open_audio_file: "فتح الصوت",
406
+ },
398
407
  generic: {
399
408
  ctrl_shortcut: "Ctrl",
400
409
  },