@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,83 @@
1
+ import type { BlockNoteEditor } from "../../editor/BlockNoteEditor.js";
2
+ import type { BlockFromConfig } from "../../schema/index.js";
3
+
4
+ /**
5
+ * Renders a preview of a code block's content (e.g. rendered LaTeX). Takes the
6
+ * same parameters as a block's `render` function and returns the same type,
7
+ * minus `contentDOM` - as a preview never holds the block's editable content.
8
+ *
9
+ * A `renderPreview` function is only responsible for the preview itself. It has
10
+ * no opinion on when, where, or how the preview is displayed - that's up to the
11
+ * code block's `render` function.
12
+ */
13
+ export type CodeBlockPreview = (
14
+ block: BlockFromConfig<any, any, any>,
15
+ editor: BlockNoteEditor<any>,
16
+ ) => {
17
+ dom: HTMLElement | DocumentFragment;
18
+ error?: string | null;
19
+ };
20
+
21
+ export type CodeBlockOptions = {
22
+ /**
23
+ * Whether to indent lines with a tab when the user presses `Tab` in a code block.
24
+ *
25
+ * @default true
26
+ */
27
+ indentLineWithTab?: boolean;
28
+ /**
29
+ * The default language to use for code blocks.
30
+ *
31
+ * @default "text"
32
+ */
33
+ defaultLanguage?: string;
34
+ /**
35
+ * The languages that are supported in the editor.
36
+ *
37
+ * @example
38
+ * {
39
+ * javascript: {
40
+ * name: "JavaScript",
41
+ * aliases: ["js"],
42
+ * },
43
+ * typescript: {
44
+ * name: "TypeScript",
45
+ * aliases: ["ts"],
46
+ * },
47
+ * }
48
+ */
49
+ supportedLanguages?: Record<
50
+ string,
51
+ {
52
+ /**
53
+ * The display name of the language.
54
+ */
55
+ name: string;
56
+ /**
57
+ * Aliases for this language.
58
+ */
59
+ aliases?: string[];
60
+ /**
61
+ * Renders a preview of the result of the code (e.g. rendered LaTeX). When
62
+ * defined, the code block displays this preview instead of the raw source
63
+ * by default, and shows the editable source in a popup when selected.
64
+ */
65
+ createPreview?: CodeBlockPreview;
66
+ }
67
+ >;
68
+ };
69
+
70
+ export function getLanguageId(
71
+ options: CodeBlockOptions,
72
+ languageName: string,
73
+ ): string | undefined {
74
+ const normalizedLanguage = languageName.trim().toLowerCase();
75
+ return Object.entries(options.supportedLanguages ?? {}).find(
76
+ ([id, { aliases }]) => {
77
+ return (
78
+ id.toLowerCase() === normalizedLanguage ||
79
+ aliases?.some((alias) => alias.toLowerCase() === normalizedLanguage)
80
+ );
81
+ },
82
+ )?.[0];
83
+ }
@@ -8,7 +8,7 @@ import {
8
8
  } from "vite-plus/test";
9
9
  import { BlockNoteEditor } from "../../editor/BlockNoteEditor.js";
10
10
  import type { PartialBlock } from "../defaultBlocks.js";
11
- import { getLanguageId, type CodeBlockOptions } from "./block.js";
11
+ import { getLanguageId, type CodeBlockOptions } from "./CodeBlockOptions.js";
12
12
 
