@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
@@ -1,7 +1,11 @@
1
1
  /** Define the main block types **/
2
2
  // import { Extension, Node } from "@tiptap/core";
3
3
  import type { Node, NodeViewRendererProps } from "@tiptap/core";
4
- import type { Fragment, Schema } from "prosemirror-model";
4
+ import type {
5
+ Fragment,
6
+ Node as ProsemirrorNode,
7
+ Schema,
8
+ } from "prosemirror-model";
5
9
  import type { ViewMutationRecord } from "prosemirror-view";
6
10
  import type { BlockNoteEditor } from "../../editor/BlockNoteEditor.js";
7
11
  import type {
@@ -28,7 +32,10 @@ export type BlockNoteDOMAttributes = Partial<{
28
32
  [DOMElement in BlockNoteDOMElement]: Record<string, string>;
29
33
  }>;
30
34
 
31
- export interface BlockConfigMeta {
35
+ export interface BlockConfigMeta<
36
+ TName extends string = string,
37
+ TProps extends PropSchema = PropSchema,
38
+ > {
32
39
  /**
33
40
  * Defines which keyboard shortcut should be used to insert a hard break into the block's inline content.
34
41
  * @default "shift+enter"
@@ -59,6 +66,18 @@ export interface BlockConfigMeta {
59
66
  * Whether the block is a {@link https://prosemirror.net/docs/ref/#model.NodeSpec.isolating} block
60
67
  */
61
68
  isolating?: boolean;
69
+
70
+ /**
71
+ * Enables syntax highlighting of the contents of the block with the result of this callback
72
+ */
73
+ highlight?(block: { type: TName; props: Props<TProps> }): string | undefined;
74
+
75
+ /**
76
+ * Marks the block as rendering a preview with an editable source popup, driven
77
+ * by the editor-wide `SourceBlockWithPreviewExtension`. When `true`, the
78
+ * block's source is hidden behind its preview and edited via the popup.
79
+ */
80
+ hasPreview?: boolean;
62
81
  }
63
82
 
64
83
  /**
@@ -209,6 +228,7 @@ export type LooseBlockSpec<
209
228
  dom: HTMLElement | DocumentFragment;
210
229
  contentDOM?: HTMLElement;
211
230
  ignoreMutation?: (mutation: ViewMutationRecord) => boolean;
231
+ update?: (node: ProsemirrorNode) => boolean;
212
232
  destroy?: () => void;
213
233
  };
214
234
  toExternalHTML?: (
@@ -267,6 +287,7 @@ export type BlockSpecs = {
267
287
  dom: HTMLElement | DocumentFragment;
268
288
  contentDOM?: HTMLElement;
269
289
  ignoreMutation?: (mutation: ViewMutationRecord) => boolean;
290
+ update?: (node: ProsemirrorNode) => boolean;
270
291
  destroy?: () => void;
271
292
  };
272
293
  toExternalHTML?: (
@@ -354,6 +375,24 @@ export type PartialPlainContent =
354
375
  | string
355
376
  | (string | (StyledText<{}> & { styles: Record<string, never> }))[];
356
377
 
378
+ /**
379
+ * The text of a block's `"plain"` content (e.g. a code block's source code).
380
+ * Accepts the partial form too: block render/export paths can receive
381
+ * `PartialBlock`s (e.g. the HTML serializers take them directly), where
382
+ * plain content may still be the bare-string sugar.
383
+ */
384
+ export function plainContentToString(
385
+ content: PlainContent | PartialPlainContent,
386
+ ): string {
387
+ if (typeof content === "string") {
388
+ return content;
389
+ }
390
+
391
+ return content
392
+ .map((item) => (typeof item === "string" ? item : item.text))
393
+ .join("");
394
+ }
395
+
357
396
  // A BlockConfig has all the information to get the type of a Block (which is a specific instance of the BlockConfig.
358
397
  // i.e.: paragraphConfig: BlockConfig defines what a "paragraph" is / supports, and BlockFromConfigNoChildren<paragraphConfig> is the shape of a specific paragraph block.
359
398
  // (for internal use)
@@ -518,7 +557,7 @@ export type BlockImplementation<
518
557
  /**
519
558
  * Metadata
520
559
  */
521
- meta?: BlockConfigMeta;
560
+ meta?: BlockConfigMeta<TName, TProps>;
522
561
  /**
523
562
  * A function that converts the block into a DOM element
524
563
  */
@@ -552,6 +591,17 @@ export type BlockImplementation<
552
591
  dom: HTMLElement | DocumentFragment;
553
592
  contentDOM?: HTMLElement;
554
593
  ignoreMutation?: (mutation: ViewMutationRecord) => boolean;
594
+ /**
595
+ * Called by ProseMirror when this block's node is updated (e.g. its content
596
+ * or props change). Return `true` to handle the update in place - keeping
597
+ * the existing DOM - or `false` to have the node view recreated via
598
+ * `render`. When omitted, ProseMirror keeps the node view and reconciles its
599
+ * `contentDOM` in place as long as the node type stays the same.
600
+ *
601
+ * Useful for blocks whose `render` builds custom DOM that needs to stay in
602
+ * sync with the node (e.g. a code block rendering a preview of its content).
603
+ */
604
+ update?: (node: ProsemirrorNode) => boolean;
555
605
  destroy?: () => void;
556
606
  };
557
607
 
@@ -24,7 +24,7 @@ import type { StyleSchema } from "./styles/types.js";
24
24
  * `undefined`.
25
25
  *
26
26
  * The assertions are the type annotations and `@ts-expect-error` directives: if
27
- * propagation breaks, this file stops compiling, which `vp lint` / `tsgo`
27
+ * propagation breaks, this file stops compiling, which `vp lint` / `tsc`
28
28
  * catches in CI (note that `vp test`, which strips types via esbuild, does not
29
29
  * — these tests are guarded by the type-checker, not the runner). The `it`
30
30
  * bodies otherwise contain no meaningful runtime logic, mirroring the existing
@@ -0,0 +1,161 @@
1
+ import { describe, expect, it } from "vite-plus/test";
2
+
3
+ import { BlockNoteSchema } from "../../blocks/BlockNoteSchema.js";
4
+ import {
5
+ defaultBlockSpecs,
6
+ defaultInlineContentSpecs,
7
+ } from "../../blocks/defaultBlocks.js";
8
+ import { BlockNoteEditor } from "../../editor/BlockNoteEditor.js";
9
+ import { YAttributionMarksExtension } from "../../y/extensions/YAttributionMarks.js";
10
+ import { createInlineContentSpec } from "./createSpec.js";
11
+
12
+ // A minimal "plain" inline content, matched from external HTML by a `parse`
13
+ // function (the `tag: "*"` parse rule).
14
+ const customPlainIC = createInlineContentSpec(
15
+ {
16
+ type: "customPlainIC",
17
+ propSchema: {},
18
+ content: "plain",
19
+ },
20
+ {
21
+ parse: (el) => (el.classList?.contains("custom-plain-ic") ? {} : undefined),
22
+ render: () => {
23
+ const dom = document.createElement("span");
24
+ const contentDOM = document.createElement("span");
25
+ dom.append(contentDOM);
26
+ return { dom, contentDOM };
27
+ },
28
+ },
29
+ );
30
+
31
+ const createEditor = () =>
32
+ BlockNoteEditor.create({
33
+ schema: BlockNoteSchema.create({
34
+ blockSpecs: defaultBlockSpecs,
35
+ inlineContentSpecs: {
36
+ ...defaultInlineContentSpecs,
37
+ customPlainIC,
38
+ },
39
+ }),
40
+ });
41
+
42
+ describe("plain inline content", () => {
43
+ it("holds its text as a string in the block model", () => {
44
+ const editor = createEditor();
45
+
46
+ editor.replaceBlocks(editor.document, [
47
+ {
48
+ type: "paragraph",
49
+ content: [
50
+ {
51
+ type: "customPlainIC",
52
+ props: {},
53
+ content: "hello world",
54
+ } as any,
55
+ ],
56
+ },
57
+ ]);
58
+
59
+ const inlineContent = (editor.document[0] as any).content[0];
60
+ expect(inlineContent.type).toBe("customPlainIC");
61
+ expect(inlineContent.content).toBe("hello world");
62
+
63
+ editor._tiptapEditor.destroy();
64
+ });
65
+
66
+ it("round-trips a newline in its string content", () => {
67
+ const editor = createEditor();
68
+
69
+ // Plain content holds raw text, so newlines are kept as characters rather
70
+ // than being split into (disallowed) `hardBreak` nodes.
71
+ editor.replaceBlocks(editor.document, [
72
+ {
73
+ type: "paragraph",
74
+ content: [
75
+ {
76
+ type: "customPlainIC",
77
+ props: {},
78
+ content: "first\nsecond",
79
+ } as any,
80
+ ],
81
+ },
82
+ ]);
83
+
84
+ const inlineContent = (editor.document[0] as any).content[0];
85
+ expect(inlineContent.type).toBe("customPlainIC");
86
+ expect(inlineContent.content).toBe("first\nsecond");
87
+
88
+ editor._tiptapEditor.destroy();
89
+ });
90
+
91
+ it("keeps text and drops formatting marks when parsed", () => {
92
+ const editor = createEditor();
93
+
94
+ const blocks = editor.tryParseHTMLToBlocks(
95
+ `<p><span class="custom-plain-ic">hello <b>world</b></span></p>`,
96
+ );
97
+
98
+ const inlineContent = (blocks[0] as any).content[0];
99
+ expect(inlineContent.type).toBe("customPlainIC");
100
+ expect(inlineContent.content).toBe("hello world");
101
+
102
+ editor._tiptapEditor.destroy();
103
+ });
104
+
105
+ it("converts line breaks to newline characters when parsed", () => {
106
+ const editor = createEditor();
107
+
108
+ const blocks = editor.tryParseHTMLToBlocks(
109
+ `<p><span class="custom-plain-ic">first<br>second</span></p>`,
110
+ );
111
+
112
+ const inlineContent = (blocks[0] as any).content[0];
113
+ expect(inlineContent.type).toBe("customPlainIC");
114
+ expect(inlineContent.content).toBe("first\nsecond");
115
+
116
+ editor._tiptapEditor.destroy();
117
+ });
118
+
119
+ it("disallows formatting marks but allows annotation marks", () => {
120
+ // Checked at the ProseMirror level because the block model intentionally
121
+ // represents plain content as a bare string, without marks.
122
+ //
123
+ // Every non-formatting mark comes from an optional extension. The Yjs
124
+ // attribution marks are the ones reachable from core, so they stand in for
125
+ // the `"annotation"` group here; register them so the group is non-empty
126
+ // (mirroring the "plain" block test).
127
+ const editor = BlockNoteEditor.create({
128
+ schema: BlockNoteSchema.create({
129
+ blockSpecs: defaultBlockSpecs,
130
+ inlineContentSpecs: {
131
+ ...defaultInlineContentSpecs,
132
+ customPlainIC,
133
+ },
134
+ }),
135
+ extensions: [YAttributionMarksExtension()],
136
+ });
137
+
138
+ const plainType = editor.pmSchema.nodes["customPlainIC"];
139
+ const insertMark = editor.pmSchema.marks["y-attributed-insert"];
140
+ const boldMark = editor.pmSchema.marks["bold"];
141
+
142
+ // The plain inline content allows the non-formatting mark but not the
143
+ // formatting one.
144
+ expect(plainType.allowsMarkType(insertMark)).toBe(true);
145
+ expect(plainType.allowsMarkType(boldMark)).toBe(false);
146
+
147
+ // ...and so `allowedMarks` keeps the former while dropping the latter.
148
+ const markNames = new Set(
149
+ plainType
150
+ .allowedMarks([
151
+ insertMark.create({ userIds: ["test-user"] }),
152
+ boldMark.create(),
153
+ ])
154
+ .map((m) => m.type.name),
155
+ );
156
+ expect(markNames.has("y-attributed-insert")).toBe(true);
157
+ expect(markNames.has("bold")).toBe(false);
158
+
159
+ editor._tiptapEditor.destroy();
160
+ });
161
+ });
@@ -1,10 +1,18 @@
1
1
  import { Node } from "@tiptap/core";
2
2
 
3
- import { TagParseRule } from "@tiptap/pm/model";
3
+ import {
4
+ DOMParser,
5
+ Fragment,
6
+ Node as ProsemirrorNode,
7
+ Schema,
8
+ TagParseRule,
9
+ } from "@tiptap/pm/model";
4
10
  import { inlineContentToNodes } from "../../api/nodeConversions/blockToNode.js";
5
11
  import { nodeToCustomInlineContent } from "../../api/nodeConversions/nodeToBlock.js";
6
12
  import type { BlockNoteEditor } from "../../editor/BlockNoteEditor.js";
13
+ import { ignoreNonContentMutations } from "../nodeViewMutations.js";
7
14
  import { propsToAttributes } from "../blocks/internal.js";
15
+ import { nonFormattingMarks } from "../markGroups.js";
8
16
  import { Props } from "../propTypes.js";
9
17
  import { StyleSchema } from "../styles/types.js";
10
18
  import {
@@ -25,6 +33,23 @@ export type CustomInlineContentImplementation<
25
33
  > = {
26
34
  meta?: {
27
35
  draggable?: boolean;
36
+ code?: boolean;
37
+ /**
38
+ * When {@link code} is `true`, this can syntax highlight the contents of the
39
+ * inline content with the result of this callback.
40
+ */
41
+ // Method syntax (rather than an arrow-function property) so its parameter is
42
+ // checked bivariantly, keeping a specific implementation assignable to the
43
+ // generic spec record type.
44
+ highlight?(
45
+ inlineContent: Pick<InlineContentFromConfig<T, S>, "type" | "props">,
46
+ ): string | undefined;
47
+ /**
48
+ * Marks the inline content as rendering a preview with an editable source
49
+ * popup, driven by the editor-wide
50
+ * `SourceInlineContentWithPreviewExtension`.
51
+ */
52
+ hasPreview?: boolean;
28
53
  };
29
54
 
30
55
  /**
@@ -32,6 +57,17 @@ export type CustomInlineContentImplementation<
32
57
  */
33
58
  parse?: (el: HTMLElement) => Partial<Props<T["propSchema"]>> | undefined;
34
59
 
60
+ /**
61
+ * Advanced parsing function that controls how the content within the inline
62
+ * content is parsed. This is not recommended to use, and is only useful for
63
+ * advanced use cases. Only applies to inline content with `content: "styled"`.
64
+ * Return `undefined` to fall through to the default inline content parsing.
65
+ */
66
+ parseContent?: (options: {
67
+ el: HTMLElement;
68
+ schema: Schema;
69
+ }) => Fragment | undefined;
70
+
35
71
  /**
36
72
  * Renders an inline content to DOM elements
37
73
  */
@@ -54,6 +90,16 @@ export type CustomInlineContentImplementation<
54
90
  editor: BlockNoteEditor<any, any, S>,
55
91
  // (note) if we want to fix the manual cast, we need to prevent circular references and separate block definition and render implementations
56
92
  // or allow manually passing <BSchema>, but that's not possible without passing the other generics because Typescript doesn't support partial inferred generics
93
+ /**
94
+ * The ProseMirror node backing this inline content.
95
+ */
96
+ node: ProsemirrorNode,
97
+ /**
98
+ * Returns this inline content's position in the document. When rendered
99
+ * outside the editor (i.e. serialized to HTML), this is a no-op that returns
100
+ * `undefined`.
101
+ */
102
+ getPos: () => number | undefined,
57
103
  ) => {
58
104
  dom: HTMLElement;
59
105
  contentDOM?: HTMLElement;
@@ -85,22 +131,98 @@ export type CustomInlineContentImplementation<
85
131
  runsBefore?: string[];
86
132
  };
87
133
 
134
+ // Resolves the element whose children hold the inline content's editable
135
+ // content, i.e. the `[data-editable]` element (or the element itself if it is /
136
+ // contains none).
137
+ function getEditableElement(element: HTMLElement) {
138
+ if (element.matches("[data-editable]")) {
139
+ return element;
140
+ }
141
+
142
+ return element.querySelector<HTMLElement>("[data-editable]") || element;
143
+ }
144
+
145
+ // Parses an element's children as inline content.
146
+ function parseInlineContent(el: HTMLElement, schema: Schema) {
147
+ return DOMParser.fromSchema(schema).parse(el, {
148
+ topNode: schema.nodes.paragraph.create(),
149
+ preserveWhitespace: true,
150
+ }).content;
151
+ }
152
+
153
+ // Flattens parsed inline content into text nodes only. "plain" inline content
154
+ // holds text only, so non-text inline nodes are flattened: line breaks become
155
+ // newline characters and other nodes (e.g. mentions) are kept as their text.
156
+ function flattenToText(content: Fragment, schema: Schema) {
157
+ const textNodes: ProsemirrorNode[] = [];
158
+ content.forEach((child) => {
159
+ if (child.isText) {
160
+ textNodes.push(child);
161
+ } else {
162
+ const text =
163
+ child.type === schema.linebreakReplacement ? "\n" : child.textContent;
164
+ if (text) {
165
+ textNodes.push(schema.text(text, child.marks));
166
+ }
167
+ }
168
+ });
169
+
170
+ return Fragment.fromArray(textNodes);
171
+ }
172
+
88
173
  export function getInlineContentParseRules<C extends CustomInlineContentConfig>(
89
174
  config: C,
90
175
  customParseFunction?: CustomInlineContentImplementation<C, any>["parse"],
176
+ customParseContentFunction?: CustomInlineContentImplementation<
177
+ C,
178
+ any
179
+ >["parseContent"],
91
180
  ) {
181
+ // When a custom `parseContent` function is provided (and this inline content
182
+ // actually holds content), it controls how content within the inline content
183
+ // is parsed. This applies to _both_ parse rules below, as content copied from
184
+ // within the editor is tagged with `data-inline-content-type` (matched by the
185
+ // first rule), while content pasted from outside is matched by the custom
186
+ // `parse` function (the second rule). `resolveContentElement` locates the
187
+ // element whose children to parse as a fallback when `parseContent` returns
188
+ // `undefined`.
189
+ // "plain" inline content always needs `getContent` so its parsed content is
190
+ // flattened to text (`<br>`/line breaks become newline characters), regardless
191
+ // of whether a custom `parseContent` is provided — mirroring "plain" blocks.
192
+ // "styled" inline content only needs it to run a custom `parseContent`.
193
+ const getContent =
194
+ config.content === "plain" ||
195
+ (customParseContentFunction && config.content === "styled")
196
+ ? (resolveContentElement: (el: HTMLElement) => HTMLElement) =>
197
+ (node: HTMLElement, schema: Schema) => {
198
+ const result = customParseContentFunction?.({ el: node, schema });
199
+
200
+ // `parseContent` may return `undefined` to fall through to the
201
+ // default inline content parsing.
202
+ if (result !== undefined) {
203
+ return config.content === "plain"
204
+ ? flattenToText(result, schema)
205
+ : result;
206
+ }
207
+
208
+ const parsed = parseInlineContent(
209
+ resolveContentElement(node),
210
+ schema,
211
+ );
212
+ return config.content === "plain"
213
+ ? flattenToText(parsed, schema)
214
+ : parsed;
215
+ }
216
+ : undefined;
217
+
92
218
  const rules: TagParseRule[] = [
93
219
  {
94
220
  tag: `[data-inline-content-type="${config.type}"]`,
95
- contentElement: (element) => {
96
- const htmlElement = element as HTMLElement;
97
-
98
- if (htmlElement.matches("[data-editable]")) {
99
- return htmlElement;
100
- }
101
-
102
- return htmlElement.querySelector("[data-editable]") || htmlElement;
103
- },
221
+ contentElement: (element) => getEditableElement(element as HTMLElement),
222
+ getContent: getContent
223
+ ? (node, schema) =>
224
+ getContent(getEditableElement)(node as HTMLElement, schema)
225
+ : undefined,
104
226
  },
105
227
  ];
106
228
 
@@ -120,6 +242,12 @@ export function getInlineContentParseRules<C extends CustomInlineContentConfig>(
120
242
 
121
243
  return props;
122
244
  },
245
+ // Because we do the parsing ourselves, we want to preserve whitespace for
246
+ // content we've parsed.
247
+ preserveWhitespace: getContent ? true : undefined,
248
+ getContent: getContent
249
+ ? (node, schema) => getContent((el) => el)(node as HTMLElement, schema)
250
+ : undefined,
123
251
  });
124
252
  }
