@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
package/src/index.ts CHANGED
@@ -23,11 +23,15 @@ export * from "./user/index.js";
23
23
  export * from "./util/browser.js";
24
24
  export * from "./util/combineByGroup.js";
25
25
  export * from "./util/expandToWords.js";
26
+ export * from "./util/Store.js";
26
27
  export * from "./util/string.js";
27
28
  export * from "./util/table.js";
28
29
  export * from "./util/typescript.js";
29
30
 
30
- export type { CodeBlockOptions } from "./blocks/Code/block.js";
31
+ export type {
32
+ CodeBlockOptions,
33
+ CodeBlockPreview,
34
+ } from "./blocks/Code/CodeBlockOptions.js";
31
35
  export { assertEmpty, UnreachableCaseError } from "./util/typescript.js";
32
36
 
33
37
  export * from "./util/EventEmitter.js";
@@ -7,12 +7,12 @@ import {
7
7
  } from "@tiptap/pm/model";
8
8
  import { NodeView } from "@tiptap/pm/view";
9
9
  import { mergeParagraphs } from "../../blocks/defaultBlockHelpers.js";
10
- import { ignoreNonContentMutations } from "../nodeViewMutations.js";
11
10
  import {
12
11
  Extension,
13
12
  ExtensionFactoryInstance,
14
13
  } from "../../editor/BlockNoteExtension.js";
15
14
  import { nonFormattingMarks } from "../markGroups.js";
15
+ import { ignoreDarkReaderMutations } from "../nodeViewMutations.js";
16
16
  import { PropSchema } from "../propTypes.js";
17
17
  import {
18
18
  getBlockFromNodeView,
@@ -279,15 +279,13 @@ export function addNodeAndExtensionsToSpec<
279
279
  applyNonSelectableBlockFix(typedNodeView, this.editor);
280
280
  }
281
281
 
282
- // Ignores DOM mutations that don't affect the block's content, so
283
- // that browser extensions which rewrite the DOM (e.g. Dark Reader)
284
- // can't trigger an infinite re-render loop that freezes the tab.
285
- ignoreNonContentMutations(typedNodeView);
282
+ // Ignores Dark Reader's rewrites of the block's DOM, which would
283
+ // otherwise trigger an infinite re-render loop that freezes the tab.
284
+ ignoreDarkReaderMutations(typedNodeView);
286
285
 
287
286
  // See explanation for why `update` is not implemented for NodeViews
288
287
  // https://github.com/TypeCellOS/BlockNote/pull/1904#discussion_r2313461464
289
- // TODO: in a future version, we might want to implement updates so that
290
- // vanilla blocks don't always re-render entirely (https://github.com/TypeCellOS/BlockNote/issues/220)
288
+ // https://github.com/TypeCellOS/BlockNote/issues/220
291
289
  return typedNodeView;
292
290
  };
293
291
  },
@@ -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,11 +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";
7
- import { ignoreNonContentMutations } from "../nodeViewMutations.js";
13
+ import { ignoreDarkReaderMutations } from "../nodeViewMutations.js";
8
14
  import { propsToAttributes } from "../blocks/internal.js";
15
+ import { nonFormattingMarks } from "../markGroups.js";
9
16
  import { Props } from "../propTypes.js";
10
17
  import { StyleSchema } from "../styles/types.js";