13
13
  /**
14
14
  * @vitest-environment jsdom
@@ -1,57 +1,14 @@
1
- import type { HighlighterGeneric } from "@shikijs/types";
2
- import { DOMParser } from "@tiptap/pm/model";
3
- import { createExtension } from "../../editor/BlockNoteExtension.js";
4
1
  import { createBlockConfig, createBlockSpec } from "../../schema/index.js";
5
- import { lazyShikiPlugin } from "./shiki.js";
2
+ import {
3
+ parsePreCode,
4
+ parsePreCodeContent,
5
+ } from "./helpers/parse/parsePreCode.js";
6
+ import { createPreCode } from "./helpers/toExternalHTML/createPreCode.js";
7
+ import { CodeKeyboardShortcutsExtension } from "./helpers/extensions/CodeKeyboardShortcutsExtension.js";
8
+ import { CodeBlockOptions } from "./CodeBlockOptions.js";
9
+ import { createCodeBlock } from "./helpers/render/createCodeBlock.js";
6
10
 
7
- export type CodeBlockOptions = {
8
- /**
9
- * Whether to indent lines with a tab when the user presses `Tab` in a code block.
10
- *
11
- * @default true
12
- */
13
- indentLineWithTab?: boolean;
14
- /**
15
- * The default language to use for code blocks.
16
- *
17
- * @default "text"
18
- */
19
- defaultLanguage?: string;
20
- /**
21
- * The languages that are supported in the editor.
22
- *
23
- * @example
24
- * {
25
- * javascript: {
26
- * name: "JavaScript",
27
- * aliases: ["js"],
28
- * },
29
- * typescript: {
30
- * name: "TypeScript",
31
- * aliases: ["ts"],
32
- * },
33
- * }
34
- */
35
- supportedLanguages?: Record<
36
- string,
37
- {
38
- /**
39
- * The display name of the language.
40
- */
41
- name: string;
42
- /**
43
- * Aliases for this language.
44
- */
45
- aliases?: string[];
46
- }
47
- >;
48
- /**
49
- * The highlighter to use for code blocks.
50
- */
51
- createHighlighter?: () => Promise<HighlighterGeneric<any, any>>;
52
- };
53
-
54
- export type CodeBlockConfig = ReturnType<typeof createCodeBlockConfig>;
11
+ const CODE_BLOCK_KEYBOARD_SHORTCUTS_KEY = "code-block-keyboard-shortcuts";
55
12
 
56
13
  export const createCodeBlockConfig = createBlockConfig(
57
14
  ({ defaultLanguage = "text" }: CodeBlockOptions) =>
@@ -66,6 +23,8 @@ export const createCodeBlockConfig = createBlockConfig(
66
23
  }) as const,
67
24
  );
68
25
 