125
253
  return rules;
@@ -137,9 +265,27 @@ export function createInlineContentSpec<
137
265
  inline: true,
138
266
  group: "inline",
139
267
  draggable: inlineContentImplementation.meta?.draggable,
140
- selectable: inlineContentConfig.content === "styled",
268
+ selectable: inlineContentConfig.content !== "none",
141
269
  atom: inlineContentConfig.content === "none",
142
- content: inlineContentConfig.content === "styled" ? "inline*" : "",
270
+ code: inlineContentImplementation.meta?.code,
271
+ content:
272
+ inlineContentConfig.content === "styled"
273
+ ? "inline*"
274
+ : inlineContentConfig.content === "plain"
275
+ ? "text*"
276
+ : "",
277
+ // "plain" inline content holds unstyled text, so it disallows formatting
278
+ // marks (mirroring "plain" blocks). It still allows the non-formatting marks
279
+ // (comments and suggestions/diffs), which annotate content without changing
280
+ // it and are ignored by the content model. `nonFormattingMarks` resolves the
281
+ // group only when at least one such mark is registered, so a plain inline
282
+ // content in an editor without any of them doesn't reference an empty
283
+ // (unknown) mark group.
284
+ marks() {
285
+ return inlineContentConfig.content === "plain"
286
+ ? nonFormattingMarks(this.editor)
287
+ : undefined;
288
+ },
143
289
 