11
18
  import {
@@ -26,6 +33,23 @@ export type CustomInlineContentImplementation<
26
33
  > = {
27
34
  meta?: {
28
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;
29
53
  };
30
54
 
31
55
  /**
@@ -33,6 +57,17 @@ export type CustomInlineContentImplementation<
33
57
  */
34
58
  parse?: (el: HTMLElement) => Partial<Props<T["propSchema"]>> | undefined;
35
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
+
36
71
  /**
37
72
  * Renders an inline content to DOM elements
38
73
  */
@@ -55,6 +90,16 @@ export type CustomInlineContentImplementation<
55
90
  editor: BlockNoteEditor<any, any, S>,
56
91
  // (note) if we want to fix the manual cast, we need to prevent circular references and separate block definition and render implementations
57
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,
58
103
  ) => {
59
104
  dom: HTMLElement;
60
105
  contentDOM?: HTMLElement;
@@ -86,22 +131,98 @@ export type CustomInlineContentImplementation<
86
131
  runsBefore?: string[];
87
132
  };
88
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
+
89
173
  export function getInlineContentParseRules<C extends CustomInlineContentConfig>(
90
174
  config: C,
91
175
  customParseFunction?: CustomInlineContentImplementation<C, any>["parse"],
176
+ customParseContentFunction?: CustomInlineContentImplementation<
177
+ C,
178
+ any
179
+ >["parseContent"],
92
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
+
93
218
  const rules: TagParseRule[] = [
94
219
  {
95
220
  tag: `[data-inline-content-type="${config.type}"]`,
96
- contentElement: (element) => {
97
- const htmlElement = element as HTMLElement;
98
-
99
- if (htmlElement.matches("[data-editable]")) {
100
- return htmlElement;
101
- }
102
-
103
- return htmlElement.querySelector("[data-editable]") || htmlElement;
104
- },
221
+ contentElement: (element) => getEditableElement(element as HTMLElement),
222
+ getContent: getContent
223
+ ? (node, schema) =>
224
+ getContent(getEditableElement)(node as HTMLElement, schema)
225
+ : undefined,
105
226
  },
106
227
  ];
107
228
 
@@ -121,6 +242,12 @@ export function getInlineContentParseRules<C extends CustomInlineContentConfig>(
121
242
 
122
243
  return props;
123
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,
124
251
  });
125
252
  }
126
253
  return rules;
@@ -138,9 +265,27 @@ export function createInlineContentSpec<
138
265
  inline: true,
139
266
  group: "inline",
140
267
  draggable: inlineContentImplementation.meta?.draggable,
141
- selectable: inlineContentConfig.content === "styled",
268
+ selectable: inlineContentConfig.content !== "none",
142
269
  atom: inlineContentConfig.content === "none",
143
- 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
+ },
144
289
 
145
290
  addAttributes() {
146
291
  return propsToAttributes(inlineContentConfig.propSchema);
@@ -154,6 +299,7 @@ export function createInlineContentSpec<
154
299
  return getInlineContentParseRules(
155
300
  inlineContentConfig,
156
301
  inlineContentImplementation.parse,
302
+ inlineContentImplementation.parseContent,
157
303
  );
158
304
  },
159
305
 
@@ -171,6 +317,8 @@ export function createInlineContentSpec<
171
317
  // No-op
172
318
  },
173
319
  editor,
320
+ node,
321
+ () => undefined,
174
322
  );
175
323
 
176
324
  return addInlineContentAttributes(
@@ -207,6 +355,8 @@ export function createInlineContentSpec<
207
355
  );
208
356
  },
209
357
  editor,
358
+ node,
359
+ getPos,
210
360
  );
211
361
 
212
362
  const nodeView = addInlineContentAttributes(
@@ -216,10 +366,9 @@ export function createInlineContentSpec<
216
366
  inlineContentConfig.propSchema,
217
367
  );
218
368
 
219
- // Ignores DOM mutations that don't affect the inline content, so that
220
- // browser extensions which rewrite the DOM (e.g. Dark Reader) can't
221
- // trigger an infinite re-render loop that freezes the tab.
222
- ignoreNonContentMutations(nodeView);
369
+ // Ignores Dark Reader's rewrites of the inline content's DOM, which
370
+ // would otherwise trigger an infinite re-render loop that freezes the tab.
371
+ ignoreDarkReaderMutations(nodeView);
223
372
 
224
373
  return nodeView;
225
374
  };
@@ -233,10 +382,19 @@ export function createInlineContentSpec<
233
382
  ...inlineContentImplementation,
234
383
  toExternalHTML: inlineContentImplementation.toExternalHTML,
235
384
  render(inlineContent, updateInlineContent, editor) {
385
+ // Rendered outside the editor (serialization), so there's no live node
386
+ // view - derive the node from the content and stub out `getPos`.
387
+ const node = inlineContentToNodes(
388
+ [inlineContent] as any,
389
+ editor.pmSchema,
390
+ )[0];
391
+
236
392
  const output = inlineContentImplementation.render(
237
393
  inlineContent,
238
394
  updateInlineContent,
239
395
  editor,
396
+ node,
397
+ () => undefined,
240
398
  );
241
399
 
242
400
  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