@blocknote/core 0.52.1 → 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 (221) 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-CiuUGa1E.js → blocks-CzQLehlc.js} +695 -652
  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-CrUGDOks.js → extensions-DGuhtOgB.js} +717 -452
  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-DTF83Cl9.js → src-BkfZVfaO.js} +921 -835
  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 +65 -62
  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 +29 -30
  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/clipboard/fromClipboard/handleFileInsertion.ts +10 -1
  54. package/src/api/exporters/html/util/serializeBlocksExternalHTML.ts +42 -8
  55. package/src/api/exporters/markdown/htmlToMarkdown.ts +45 -10
  56. package/src/api/exporters/markdown/markdownExporter.ts +10 -1
  57. package/src/api/nodeConversions/blockToNode.ts +5 -1
  58. package/src/api/nodeConversions/nodeToBlock.ts +3 -0
  59. package/src/api/parsers/html/parseHTML.ts +43 -0
  60. package/src/api/pmUtil.ts +16 -8
  61. package/src/blocks/Code/CodeBlockOptions.ts +83 -0
  62. package/src/blocks/Code/block.test.ts +1 -1
  63. package/src/blocks/Code/block.ts +29 -281
  64. package/src/blocks/Code/helpers/extensions/CodeKeyboardShortcutsExtension.ts +120 -0
  65. package/src/blocks/Code/helpers/parse/parsePreCode.ts +45 -0
  66. package/src/blocks/Code/helpers/render/createCodeBlock.ts +98 -0
  67. package/src/blocks/Code/helpers/toExternalHTML/createPreCode.ts +14 -0
  68. package/src/blocks/Table/TableExtension.test.ts +102 -0
  69. package/src/blocks/ToggleWrapper/createToggleWrapper.ts +16 -5
  70. package/src/blocks/defaultBlockTypeGuards.ts +6 -0
  71. package/src/blocks/index.ts +3 -0
  72. package/src/comments/extension.ts +2 -4
  73. package/src/editor/Block.css +324 -3
  74. package/src/editor/BlockNoteEditor.ts +25 -4
  75. package/src/editor/BlockNoteExtension.ts +1 -1
  76. package/src/editor/managers/ExtensionManager/extensions.ts +6 -0
  77. package/src/exporter/ExportImage.ts +50 -0
  78. package/src/exporter/Exporter.test.ts +48 -0
  79. package/src/exporter/Exporter.ts +46 -12
  80. package/src/exporter/index.ts +1 -0
  81. package/src/exporter/mapping.ts +4 -1
  82. package/src/extensions/FormattingToolbar/FormattingToolbar.ts +10 -8
  83. package/src/extensions/InlineContentBoundaryEdit/InlineContentBoundaryEdit.ts +109 -0
  84. package/src/extensions/SideMenu/SideMenu.ts +20 -3
  85. package/src/extensions/SourceBlockWithPreview/SourceBlockWithPreview.ts +192 -0
  86. package/src/extensions/SourceInlineContentWithPreview/SourceInlineContentWithPreview.ts +142 -0
  87. package/src/extensions/SuggestionMenu/SuggestionMenu.test.ts +118 -0
  88. package/src/extensions/SuggestionMenu/SuggestionMenu.ts +40 -27
  89. package/src/extensions/SuggestionMenu/getDefaultSlashMenuItems.ts +13 -3
  90. package/src/extensions/SyntaxHighlighting/SyntaxHighlighting.test.ts +120 -0
  91. package/src/extensions/SyntaxHighlighting/SyntaxHighlighting.ts +89 -0
  92. package/src/extensions/SyntaxHighlighting/inlineHighlight.test.ts +135 -0
  93. package/src/extensions/SyntaxHighlighting/shiki.ts +116 -0
  94. package/src/extensions/TableHandles/TableHandles.ts +25 -12
  95. package/src/extensions/TrailingNode/TrailingNode.ts +98 -32
  96. package/src/extensions/Versioning/inMemoryVersioning.ts +13 -3
  97. package/src/extensions/index.ts +4 -0
  98. package/src/extensions/tiptap-extensions/KeyboardShortcuts/KeyboardShortcutsExtension.test.ts +51 -2
  99. package/src/extensions/tiptap-extensions/KeyboardShortcuts/KeyboardShortcutsExtension.ts +22 -2
  100. package/src/i18n/locales/ar.ts +9 -0
  101. package/src/i18n/locales/de.ts +9 -0
  102. package/src/i18n/locales/en.ts +9 -0
  103. package/src/i18n/locales/es.ts +9 -0
  104. package/src/i18n/locales/fa.ts +9 -0
  105. package/src/i18n/locales/fr.ts +9 -0
  106. package/src/i18n/locales/he.ts +9 -0
  107. package/src/i18n/locales/hr.ts +9 -0
  108. package/src/i18n/locales/is.ts +9 -0
  109. package/src/i18n/locales/it.ts +9 -0
  110. package/src/i18n/locales/ja.ts +9 -0
  111. package/src/i18n/locales/ko.ts +9 -0
  112. package/src/i18n/locales/nl.ts +9 -0
  113. package/src/i18n/locales/no.ts +9 -0
  114. package/src/i18n/locales/pl.ts +9 -0
  115. package/src/i18n/locales/pt.ts +9 -0
  116. package/src/i18n/locales/ru.ts +9 -0
  117. package/src/i18n/locales/sk.ts +9 -0
  118. package/src/i18n/locales/uk.ts +9 -0
  119. package/src/i18n/locales/uz.ts +9 -0
  120. package/src/i18n/locales/vi.ts +9 -0
  121. package/src/i18n/locales/zh-tw.ts +9 -0
  122. package/src/i18n/locales/zh.ts +9 -0
  123. package/src/index.ts +6 -1
  124. package/src/schema/blocks/createSpec.ts +17 -5
  125. package/src/schema/blocks/internal.test.ts +86 -0
  126. package/src/schema/blocks/internal.ts +64 -0
  127. package/src/schema/blocks/types.ts +53 -3
  128. package/src/schema/contentTypePropagation.test.ts +1 -1
  129. package/src/schema/inlineContent/createSpec.test.ts +161 -0
  130. package/src/schema/inlineContent/createSpec.ts +180 -13
  131. package/src/schema/inlineContent/internal.ts +21 -2
  132. package/src/schema/inlineContent/types.ts +35 -5
  133. package/src/schema/nodeViewMutations.test.ts +121 -0
  134. package/src/schema/nodeViewMutations.ts +46 -0
  135. package/src/user/UserStore.ts +1 -1
  136. package/src/util/Store.test.ts +132 -0
  137. package/src/util/Store.ts +135 -0
  138. package/src/y/extensions/AttributionExtension.test.ts +10 -0
  139. package/src/y/extensions/Versioning.test.ts +20 -7
  140. package/src/yjs/comments/RESTYjsThreadStore.test.ts +85 -0
  141. package/src/yjs/comments/RESTYjsThreadStore.ts +7 -1
  142. package/src/yjs/extensions/ForkYDoc.test.ts +37 -0
  143. package/src/yjs/extensions/ForkYDoc.ts +26 -1
  144. package/src/yjs/extensions/RelativePositionMapping.ts +8 -2
  145. package/src/yjs/extensions/Versioning.test.ts +17 -5
  146. package/types/src/api/exporters/html/util/serializeBlocksExternalHTML.d.ts +13 -0
  147. package/types/src/api/pmUtil.d.ts +7 -7
  148. package/types/src/blocks/Code/CodeBlockOptions.d.ts +61 -0
  149. package/types/src/blocks/Code/block.d.ts +3 -53
  150. package/types/src/blocks/Code/helpers/extensions/CodeKeyboardShortcutsExtension.d.ts +35 -0
  151. package/types/src/blocks/Code/helpers/parse/parsePreCode.d.ts +8 -0
  152. package/types/src/blocks/Code/helpers/render/createCodeBlock.d.ts +18 -0
  153. package/types/src/blocks/Code/helpers/toExternalHTML/createPreCode.d.ts +5 -0
  154. package/types/src/blocks/defaultBlockTypeGuards.d.ts +2 -0
  155. package/types/src/blocks/index.d.ts +3 -0
  156. package/types/src/comments/extension.d.ts +1 -15
  157. package/types/src/editor/BlockNoteEditor.d.ts +3 -3
  158. package/types/src/editor/BlockNoteExtension.d.ts +1 -1
  159. package/types/src/exporter/ExportImage.d.ts +36 -0
  160. package/types/src/exporter/Exporter.d.ts +21 -0
  161. package/types/src/exporter/Exporter.test.d.ts +1 -0
  162. package/types/src/exporter/index.d.ts +1 -0
  163. package/types/src/exporter/mapping.d.ts +1 -1
  164. package/types/src/extensions/FilePanel/FilePanel.d.ts +1 -1
  165. package/types/src/extensions/FormattingToolbar/FormattingToolbar.d.ts +1 -1
  166. package/types/src/extensions/InlineContentBoundaryEdit/InlineContentBoundaryEdit.d.ts +5 -0
  167. package/types/src/extensions/ShowSelection/ShowSelection.d.ts +1 -5
  168. package/types/src/extensions/SideMenu/SideMenu.d.ts +6 -1
  169. package/types/src/extensions/SourceBlockWithPreview/SourceBlockWithPreview.d.ts +45 -0
  170. package/types/src/extensions/SourceInlineContentWithPreview/SourceInlineContentWithPreview.d.ts +49 -0
  171. package/types/src/extensions/SuggestionMenu/SuggestionMenu.d.ts +7 -11
  172. package/types/src/extensions/SyntaxHighlighting/SyntaxHighlighting.d.ts +44 -0
  173. package/types/src/extensions/SyntaxHighlighting/SyntaxHighlighting.test.d.ts +1 -0
  174. package/types/src/extensions/SyntaxHighlighting/inlineHighlight.test.d.ts +1 -0
  175. package/types/src/extensions/SyntaxHighlighting/shiki.d.ts +13 -0
  176. package/types/src/extensions/TableHandles/TableHandles.d.ts +1 -1
  177. package/types/src/extensions/TrailingNode/TrailingNode.d.ts +5 -4
  178. package/types/src/extensions/Versioning/Versioning.d.ts +1 -35
  179. package/types/src/extensions/index.d.ts +4 -0
  180. package/types/src/i18n/locales/en.d.ts +9 -0
  181. package/types/src/i18n/locales/fa.d.ts +9 -0
  182. package/types/src/i18n/locales/sk.d.ts +9 -0
  183. package/types/src/index.d.ts +3 -1
  184. package/types/src/schema/blocks/internal.d.ts +21 -0
  185. package/types/src/schema/blocks/internal.test.d.ts +1 -0
  186. package/types/src/schema/blocks/types.d.ts +36 -3
  187. package/types/src/schema/inlineContent/createSpec.d.ts +35 -3
  188. package/types/src/schema/inlineContent/createSpec.test.d.ts +1 -0
  189. package/types/src/schema/inlineContent/internal.d.ts +3 -2
  190. package/types/src/schema/inlineContent/types.d.ts +22 -3
  191. package/types/src/schema/nodeViewMutations.d.ts +3 -0
  192. package/types/src/schema/nodeViewMutations.test.d.ts +1 -0
  193. package/types/src/user/UserStore.d.ts +1 -1
  194. package/types/src/util/Store.d.ts +45 -0
  195. package/types/src/util/Store.test.d.ts +1 -0
  196. package/types/src/y/extensions/AttributionExtension.d.ts +1 -1
  197. package/types/src/y/extensions/DiffVersioningExtension.d.ts +1 -1
  198. package/types/src/y/extensions/ForkYDoc.d.ts +1 -5
  199. package/types/src/y/extensions/index.d.ts +2 -10
  200. package/types/src/yjs/comments/RESTYjsThreadStore.test.d.ts +1 -0
  201. package/types/src/yjs/extensions/ForkYDoc.d.ts +1 -5
  202. package/types/src/yjs/extensions/index.d.ts +1 -5
  203. package/dist/BlockNoteExtension-B_IEJHS8.js +0 -38
  204. package/dist/BlockNoteExtension-B_IEJHS8.js.map +0 -1
  205. package/dist/BlockNoteExtension-M0znTzxP.cjs +0 -2
  206. package/dist/BlockNoteExtension-M0znTzxP.cjs.map +0 -1
  207. package/dist/UserStore-QbbPFj0z.js.map +0 -1
  208. package/dist/blocks-BqHv0xhc.cjs +0 -12
  209. package/dist/blocks-BqHv0xhc.cjs.map +0 -1
  210. package/dist/blocks-CiuUGa1E.js.map +0 -1
  211. package/dist/en-C5Z4Ag9_.cjs +0 -2
  212. package/dist/en-C5Z4Ag9_.cjs.map +0 -1
  213. package/dist/en-CGKXfWma.js.map +0 -1
  214. package/dist/extensions-CrUGDOks.js.map +0 -1
  215. package/dist/extensions-Ghr9QqqR.cjs +0 -52
  216. package/dist/extensions-Ghr9QqqR.cjs.map +0 -1
  217. package/dist/src-DTF83Cl9.js.map +0 -1
  218. package/dist/src-OfbaVbMw.cjs +0 -21
  219. package/dist/src-OfbaVbMw.cjs.map +0 -1
  220. package/src/blocks/Code/shiki.ts +0 -73
  221. package/types/src/blocks/Code/shiki.d.ts +0 -4
