@blocknote/core 0.53.0 → 0.54.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (197) hide show
  1. package/dist/BlockNoteExtension-BJCLnwrE.cjs +2 -0
  2. package/dist/BlockNoteExtension-BJCLnwrE.cjs.map +1 -0
  3. package/dist/BlockNoteExtension-CbvDA0F7.js +80 -0
  4. package/dist/BlockNoteExtension-CbvDA0F7.js.map +1 -0
  5. package/dist/{UserStore-QbbPFj0z.js → UserStore-C4TzkGEn.js} +3 -3
  6. package/dist/{UserStore-DCG6lsla.cjs.map → UserStore-C4TzkGEn.js.map} +1 -1
  7. package/dist/{UserStore-DCG6lsla.cjs → UserStore-mj3fXW4D.cjs} +2 -2
  8. package/dist/UserStore-mj3fXW4D.cjs.map +1 -0
  9. package/dist/blocknote.cjs +1 -1
  10. package/dist/blocknote.js +6 -6
  11. package/dist/{blocks-BEJW9OM_.js → blocks-CzQLehlc.js} +571 -568
  12. package/dist/blocks-CzQLehlc.js.map +1 -0
  13. package/dist/blocks-CzkqclGj.cjs +12 -0
  14. package/dist/blocks-CzkqclGj.cjs.map +1 -0
  15. package/dist/blocks.cjs +1 -1
  16. package/dist/blocks.js +2 -2
  17. package/dist/comments.cjs +1 -1
  18. package/dist/comments.cjs.map +1 -1
  19. package/dist/comments.js +7 -7
  20. package/dist/comments.js.map +1 -1
  21. package/dist/en-BuzSuuTq.cjs +2 -0
  22. package/dist/en-BuzSuuTq.cjs.map +1 -0
  23. package/dist/{en-CGKXfWma.js → en-uzHzj6eH.js} +10 -1
  24. package/dist/en-uzHzj6eH.js.map +1 -0
  25. package/dist/extensions-CSB9ZvuL.cjs +58 -0
  26. package/dist/extensions-CSB9ZvuL.cjs.map +1 -0
  27. package/dist/{extensions-BR5hkAio.js → extensions-DGuhtOgB.js} +662 -427
  28. package/dist/extensions-DGuhtOgB.js.map +1 -0
  29. package/dist/extensions.cjs +1 -1
  30. package/dist/extensions.js +4 -4
  31. package/dist/locales.cjs +1 -1
  32. package/dist/locales.cjs.map +1 -1
  33. package/dist/locales.js +199 -1
  34. package/dist/locales.js.map +1 -1
  35. package/dist/{src-BUXut68d.js → src-BkfZVfaO.js} +967 -899
  36. package/dist/src-BkfZVfaO.js.map +1 -0
  37. package/dist/src-DM3syMdf.cjs +23 -0
  38. package/dist/src-DM3syMdf.cjs.map +1 -0
  39. package/dist/style.css +1 -1
  40. package/dist/webpack-stats.json +1 -1
  41. package/dist/y.cjs +1 -1
  42. package/dist/y.cjs.map +1 -1
  43. package/dist/y.js +59 -56
  44. package/dist/y.js.map +1 -1
  45. package/dist/yjs.cjs +1 -1
  46. package/dist/yjs.cjs.map +1 -1
  47. package/dist/yjs.js +88 -76
  48. package/dist/yjs.js.map +1 -1
  49. package/package.json +17 -18
  50. package/src/api/blockManipulation/commands/updateBlock/__snapshots__/updateBlock.test.ts.snap +2505 -763
  51. package/src/api/blockManipulation/commands/updateBlock/updateBlock.test.ts +42 -0
  52. package/src/api/blockManipulation/commands/updateBlock/updateBlock.ts +16 -4
  53. package/src/api/exporters/markdown/htmlToMarkdown.ts +45 -10
  54. package/src/api/nodeConversions/blockToNode.ts +5 -1
  55. package/src/api/nodeConversions/nodeToBlock.ts +3 -0
  56. package/src/api/pmUtil.ts +16 -8
  57. package/src/blocks/Code/CodeBlockOptions.ts +83 -0
  58. package/src/blocks/Code/block.test.ts +1 -1
  59. package/src/blocks/Code/block.ts +29 -281
  60. package/src/blocks/Code/helpers/extensions/CodeKeyboardShortcutsExtension.ts +120 -0
  61. package/src/blocks/Code/helpers/parse/parsePreCode.ts +45 -0
  62. package/src/blocks/Code/helpers/render/createCodeBlock.ts +98 -0
  63. package/src/blocks/Code/helpers/toExternalHTML/createPreCode.ts +14 -0
  64. package/src/blocks/index.ts +3 -0
  65. package/src/comments/extension.ts +2 -4
  66. package/src/editor/Block.css +273 -2
  67. package/src/editor/BlockNoteEditor.ts +25 -4
  68. package/src/editor/BlockNoteExtension.ts +1 -1
  69. package/src/editor/managers/ExtensionManager/extensions.ts +6 -0
  70. package/src/exporter/ExportImage.ts +50 -0
  71. package/src/exporter/Exporter.test.ts +48 -0
  72. package/src/exporter/Exporter.ts +46 -12
  73. package/src/exporter/index.ts +1 -0
  74. package/src/exporter/mapping.ts +4 -1
  75. package/src/extensions/FormattingToolbar/FormattingToolbar.ts +10 -8
  76. package/src/extensions/InlineContentBoundaryEdit/InlineContentBoundaryEdit.ts +109 -0
  77. package/src/extensions/SourceBlockWithPreview/SourceBlockWithPreview.ts +192 -0
  78. package/src/extensions/SourceInlineContentWithPreview/SourceInlineContentWithPreview.ts +142 -0
  79. package/src/extensions/SyntaxHighlighting/SyntaxHighlighting.test.ts +120 -0
  80. package/src/extensions/SyntaxHighlighting/SyntaxHighlighting.ts +89 -0
  81. package/src/extensions/SyntaxHighlighting/inlineHighlight.test.ts +135 -0
  82. package/src/extensions/SyntaxHighlighting/shiki.ts +116 -0
  83. package/src/extensions/Versioning/inMemoryVersioning.ts +13 -3
  84. package/src/extensions/index.ts +4 -0
  85. package/src/extensions/tiptap-extensions/KeyboardShortcuts/KeyboardShortcutsExtension.test.ts +51 -2
  86. package/src/extensions/tiptap-extensions/KeyboardShortcuts/KeyboardShortcutsExtension.ts +22 -2
  87. package/src/i18n/locales/ar.ts +9 -0
  88. package/src/i18n/locales/de.ts +9 -0
  89. package/src/i18n/locales/en.ts +9 -0
  90. package/src/i18n/locales/es.ts +9 -0
  91. package/src/i18n/locales/fa.ts +9 -0
  92. package/src/i18n/locales/fr.ts +9 -0
  93. package/src/i18n/locales/he.ts +9 -0
  94. package/src/i18n/locales/hr.ts +9 -0
  95. package/src/i18n/locales/is.ts +9 -0
  96. package/src/i18n/locales/it.ts +9 -0
  97. package/src/i18n/locales/ja.ts +9 -0
  98. package/src/i18n/locales/ko.ts +9 -0
  99. package/src/i18n/locales/nl.ts +9 -0
  100. package/src/i18n/locales/no.ts +9 -0
  101. package/src/i18n/locales/pl.ts +9 -0
  102. package/src/i18n/locales/pt.ts +9 -0
  103. package/src/i18n/locales/ru.ts +9 -0
  104. package/src/i18n/locales/sk.ts +9 -0
  105. package/src/i18n/locales/uk.ts +9 -0
  106. package/src/i18n/locales/uz.ts +9 -0
  107. package/src/i18n/locales/vi.ts +9 -0
  108. package/src/i18n/locales/zh-tw.ts +9 -0
  109. package/src/i18n/locales/zh.ts +9 -0
  110. package/src/index.ts +5 -1
  111. package/src/schema/blocks/createSpec.ts +2 -3
  112. package/src/schema/blocks/types.ts +53 -3
  113. package/src/schema/contentTypePropagation.test.ts +1 -1
  114. package/src/schema/inlineContent/createSpec.test.ts +161 -0
  115. package/src/schema/inlineContent/createSpec.ts +171 -12
  116. package/src/schema/inlineContent/internal.ts +21 -2
  117. package/src/schema/inlineContent/types.ts +35 -5
  118. package/src/user/UserStore.ts +1 -1
  119. package/src/util/Store.test.ts +132 -0
  120. package/src/util/Store.ts +135 -0
  121. package/src/y/extensions/AttributionExtension.test.ts +10 -0
  122. package/src/y/extensions/Versioning.test.ts +20 -7
  123. package/src/yjs/comments/RESTYjsThreadStore.test.ts +85 -0
  124. package/src/yjs/comments/RESTYjsThreadStore.ts +7 -1
  125. package/src/yjs/extensions/ForkYDoc.test.ts +37 -0
  126. package/src/yjs/extensions/ForkYDoc.ts +26 -1
  127. package/src/yjs/extensions/RelativePositionMapping.ts +8 -2
  128. package/src/yjs/extensions/Versioning.test.ts +17 -5
  129. package/types/src/api/pmUtil.d.ts +7 -7
  130. package/types/src/blocks/Code/CodeBlockOptions.d.ts +61 -0
  131. package/types/src/blocks/Code/block.d.ts +3 -53
  132. package/types/src/blocks/Code/helpers/extensions/CodeKeyboardShortcutsExtension.d.ts +35 -0
  133. package/types/src/blocks/Code/helpers/parse/parsePreCode.d.ts +8 -0
  134. package/types/src/blocks/Code/helpers/render/createCodeBlock.d.ts +18 -0
  135. package/types/src/blocks/Code/helpers/toExternalHTML/createPreCode.d.ts +5 -0
  136. package/types/src/blocks/index.d.ts +3 -0
  137. package/types/src/comments/extension.d.ts +1 -15
  138. package/types/src/editor/BlockNoteEditor.d.ts +3 -3
  139. package/types/src/editor/BlockNoteExtension.d.ts +1 -1
  140. package/types/src/exporter/ExportImage.d.ts +36 -0
  141. package/types/src/exporter/Exporter.d.ts +21 -0
  142. package/types/src/exporter/Exporter.test.d.ts +1 -0
  143. package/types/src/exporter/index.d.ts +1 -0
  144. package/types/src/exporter/mapping.d.ts +1 -1
  145. package/types/src/extensions/FilePanel/FilePanel.d.ts +1 -1
  146. package/types/src/extensions/FormattingToolbar/FormattingToolbar.d.ts +1 -1
  147. package/types/src/extensions/InlineContentBoundaryEdit/InlineContentBoundaryEdit.d.ts +5 -0
  148. package/types/src/extensions/ShowSelection/ShowSelection.d.ts +1 -5
  149. package/types/src/extensions/SideMenu/SideMenu.d.ts +1 -1
  150. package/types/src/extensions/SourceBlockWithPreview/SourceBlockWithPreview.d.ts +45 -0
  151. package/types/src/extensions/SourceInlineContentWithPreview/SourceInlineContentWithPreview.d.ts +49 -0
  152. package/types/src/extensions/SuggestionMenu/SuggestionMenu.d.ts +1 -11
  153. package/types/src/extensions/SyntaxHighlighting/SyntaxHighlighting.d.ts +44 -0
  154. package/types/src/extensions/SyntaxHighlighting/SyntaxHighlighting.test.d.ts +1 -0
  155. package/types/src/extensions/SyntaxHighlighting/inlineHighlight.test.d.ts +1 -0
  156. package/types/src/extensions/SyntaxHighlighting/shiki.d.ts +13 -0
  157. package/types/src/extensions/TableHandles/TableHandles.d.ts +1 -1
  158. package/types/src/extensions/Versioning/Versioning.d.ts +1 -35
  159. package/types/src/extensions/index.d.ts +4 -0
  160. package/types/src/i18n/locales/en.d.ts +9 -0
  161. package/types/src/i18n/locales/fa.d.ts +9 -0
  162. package/types/src/i18n/locales/sk.d.ts +9 -0
  163. package/types/src/index.d.ts +2 -1
  164. package/types/src/schema/blocks/types.d.ts +36 -3
  165. package/types/src/schema/inlineContent/createSpec.d.ts +35 -3
  166. package/types/src/schema/inlineContent/createSpec.test.d.ts +1 -0
  167. package/types/src/schema/inlineContent/internal.d.ts +3 -2
  168. package/types/src/schema/inlineContent/types.d.ts +22 -3
  169. package/types/src/user/UserStore.d.ts +1 -1
  170. package/types/src/util/Store.d.ts +45 -0
  171. package/types/src/util/Store.test.d.ts +1 -0
  172. package/types/src/y/extensions/AttributionExtension.d.ts +1 -1
  173. package/types/src/y/extensions/DiffVersioningExtension.d.ts +1 -1
  174. package/types/src/y/extensions/ForkYDoc.d.ts +1 -5
  175. package/types/src/y/extensions/index.d.ts +2 -10
  176. package/types/src/yjs/comments/RESTYjsThreadStore.test.d.ts +1 -0
  177. package/types/src/yjs/extensions/ForkYDoc.d.ts +1 -5
  178. package/types/src/yjs/extensions/index.d.ts +1 -5
  179. package/dist/BlockNoteExtension-B_IEJHS8.js +0 -38
  180. package/dist/BlockNoteExtension-B_IEJHS8.js.map +0 -1
  181. package/dist/BlockNoteExtension-M0znTzxP.cjs +0 -2
  182. package/dist/BlockNoteExtension-M0znTzxP.cjs.map +0 -1
  183. package/dist/UserStore-QbbPFj0z.js.map +0 -1
  184. package/dist/blocks-BEJW9OM_.js.map +0 -1
  185. package/dist/blocks-BPmJglFo.cjs +0 -12
  186. package/dist/blocks-BPmJglFo.cjs.map +0 -1
  187. package/dist/en-C5Z4Ag9_.cjs +0 -2
  188. package/dist/en-C5Z4Ag9_.cjs.map +0 -1
  189. package/dist/en-CGKXfWma.js.map +0 -1
  190. package/dist/extensions-AitWbYdJ.cjs +0 -52
  191. package/dist/extensions-AitWbYdJ.cjs.map +0 -1
  192. package/dist/extensions-BR5hkAio.js.map +0 -1
  193. package/dist/src-BHPuOo7W.cjs +0 -21
  194. package/dist/src-BHPuOo7W.cjs.map +0 -1
  195. package/dist/src-BUXut68d.js.map +0 -1
  196. package/src/blocks/Code/shiki.ts +0 -73
  197. package/types/src/blocks/Code/shiki.d.ts +0 -4
@@ -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
13
  import { ignoreNonContentMutations } 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(
@@ -233,10 +383,19 @@ export function createInlineContentSpec<
233
383
  ...inlineContentImplementation,
234
384
  toExternalHTML: inlineContentImplementation.toExternalHTML,
235
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
+
236
393
  const output = inlineContentImplementation.render(
237
394
  inlineContent,
238
395
  updateInlineContent,
239
396
  editor,
397
+ node,
398
+ () => undefined,
240
399
  );
241
400
 
242
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
@@ -1,5 +1,5 @@
1
- import { Store } from "@tanstack/store";
2
1
  import { createStore } from "../editor/BlockNoteExtension.js";
2
+ import { Store } from "../util/Store.js";
3
3
 
4
4
  /**
5
5
  * A collaborator of the document.