26
+ export type CodeBlockConfig = ReturnType<typeof createCodeBlockConfig>;
27
+
69
28
  export const createCodeBlockSpec = createBlockSpec(
70
29
  createCodeBlockConfig,
71
30
  (options) => ({
@@ -73,236 +32,25 @@ export const createCodeBlockSpec = createBlockSpec(
73
32
  code: true,
74
33
  defining: true,
75
34
  isolating: false,
35
+ highlight: (block) => block.props.language,
76
36
  },
77
- parse: (e) => {
78
- if (e.tagName !== "PRE") {
79
- return undefined;
80
- }
81
-
82
- if (
83
- e.childElementCount !== 1 ||
84
- e.firstElementChild?.tagName !== "CODE"
85
- ) {
86
- return undefined;
87
- }
88
-
89
- const code = e.firstElementChild!;
90
- const language =
91
- code.getAttribute("data-language") ||
92
- code.className
93
- .split(" ")
94
- .find((name) => name.includes("language-"))
95
- ?.replace("language-", "");
96
-
97
- return { language };
98
- },
99
-
100
- parseContent: ({ el, schema }) => {
101
- const parser = DOMParser.fromSchema(schema);
102
- const code = el.firstElementChild!;
103
-
104
- return parser.parse(code, {
105
- preserveWhitespace: "full",
106
- topNode: schema.nodes["codeBlock"].create(),
107
- }).content;
108
- },
109
-
110
- render(block, editor) {
111
- const wrapper = document.createDocumentFragment();
112
- const pre = document.createElement("pre");
113
- const code = document.createElement("code");
114
- pre.appendChild(code);
115
-
116
- let removeSelectChangeListener = undefined;
117
-
118
- if (options.supportedLanguages) {
119
- const select = document.createElement("select");
120
-
121
- Object.entries(options.supportedLanguages ?? {}).forEach(
122
- ([id, { name }]) => {
123
- const option = document.createElement("option");
124
-
125
- option.value = id;
126
- option.text = name;
127
- select.appendChild(option);
128
- },
129
- );
130
- select.value =
131
- block.props.language || options.defaultLanguage || "text";
132
-
133
- if (editor.isEditable) {
134
- const handleLanguageChange = (event: Event) => {
135
- const language = (event.target as HTMLSelectElement).value;
136
-
137
- editor.updateBlock(block.id, { props: { language } });
138
- };
139
- select.addEventListener("change", handleLanguageChange);
140
- removeSelectChangeListener = () =>
141
- select.removeEventListener("change", handleLanguageChange);
142
- } else {
143
- select.disabled = true;
144
- }
145
-
146
- const selectWrapper = document.createElement("div");
147
- selectWrapper.contentEditable = "false";
148
-
149
- selectWrapper.appendChild(select);
150
- wrapper.appendChild(selectWrapper);
151
- }
152
- wrapper.appendChild(pre);
153
-
154
- return {
155
- dom: wrapper,
156
- contentDOM: code,
157
- destroy: () => {
158
- removeSelectChangeListener?.();
37
+ parse: (el) => parsePreCode(el),
38
+ parseContent: (opts) => parsePreCodeContent(opts, "codeBlock"),
39
+ render: (block, editor) =>
40
+ createCodeBlock(
41
+ block,
42
+ editor,
43
+ options.supportedLanguages && {
44
+ selectedLanguage: block.props.language,
45
+ supportedLanguages: options.supportedLanguages,
159
46
  },
160
- };
161
- },
162
- toExternalHTML(block) {
163
- const pre = document.createElement("pre");
164
- const code = document.createElement("code");
165
- code.className = `language-${block.props.language}`;
166
- code.dataset.language = block.props.language;
167
- pre.appendChild(code);
168
- return {
169
- dom: pre,
170
- contentDOM: code,
171
- };
172
- },
47
+ ),
48
+ toExternalHTML: (block) => createPreCode(block),
173
49
  }),
174
- (options) => {
175
- return [
176
- createExtension({
177
- key: "code-block-highlighter",
178
- prosemirrorPlugins: [lazyShikiPlugin(options)],
179
- }),
180
- createExtension({
181
- key: "code-block-keyboard-shortcuts",
182
- keyboardShortcuts: {
183
- Delete: ({ editor }) => {
184
- return editor.transact((tr) => {
185
- const { block } = editor.getTextCursorPosition();
186
- if (block.type !== "codeBlock") {
187
- return false;
188
- }
189
- const { $from } = tr.selection;
190
-
191
- // When inside empty codeblock, on `DELETE` key press, delete the codeblock
192
- if (!$from.parent.textContent) {
193
- editor.removeBlocks([block]);
194
-
195
- return true;
196
- }
197
-
198
- return false;
199
- });
200
- },
201
- Tab: ({ editor }) => {
202
- if (options.indentLineWithTab === false) {
203
- return false;
204
- }
205
-
206
- return editor.transact((tr) => {
207
- const { block } = editor.getTextCursorPosition();
208
- if (block.type === "codeBlock") {
209
- // TODO should probably only tab when at a line start or already tabbed in
210
- tr.insertText(" ");
211
- return true;
212
- }
213
-
214
- return false;
215
- });
216
- },
217
- Enter: ({ editor }) => {
218
- return editor.transact((tr) => {
219
- const { block, nextBlock } = editor.getTextCursorPosition();
220
- if (block.type !== "codeBlock") {
221
- return false;
222
- }
223
- const { $from } = tr.selection;
224
-
225
- const isAtEnd = $from.parentOffset === $from.parent.nodeSize - 2;
226
- const endsWithDoubleNewline =
227
- $from.parent.textContent.endsWith("\n\n");
228
-
229
- // The user is trying to exit the code block by pressing enter at the end of the code block
230
- if (isAtEnd && endsWithDoubleNewline) {
231
- // Remove the double newline
232
- tr.delete($from.pos - 2, $from.pos);
233
-
234
- // If there is a next block, move the cursor to it
235
- if (nextBlock) {
236
- editor.setTextCursorPosition(nextBlock, "start");
237
- return true;
238
- }
239
-
240
- // If there is no next block, insert a new paragraph
241
- const [newBlock] = editor.insertBlocks(
242
- [{ type: "paragraph" }],
243
- block,
244
- "after",
245
- );
246
- // Move the cursor to the new block
247
- editor.setTextCursorPosition(newBlock, "start");
248
-
249
- return true;
250
- }
251
-
252
- tr.insertText("\n");
253
- return true;
254
- });
255
- },
256
- "Shift-Enter": ({ editor }) => {
257
- return editor.transact(() => {
258
- const { block } = editor.getTextCursorPosition();
259
- if (block.type !== "codeBlock") {
260
- return false;
261
- }
262
-
263
- const [newBlock] = editor.insertBlocks(
264
- // insert a new paragraph
265
- [{ type: "paragraph" }],
266
- block,
267
- "after",
268
- );
269
- // move the cursor to the new block
270
- editor.setTextCursorPosition(newBlock, "start");
271
- return true;
272
- });
273
- },
274
- },
275
- inputRules: [
276
- {
277
- find: /^```(.*?)\s$/,
278
- replace: ({ match }) => {
279
- const languageName = match[1].trim();
280
- const attributes = {
281
- language: getLanguageId(options, languageName) ?? languageName,
282
- };
283
-
284
- return {
285
- type: "codeBlock",
286
- props: {
287
- language: attributes.language,
288
- },
289
- content: [],
290
- };
291
- },
292
- },
293
- ],
294
- }),
295
- ];
296
- },
50
+ (options) => [
51
+ CodeKeyboardShortcutsExtension(options)(
52
+ CODE_BLOCK_KEYBOARD_SHORTCUTS_KEY,
53
+ "codeBlock",
54
+ ),
55
+ ],
297
56
  );
298
-
299
- export function getLanguageId(
300
- options: CodeBlockOptions,
301
- languageName: string,
302
- ): string | undefined {
303
- return Object.entries(options.supportedLanguages ?? {}).find(
304
- ([id, { aliases }]) => {
305
- return aliases?.includes(languageName) || id === languageName;
306
- },
307
- )?.[0];
308
- }
@@ -0,0 +1,120 @@
1
+ import { createExtension } from "../../../../editor/BlockNoteExtension.js";
2
+ import { CodeBlockOptions, getLanguageId } from "../../CodeBlockOptions.js";
3
+
4
+ export const CodeKeyboardShortcutsExtension =
5
+ (options: CodeBlockOptions) => (key: string, blockType: string) =>
6
+ createExtension({
7
+ key,
8
+ keyboardShortcuts: {
9
+ Delete: ({ editor }) => {
10
+ return editor.transact((tr) => {
11
+ const { block } = editor.getTextCursorPosition();
12
+ if (block.type !== blockType) {
13
+ return false;
14
+ }
15
+ const { $from } = tr.selection;
16
+
17
+ // When inside empty codeblock, on `DELETE` key press, delete the codeblock
18
+ if (!$from.parent.textContent) {
19
+ editor.removeBlocks([block]);
20
+
21
+ return true;
22
+ }
23
+
24
+ return false;
25
+ });
26
+ },
27
+ Tab: ({ editor }) => {
28
+ if (options.indentLineWithTab === false) {
29
+ return false;
30
+ }
31
+
32
+ return editor.transact((tr) => {
33
+ const { block } = editor.getTextCursorPosition();
34
+ if (block.type === blockType) {
35
+ // TODO should probably only tab when at a line start or already tabbed in
36
+ tr.insertText(" ");
37
+ return true;
38
+ }
39
+
40
+ return false;
41
+ });
42
+ },
43
+ Enter: ({ editor }) => {
44
+ return editor.transact((tr) => {
45
+ const { block, nextBlock } = editor.getTextCursorPosition();
46
+ if (block.type !== blockType) {
47
+ return false;
48
+ }
49
+ const { $from } = tr.selection;
50
+
51
+ const isAtEnd = $from.parentOffset === $from.parent.nodeSize - 2;
52
+ const endsWithDoubleNewline =
53
+ $from.parent.textContent.endsWith("\n\n");
54
+
55
+ // The user is trying to exit the code block by pressing enter at the end of the code block
56
+ if (isAtEnd && endsWithDoubleNewline) {
57
+ // Remove the double newline
58
+ tr.delete($from.pos - 2, $from.pos);
59
+
60
+ // If there is a next block, move the cursor to it
61
+ if (nextBlock) {
62
+ editor.setTextCursorPosition(nextBlock, "start");
63
+ return true;
64
+ }
65
+
66
+ // If there is no next block, insert a new paragraph
67
+ const [newBlock] = editor.insertBlocks(
68
+ [{ type: "paragraph" }],
69
+ block,
70
+ "after",
71
+ );
72
+ // Move the cursor to the new block
73
+ editor.setTextCursorPosition(newBlock, "start");
74
+
75
+ return true;
76
+ }
77
+
78
+ tr.insertText("\n");
79
+ return true;
80
+ });
81
+ },
82
+ "Shift-Enter": ({ editor }) => {
83
+ return editor.transact(() => {
84
+ const { block } = editor.getTextCursorPosition();
85
+ if (block.type !== blockType) {
86
+ return false;
87
+ }
88
+
89
+ const [newBlock] = editor.insertBlocks(
90
+ // insert a new paragraph
91
+ [{ type: "paragraph" }],
92
+ block,
93
+ "after",
94
+ );
95
+ // move the cursor to the new block
96
+ editor.setTextCursorPosition(newBlock, "start");
97
+ return true;
98
+ });
99
+ },
100
+ },
101
+ inputRules: [
102
+ {
103
+ find: /^```(.*?)\s$/,
104
+ replace: ({ match }) => {
105
+ const languageName = match[1].trim();
106
+ const attributes = {
107
+ language: getLanguageId(options, languageName) ?? languageName,
108
+ };
109
+
110
+ return {
111
+ type: blockType,
112
+ props: {
113
+ language: attributes.language,
114
+ },
115
+ content: [],
116
+ };
117
+ },
118
+ },
119
+ ],
120
+ });
@@ -0,0 +1,45 @@
1
+ import { DOMParser, Schema } from "@tiptap/pm/model";
2
+
3
+ export const parsePreCode = (el: HTMLElement) => {
4
+ {
5
+ if (el.tagName !== "PRE") {
6
+ return undefined;
7
+ }
8
+
9
+ if (
10
+ el.childElementCount !== 1 ||
11
+ el.firstElementChild?.tagName !== "CODE"
12
+ ) {
13
+ return undefined;
14
+ }
15
+
16
+ const code = el.firstElementChild!;
17
+ const language =
18
+ code.getAttribute("data-language") ||
19
+ code.className
20
+ .split(" ")
21
+ .find((name) => name.startsWith("language-"))
22
+ ?.replace("language-", "");
23
+
24
+ return { language };
25
+ }
26
+ };
27
+
28
+ export const parsePreCodeContent = (
29
+ {
30
+ el,
31
+ schema,
32
+ }: {
33
+ el: HTMLElement;
34
+ schema: Schema;
35
+ },
36
+ blockType: string,
37
+ ) => {
38
+ const parser = DOMParser.fromSchema(schema);
39
+ const code = el.firstElementChild!;
40
+
41
+ return parser.parse(code, {
42
+ preserveWhitespace: "full",
43
+ topNode: schema.nodes[blockType].create(),
44
+ }).content;
45
+ };
@@ -0,0 +1,98 @@
1
+ import type { BlockNoteEditor } from "../../../../editor/BlockNoteEditor.js";
2
+ import type { BlockFromConfig } from "../../../../schema/index.js";
3
+
4
+ // Select dropdown to change the block's language. Assumes `block` has a `language` prop.
5
+ export const createLanguageSelect = (
6
+ block: BlockFromConfig<any, any, any>,
7
+ editor: BlockNoteEditor<any>,
8
+ selectedLanguage: string,
9
+ supportedLanguages: Record<
10
+ string,
11
+ {
12
+ name: string;
13
+ }
14
+ >,
15
+ ) => {
16
+ if (!(selectedLanguage in supportedLanguages)) {
17
+ throw new Error(`Language ${selectedLanguage} is not supported.`);
18
+ }
19
+
20
+ const select = document.createElement("select");
21
+ Object.entries(supportedLanguages).forEach(([id, { name }]) => {
22
+ const option = document.createElement("option");
23
+ option.value = id;
24
+ option.text = name;
25
+ select.appendChild(option);
26
+ });
27
+ select.value = selectedLanguage;
28
+
29
+ const handleLanguageChange = (event: Event) => {
30
+ if (!editor.isEditable) {
31
+ return;
32
+ }
33
+
34
+ editor.updateBlock(block.id, {
35
+ props: { language: (event.target as HTMLSelectElement).value },
36
+ });
37
+ };
38
+
39
+ if (editor.isEditable) {
40
+ select.addEventListener("change", handleLanguageChange);
41
+ } else {
42
+ select.disabled = true;
43
+ }
44
+
45
+ const selectWrapper = document.createElement("div");
46
+ selectWrapper.contentEditable = "false";
47
+ selectWrapper.appendChild(select);
48
+
49
+ return {
50
+ dom: selectWrapper,
51
+ destroy: () => select.removeEventListener("change", handleLanguageChange),
52
+ };
53
+ };
54
+
55
+ // Renders the block's inline content as code, alongside a language picker, if multiple languages
56
+ // are supported.
57
+ export const createCodeBlock = (
58
+ block: BlockFromConfig<any, any, any>,
59
+ editor: BlockNoteEditor<any>,
60
+ options?: {
61
+ selectedLanguage: string;
62
+ supportedLanguages: Record<
63
+ string,
64
+ {
65
+ name: string;
66
+ }
67
+ >;
68
+ },
69
+ ) => {
70
+ const pre = document.createElement("pre");
71
+ const code = document.createElement("code");
72
+ pre.appendChild(code);
73
+
74
+ const sourceBlock = document.createDocumentFragment();
75
+
76
+ let languageSelect: ReturnType<typeof createLanguageSelect> | undefined =
77
+ undefined;
78
+ if (options && Object.keys(options.supportedLanguages).length > 1) {
79
+ languageSelect = createLanguageSelect(
80
+ block,
81
+ editor,
82
+ options.selectedLanguage,
83
+ options.supportedLanguages,
84
+ );
85
+
86
+ sourceBlock.appendChild(languageSelect.dom);
87
+ }
88
+
89
+ sourceBlock.appendChild(pre);
90
+
91
+ return {
92
+ dom: sourceBlock,
93
+ contentDOM: code,
94
+ destroy: () => {
95
+ languageSelect?.destroy();
96
+ },
97
+ };
98
+ };
@@ -0,0 +1,14 @@
1
+ import type { BlockFromConfig } from "../../../../schema/index.js";
2
+
3
+ export const createPreCode = (block: BlockFromConfig<any, any, any>) => {
4
+ const pre = document.createElement("pre");
5
+ const code = document.createElement("code");
6
+ code.className = `language-${block.props.language}`;
7
+ code.dataset.language = block.props.language;
8
+ pre.appendChild(code);
9
+
10
+ return {
11
+ dom: pre,
12
+ contentDOM: code,
13
+ };
14
+ };