144
290
  addAttributes() {
145
291
  return propsToAttributes(inlineContentConfig.propSchema);
@@ -153,6 +299,7 @@ export function createInlineContentSpec<
153
299
  return getInlineContentParseRules(
154
300
  inlineContentConfig,
155
301
  inlineContentImplementation.parse,
302
+ inlineContentImplementation.parseContent,
156
303
  );
157
304
  },
158
305
 
@@ -170,6 +317,8 @@ export function createInlineContentSpec<
170
317
  // No-op
171
318
  },
172
319
  editor,
320
+ node,
321
+ () => undefined,
173
322
  );
174
323
 
175
324
  return addInlineContentAttributes(
@@ -206,14 +355,23 @@ export function createInlineContentSpec<
206
355
  );
207
356
  },
208
357
  editor,
358
+ node,
359
+ getPos,
209
360
  );
210
361
 
211
- return addInlineContentAttributes(
362
+ const nodeView = addInlineContentAttributes(
212
363
  output,
213
364
  inlineContentConfig.type,
214
365
  node.attrs as Props<T["propSchema"]>,
215
366
  inlineContentConfig.propSchema,
216
367
  );
368
+
369
+ // Ignores DOM mutations that don't affect the inline content, so that
370
+ // browser extensions which rewrite the DOM (e.g. Dark Reader) can't
371
+ // trigger an infinite re-render loop that freezes the tab.
372
+ ignoreNonContentMutations(nodeView);
373
+
374
+ return nodeView;
217
375
  };