@@ -475,6 +475,48 @@ describe("Test updateBlock", () => {
475
475
  expect(getEditor().document).toMatchSnapshot();
476
476
  });
477
477
 
478
+ it("Update inline content to plain content", () => {
479
+ expect(
480
+ getEditor().transact((tr) =>
481
+ updateBlock(tr, "paragraph-0", {
482
+ type: "codeBlock",
483
+ }),
484
+ ),
485
+ ).toMatchSnapshot();
486
+
487
+ expect(getEditor().document).toMatchSnapshot();
488
+ });
489
+
490
+ it("Update styled inline content to plain content", () => {
491
+ // The plain block can't hold formatting marks, so the styling is dropped
492
+ // but the text is preserved.
493
+ expect(
494
+ getEditor().transact((tr) =>
495
+ updateBlock(tr, "paragraph-with-styled-content", {
496
+ type: "codeBlock",
497
+ }),
498
+ ),
499
+ ).toMatchSnapshot();
500
+
501
+ expect(getEditor().document).toMatchSnapshot();
502
+ });
503
+
504
+ it("Update plain content to inline content", () => {
505
+ getEditor().transact((tr) =>
506
+ updateBlock(tr, "paragraph-0", { type: "codeBlock" }),
507
+ );
508
+
509
+ expect(
510
+ getEditor().transact((tr) =>
511
+ updateBlock(tr, "paragraph-0", {
512
+ type: "paragraph",
513
+ }),
514
+ ),
515
+ ).toMatchSnapshot();
516
+
517
+ expect(getEditor().document).toMatchSnapshot();
518
+ });
519
+
478
520
  it("Update no content to empty inline content", () => {
479
521
  expect(
480
522
  getEditor().transact((tr) =>
@@ -202,14 +202,26 @@ function updateBlockContentNode<
202
202
  // Since some block types contain inline content and others don't,
203
203
  // we either need to call setNodeMarkup to just update type &
204
204
  // attributes, or replaceWith to replace the whole blockContent.
205
+ const oldContent = blockInfo.blockContent.node.content;
205
206
  if (oldNodeType.spec.content === "") {
206
207
  // keep old content, because it's empty anyway and should be compatible with
207
208
  // any newContentType
208
- } else if (newNodeType.spec.content !== oldNodeType.spec.content) {
209
- // the content type changed, replace the previous content
210
- content = [];
211
- } else {
209
+ } else if (newNodeType.spec.content === oldNodeType.spec.content) {
212
210
  // keep old content, because the content type is the same and should be compatible
211
+ } else if (newNodeType.validContent(oldContent)) {
212
+ // The content-type strings differ (e.g. "inline*" vs a plain block's
213
+ // "text*"), but the existing content is still valid for the new type
214
+ // (e.g. plain, unstyled text), so keep it rather than clearing it.
215
+ } else if (newNodeType.inlineContent && oldNodeType.inlineContent) {
216
+ // Both blocks hold text, but the existing content isn't directly valid
217
+ // for the new type (e.g. converting styled/complex inline content into a
218
+ // plain block that disallows formatting marks and inline nodes). Preserve
219
+ // the text, dropping the styling the new type can't represent.
220
+ const text = blockInfo.blockContent.node.textContent;
221
+ content = text.length > 0 ? [pmSchema.text(text)] : [];
222
+ } else {
223
+ // the content type changed and is incompatible, replace the previous content
224
+ content = [];
213
225
  }
214
226
  }
215
227
 
@@ -170,9 +170,14 @@ export async function handleFileInsertion<
170
170
 
171
171
  const blockRect = blockElement?.getBoundingClientRect();
172
172
 
173
+ const existingBlock = editor.getBlock(id);
174
+ if (!existingBlock) {
175
+ return;
176
+ }
177
+
173
178
  return insertOrUpdateBlock(
174
179
  editor,
175
- editor.getBlock(id)!,
180
+ existingBlock,
176
181
  fileBlock,
177
182
  blockRect && (blockRect.top + blockRect.bottom) / 2 > coords.top
178
183
  ? "before"
@@ -183,6 +188,10 @@ export async function handleFileInsertion<
183
188
  return;
184
189
  }
185
190
 
191
+ if (!insertedBlockId) {
192
+ return;
193
+ }
194
+
186
195
  const updateData = await editor.uploadFile(file, insertedBlockId);
187
196
 
188
197
  const updatedFileBlock =
@@ -15,6 +15,20 @@ import {
15
15
  } from "../../../nodeConversions/blockToNode.js";
16
16
  import { nodeToCustomInlineContent } from "../../../nodeConversions/nodeToBlock.js";
17
17
 
18
+ /**
19
+ * Placeholder character inserted into empty inline-content blocks when
20
+ * exporting to external HTML. An empty block serializes to an element with no
21
+ * children (e.g. `<p></p>`), which is dropped when the HTML is parsed back into
22
+ * blocks. Filling it with this character keeps the block alive through the
23
+ * round trip, and the parser strips the character again so the block ends up
24
+ * empty (see `HTMLToBlocks`).
25
+ *
26
+ * The Unicode object replacement character (U+FFFC) is used because it's a
27
+ * reserved placeholder that users don't type, so it can be safely removed on
28
+ * import without discarding legitimate content (unlike a non-breaking space).
29
+ */
30
+ export const EMPTY_BLOCK_PLACEHOLDER = "";
31
+
18
32
  function addAttributesAndRemoveClasses(element: HTMLElement) {
19
33
  // Removes all BlockNote specific class names.
20
34
  const className =
@@ -265,15 +279,35 @@ function serializeBlock<
265
279
  }
266
280
  }
267
281
 
268
- if (ret.contentDOM && block.content) {
269
- const ic = serializeInlineContentExternalHTML(
270
- editor,
271
- block.content as any, // TODO
272
- serializer,
273
- { ...options, blockType: block.type },
274
- );
282
+ if (ret.contentDOM) {
283
+ if (block.content) {
284
+ const ic = serializeInlineContentExternalHTML(
285
+ editor,
286
+ block.content as any, // TODO
287
+ serializer,
288
+ { ...options, blockType: block.type },
289
+ );
290
+
291
+ ret.contentDOM.appendChild(ic);
292
+ }
275
293
 
276
- ret.contentDOM.appendChild(ic);
294
+ // Blocks with empty inline content (e.g. an empty paragraph) serialize to
295
+ // an element with no children (e.g. `<p></p>`), which is ignored when the
296
+ // HTML is parsed back into blocks. To make these blocks survive such a
297
+ // round trip, we fill their content with a placeholder character that the
298
+ // parser strips out again (see `EMPTY_BLOCK_PLACEHOLDER`).
299
+ //
300
+ // Only applies to blocks that hold inline content: containers (columns,
301
+ // tables) fill their `contentDOM` with child blocks later on, and code
302
+ // blocks would turn the placeholder into literal content.
303
+ const blockNodeType = editor.pmSchema.nodes[block.type as any];
304
+ if (
305
+ blockNodeType?.inlineContent &&
306
+ !blockNodeType.spec.code &&
307
+ ret.contentDOM.childNodes.length === 0
308
+ ) {
309
+ ret.contentDOM.appendChild(doc.createTextNode(EMPTY_BLOCK_PLACEHOLDER));
310
+ }
277
311
  }
278
312
 
279
313
  let listType = undefined;
@@ -78,6 +78,8 @@ function serializeNode(node: Node, ctx: SerializeContext): string {
78
78
  return serializeTable(el, ctx);
79
79
  case "hr":
80
80
  return ctx.indent + "***\n\n";
81
+ case "math":
82
+ return serializeMathBlock(el, ctx);
81
83
  case "img":
82
84
  return serializeImage(el, ctx);
83
85
  case "video":
@@ -175,18 +177,41 @@ function serializeCodeBlock(el: HTMLElement, ctx: SerializeContext): string {
175
177
 
176
178
  // For empty code blocks, don't add a newline between the fences
177
179
  if (!code) {
178
- return ctx.indent + fence + language + "\n" + fence + "\n\n";
179
- }
180
+ return ctx.indent + fence + language + "\n" + ctx.indent + fence + "\n\n";
181
+ }
182
+
183
+ // Every (non-blank) line carries the indent - inside a list item or
184
+ // blockquote, an unindented line would end the parent container. Blank
185
+ // lines stay blank: they don't terminate an indented fence, and indenting
186
+ // them would add trailing whitespace.
187
+ const lines = [
188
+ fence + language,
189
+ ...(code.endsWith("\n") ? code.slice(0, -1) : code).split("\n"),
190
+ fence,
191
+ ];
192
+ return (
193
+ lines.map((line) => (line ? ctx.indent + line : line)).join("\n") + "\n\n"
194
+ );
195
+ }
180
196
 
197
+ // The LaTeX source of a MathML element, taken from the annotation KaTeX
198
+ // embeds in its output (also what external HTML parsing reads). Falls back to
199
+ // the element's text content for MathML from other sources.
200
+ function extractMathLatexSource(el: Element): string {
201
+ const annotation = el.querySelector(
202
+ 'annotation[encoding="application/x-tex"]',
203
+ );
204
+ return (annotation?.textContent ?? el.textContent ?? "").trim();
205
+ }
206
+
207
+ function serializeMathBlock(el: HTMLElement, ctx: SerializeContext): string {
208
+ const latex = extractMathLatexSource(el);
209
+ // Every (non-blank) line carries the indent - inside a list item or
210
+ // blockquote, an unindented line would end the parent container.
181
211
  return (
182
- ctx.indent +
183
- fence +
184
- language +
185
- "\n" +
186
- code +
187
- (code.endsWith("\n") ? "" : "\n") +
188
- fence +
189
- "\n\n"
212
+ ["$$", ...latex.split("\n"), "$$"]
213
+ .map((line) => (line ? ctx.indent + line : line))
214
+ .join("\n") + "\n\n"
190
215
  );
191
216
  }
192
217
 
@@ -775,6 +800,16 @@ function serializeInlineContent(el: Element): string {
775
800
  case "br":
776
801
  result += "\\\n";
777
802
  break;
803
+ case "math": {
804
+ // Inline math — emit its LaTeX source as a math span. Collapsed to
805
+ // a single line, as $...$ spans cannot contain newlines.
806
+ const latex = extractMathLatexSource(childEl)
807
+ .split("\n")
808
+ .map((line) => line.trim())
809
+ .join(" ");
810
+ result += `$${latex}$`;
811
+ break;
812
+ }
778
813
  case "span":
779
814
  // Color spans, etc. — strip the tag, keep content
780
815
  result += serializeInlineContent(childEl);
@@ -8,11 +8,20 @@ import {
8
8
  StyleSchema,
9
9
  } from "../../../schema/index.js";
10
10
  import { createExternalHTMLExporter } from "../html/externalHTMLExporter.js";
11
+ import { EMPTY_BLOCK_PLACEHOLDER } from "../html/util/serializeBlocksExternalHTML.js";
11
12
  import { htmlToMarkdown } from "./htmlToMarkdown.js";
12
13
 
13
14
  // Needs to be sync because it's used in drag handler event (SideMenuPlugin)
14
15
  export function cleanHTMLToMarkdown(cleanHTMLString: string) {
15
- return htmlToMarkdown(cleanHTMLString);
16
+ // The external HTML exporter fills empty inline-content blocks with a
17
+ // placeholder character so they survive an HTML round trip (see
18
+ // `EMPTY_BLOCK_PLACEHOLDER`). Markdown has no need for that placeholder, so we
19
+ // remove it to avoid it showing up as a stray character in the output.
20
+ const withoutPlaceholder = cleanHTMLString
21
+ .split(EMPTY_BLOCK_PLACEHOLDER)
22
+ .join("");
23
+
24
+ return htmlToMarkdown(withoutPlaceholder);
16
25
  }
17
26
 
18
27
  export function blocksToMarkdown<
@@ -64,7 +64,11 @@ function styledTextToNodes<T extends StyleSchema>(
64
64
  ? [...schema.nodes[blockType].allowedMarks(marks)]
65
65
  : marks;
66
66
 
67
- const parseHardBreaks = !blockType || !schema.nodes[blockType].spec.code;
67
+ // Plain content nodes hold raw text — including newlines —
68
+ // rather than inline content, so they can't contain `hardBreak` nodes. Keep
69
+ // newlines as text characters for them instead of splitting into hard breaks.
70
+ const parseHardBreaks =
71
+ !blockType || !isPlainContentNodeType(schema, schema.nodes[blockType]);
68
72
 
69
73
  if (!parseHardBreaks) {
70
74
  return styledText.text.length > 0
@@ -373,6 +373,9 @@ export function nodeToCustomInlineContent<
373
373
  inlineContentSchema,
374
374
  styleSchema,
375
375
  ) as any; // TODO: is this safe? could we have Links here that are undesired?
376
+ } else if (icConfig.content === "plain") {
377
+ // Plain inline content is a single unstyled string.
378
+ content = node.textContent as any;
376
379
  } else {
377
380
  content = undefined;
378
381
  }
@@ -6,10 +6,51 @@ import {
6
6
  } from "../../../schema/index.js";
7
7
 
8
8
  import { Block } from "../../../blocks/defaultBlocks.js";
9
+ import { EMPTY_BLOCK_PLACEHOLDER } from "../../exporters/html/util/serializeBlocksExternalHTML.js";
9
10
  import { nodeToBlock } from "../../nodeConversions/nodeToBlock.js";
10
11
  import { nestedListsToBlockNoteStructure } from "./util/nestedLists.js";
11
12
  import { preprocessHTMLWhitespace } from "./util/normalizeWhitespace.js";
12
13
 
14
+ /**
15
+ * Removes the placeholder character that the external HTML exporter inserts
16
+ * into empty inline-content blocks (see `EMPTY_BLOCK_PLACEHOLDER`). The
17
+ * placeholder keeps such blocks from being dropped while the HTML is parsed;
18
+ * stripping it here lets the block round trip back to genuinely empty content.
19
+ */
20
+ function stripEmptyBlockPlaceholder(content: any[]): any[] {
21
+ const stripped: any[] = [];
22
+
23
+ for (const item of content) {
24
+ if (item.type === "text") {
25
+ const text = item.text.split(EMPTY_BLOCK_PLACEHOLDER).join("");
26
+ if (text.length > 0) {
27
+ stripped.push({ ...item, text });
28
+ }
29
+ } else if (Array.isArray(item.content)) {
30
+ // Links and custom inline content that hold nested styled text.
31
+ stripped.push({
32
+ ...item,
33
+ content: stripEmptyBlockPlaceholder(item.content),
34
+ });
35
+ } else {
36
+ stripped.push(item);
37
+ }
38
+ }
39
+
40
+ return stripped;
41
+ }
42
+
43
+ function stripEmptyBlockPlaceholderFromBlocks(blocks: Block<any, any, any>[]) {
44
+ for (const block of blocks) {
45
+ if (Array.isArray(block.content)) {
46
+ (block as any).content = stripEmptyBlockPlaceholder(block.content);
47
+ }
48
+ if (block.children.length > 0) {
49
+ stripEmptyBlockPlaceholderFromBlocks(block.children);
50
+ }
51
+ }
52
+ }
53
+
13
54
  export function HTMLToBlocks<
14
55
  BSchema extends BlockSchema,
15
56
  I extends InlineContentSchema,
@@ -33,5 +74,7 @@ export function HTMLToBlocks<
33
74
  blocks.push(nodeToBlock(parentNode.child(i), parentNode));
34
75
  }
35
76
 
77
+ stripEmptyBlockPlaceholderFromBlocks(blocks);
78
+
36
79
  return blocks;
37
80
  }
package/src/api/pmUtil.ts CHANGED
@@ -54,18 +54,26 @@ export function getBlockCache(schema: Schema) {
54
54
  }
55
55
 
56
56
  /**
57
- * Whether `nodeType` is a BlockNote block whose content type is `"plain"` — i.e.
58
- * it holds unstyled text and only allows the non-formatting (`"annotation"`)
59
- * marks. Resolved semantically from the block schema (the source of truth),
60
- * reachable from `schema.cached.blockNoteEditor`.
57
+ * Whether `nodeType` is a BlockNote block or inline content whose content type
58
+ * is `"plain"` — i.e. it holds unstyled text and only allows the non-formatting
59
+ * (`"annotation"`) marks. Resolved semantically from the block / inline content
60
+ * schema (the source of truth), reachable from `schema.cached.blockNoteEditor`.
61
61
  *
62
- * Returns `false` for every non-block / structural node type (`doc`,
63
- * `blockGroup`, `text`, inline content, table sub-nodes), since those aren't
64
- * keys in the block schema.
62
+ * Returns `false` for every other node type (`doc`, `blockGroup`, `text`, table
63
+ * sub-nodes, and non-plain blocks / inline content), since those aren't plain
64
+ * content keys in either schema.
65
65
  */
66
66
  export function isPlainContentNodeType(
67
67
  schema: Schema,
68
68
  nodeType: NodeType,
69
69
  ): boolean {
70
- return getBlockSchema(schema)[nodeType.name]?.content === "plain";
70
+ if (getBlockSchema(schema)[nodeType.name]?.content === "plain") {
71
+ return true;
72
+ }
73
+
74
+ const inlineContentConfig = getInlineContentSchema(schema)[nodeType.name];
75
+ return (
76
+ typeof inlineContentConfig === "object" &&
77
+ inlineContentConfig.content === "plain"
78
+ );
71
79
  }
@@ -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