218
376
  },
219
377
  });
@@ -225,10 +383,19 @@ export function createInlineContentSpec<
225
383
  ...inlineContentImplementation,
226
384
  toExternalHTML: inlineContentImplementation.toExternalHTML,
227
385
  render(inlineContent, updateInlineContent, editor) {
386
+ // Rendered outside the editor (serialization), so there's no live node
387
+ // view - derive the node from the content and stub out `getPos`.
388
+ const node = inlineContentToNodes(
389
+ [inlineContent] as any,
390
+ editor.pmSchema,
391
+ )[0];
392
+
228
393
  const output = inlineContentImplementation.render(
229
394
  inlineContent,
230
395
  updateInlineContent,
231
396
  editor,
397
+ node,
398
+ () => undefined,
232
399
  );
233
400
 
234
401
  return addInlineContentAttributes(
@@ -1,5 +1,9 @@
1
1
  import { KeyboardShortcutCommand, Node } from "@tiptap/core";
2
2
 
3
+ import {
4
+ Extension,
5
+ ExtensionFactoryInstance,
6
+ } from "../../editor/BlockNoteExtension.js";
3
7
  import { camelToDataKebab } from "../../util/string.js";
4
8
  import { PropSchema, Props } from "../propTypes.js";
5
9
  import {
@@ -77,10 +81,12 @@ export function createInternalInlineContentSpec<
77
81
  >(
78
82
  config: T,
79
83
  implementation: InlineContentImplementation<NoInfer<T>>,
84
+ extensions?: (Extension | ExtensionFactoryInstance)[],
80
85
  ): InlineContentSpec<T> {
81
86
  return {
82
87
  config,
83
88
  implementation,
89
+ extensions,
84
90
  } as const;
85
91
  }
86
92
 
@@ -99,12 +105,25 @@ export function createInlineContentSpecFromTipTapNode<
99
105
  {
100
106
  type: node.name as T["name"],
101
107
  propSchema,
102
- content: node.config.content === "inline*" ? "styled" : "none",
108
+ content:
109
+ node.config.content === "inline*"
110
+ ? "styled"
111
+ : node.config.content === "text*"
112
+ ? "plain"
113
+ : "none",
103
114
  },
115
+ // Cast needed because `implementation` is typed against the generic
116
+ // `CustomInlineContentConfig`, while `createInternalInlineContentSpec`
117
+ // expects the implementation for the specific (still-generic) config
118
+ // inferred from `node`/`propSchema` above.
104
119
  {
105
120
  ...implementation,
106
121
  node,
107
- },
122
+ } as unknown as InlineContentImplementation<{
123
+ type: T["name"];
124
+ propSchema: P;
125
+ content: "styled" | "none" | "plain";
126
+ }>,
108
127
  );
109
128
  }
110
129
 
@@ -1,8 +1,12 @@
1
1
  import { Node } from "@tiptap/core";
2
2
  import { ViewMutationRecord } from "prosemirror-view";
3
- import { BlockNoteEditor } from "../../editor/BlockNoteEditor.js";
4
3
  import { PropSchema, Props } from "../propTypes.js";
5
4
  import { StyleSchema, Styles } from "../styles/types.js";
5
+ import { BlockNoteEditor } from "../../editor/BlockNoteEditor.js";
6
+ import {
7
+ Extension,
8
+ ExtensionFactoryInstance,
9
+ } from "../../editor/BlockNoteExtension.js";
6
10
 
7
11
  export type CustomInlineContentConfig = {
8
12
  type: string;
@@ -16,11 +20,35 @@ export type InlineContentConfig = CustomInlineContentConfig | "text" | "link";
16
20
  // InlineContentImplementation contains the "implementation" info about an InlineContent element
17
21
  // such as the functions / Nodes required to render and / or serialize it
18
22
  export type InlineContentImplementation<T extends InlineContentConfig> =
19
- T extends "link" | "text"
20
- ? undefined
21
- : {
23
+ T extends CustomInlineContentConfig
24
+ ? {
22
25
  meta?: {
26
+ /**
27
+ * Whether the inline content is a {@link https://prosemirror.net/docs/ref/#model.NodeSpec.code} block
28
+ */
29
+ code?: boolean;
30
+ /**
31
+ * When {@link code} is `true`, this can syntax highlight the contents of the block with the result of this callback
32
+ */
33
+ // Method syntax (rather than an arrow-function property) so its
34
+ // parameter is checked bivariantly, keeping a specific
35
+ // implementation assignable to the generic spec record type.
36
+ highlight?(
37
+ inlineContent: Pick<
38
+ CustomInlineContentFromConfig<T, any>,
39
+ "type" | "props"
40
+ >,
41
+ ): string | undefined;
42
+ /**
43
+ * Whether the inline content is draggable
44
+ */
23
45
  draggable?: boolean;
46
+ /**
47
+ * Marks the inline content as rendering a preview with an editable
48
+ * source popup, driven by the editor-wide
49
+ * `SourceInlineContentWithPreviewExtension`.
50
+ */
51
+ hasPreview?: boolean;
24
52
  };
25
53
  node: Node;
26
54
  toExternalHTML?: (
@@ -43,7 +71,8 @@ export type InlineContentImplementation<T extends InlineContentConfig> =
43
71
  destroy?: () => void;
44
72
  };
45
73
  runsBefore?: string[];
46
- };
74
+ }
75
+ : undefined;
47
76
 
48
77
  export type InlineContentSchemaWithInlineContent<
49
78
  IType extends string,
@@ -57,6 +86,7 @@ export type InlineContentSchemaWithInlineContent<
57
86
  export type InlineContentSpec<T extends InlineContentConfig> = {
58
87
  config: T;
59
88
  implementation: InlineContentImplementation<T>;
89
+ extensions?: (Extension | ExtensionFactoryInstance)[];
60
90
  };
61
91
 
62
92
  // A Schema contains all the types (Configs) supported in an editor