platejs 54.0.0-beta.2 → 54.0.0-beta.4

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 (255) hide show
  1. package/README.md +1 -1
  2. package/THIRD_PARTY_NOTICES.md +56 -0
  3. package/dist/{AIChatRequestContext-BMVWhfPV.d.ts → AIChatRequestContext-DrUahdTc.d.ts} +2 -2
  4. package/dist/{BaseSuggestionPlugin-DBl3fqnm.d.ts → BaseSuggestionPlugin-_7Ka6nY3.d.ts} +7 -3
  5. package/dist/{PlateStatic-DhNv2ejp.d.ts → PlateStatic-D3Cb-QwH.d.ts} +7 -7
  6. package/dist/ai/index.d.ts +1 -1
  7. package/dist/ai/react/AIChatPlugin.js +113 -78
  8. package/dist/ai/react/CopilotPlugin.js +1 -1
  9. package/dist/ai/react/index.d.ts +23 -16
  10. package/dist/authored/index.d.ts +4 -4
  11. package/dist/code-block/codemirror/createCodeMirrorAdapter.js +12 -9
  12. package/dist/code-block/codemirror/index.d.ts +1 -1
  13. package/dist/{combobox-DkBGgyd3.d.ts → combobox-Dr0OYZIE.d.ts} +2 -2
  14. package/dist/{comparison-types-CYnJ35qU.d.ts → comparison-types-DdwE6mYq-BogsHoUO.d.ts} +10 -2
  15. package/dist/compiler/index.d.ts +2 -2
  16. package/dist/{core-BJG8l_mk.d.ts → core-BqR-fEDp.d.ts} +493 -116
  17. package/dist/diff/index.d.ts +2 -2
  18. package/dist/docs/ai.md +4 -3
  19. package/dist/docs/api/core/plate-components.md +0 -19
  20. package/dist/docs/api/core/plate-controller.md +5 -3
  21. package/dist/docs/api/core/plate-plugin.md +3 -0
  22. package/dist/docs/api/core/plate-store.md +3 -3
  23. package/dist/docs/api/core.md +45 -5
  24. package/dist/docs/api/dom.md +11 -4
  25. package/dist/docs/api/editor-transforms.md +2 -1
  26. package/dist/docs/api/react-hooks.md +45 -13
  27. package/dist/docs/authored-changes.md +9 -4
  28. package/dist/docs/basic-blocks.md +42 -29
  29. package/dist/docs/block-placeholder.md +4 -2
  30. package/dist/docs/blockquote.md +5 -3
  31. package/dist/docs/board.md +118 -10
  32. package/dist/docs/code-block.md +47 -0
  33. package/dist/docs/comment.md +2 -2
  34. package/dist/docs/discussion.md +1 -1
  35. package/dist/docs/document-model.md +15 -3
  36. package/dist/docs/docx.md +1 -1
  37. package/dist/docs/dom-coverage.md +5 -0
  38. package/dist/docs/editor.md +50 -4
  39. package/dist/docs/examples/editable-voids.md +2 -3
  40. package/dist/docs/external-text.md +2 -0
  41. package/dist/docs/field.md +370 -0
  42. package/dist/docs/find.md +9 -6
  43. package/dist/docs/form.md +2 -0
  44. package/dist/docs/heading.md +6 -0
  45. package/dist/docs/installation/manual.md +0 -4
  46. package/dist/docs/link.md +3 -3
  47. package/dist/docs/llms.txt +4 -2
  48. package/dist/docs/markdown.md +270 -14
  49. package/dist/docs/plugin-components.md +13 -10
  50. package/dist/docs/plugin-input-rules.md +66 -15
  51. package/dist/docs/prompt-editor.md +205 -0
  52. package/dist/docs/roots.md +8 -6
  53. package/dist/docs/rtl.md +6 -4
  54. package/dist/docs/selection.md +7 -4
  55. package/dist/docs/suggestion.md +1 -1
  56. package/dist/docs/toolbar.md +26 -30
  57. package/dist/docs/upload.md +106 -17
  58. package/dist/docx/export/index.d.ts +3 -3
  59. package/dist/docx/export/lib/docxStaticComponents.js +12 -2
  60. package/dist/docx/export/lib/exportDocx.js +46 -17
  61. package/dist/docx/export/lib/internal/xml-builder.js +11 -16
  62. package/dist/docx/export/lib/reviewPart.js +1 -1
  63. package/dist/docx/export/lib/reviewProjection.js +52 -10
  64. package/dist/docx/import/index.d.ts +4 -4
  65. package/dist/docx/paste/index.d.ts +4 -4
  66. package/dist/dom/index.d.ts +1 -6
  67. package/dist/dom/plite-dom.internal.js +2 -2
  68. package/dist/excalidraw/index.d.ts +3 -2
  69. package/dist/excalidraw/react/index.d.ts +3 -2
  70. package/dist/facade.js +2 -2
  71. package/dist/features/basic-nodes/lib/BaseBlockPlugins.js +13 -5
  72. package/dist/features/basic-nodes/lib/BaseHeadingPlugins.js +32 -10
  73. package/dist/features/board/index.d.ts +2 -2
  74. package/dist/features/board/lib/BaseBoardPlugin.js +14 -1
  75. package/dist/features/board/lib/boardStroke.internal.js +13 -1
  76. package/dist/features/board/lib/boardTypes.internal.js +16 -0
  77. package/dist/features/callout/index.d.ts +3 -2
  78. package/dist/features/code-block/lib/BaseCodeBlockPlugin.js +14 -5
  79. package/dist/features/combobox/index.d.ts +2 -2
  80. package/dist/features/comments/BaseCommentsPlugin.js +33 -17
  81. package/dist/features/comments/index.d.ts +1 -1
  82. package/dist/features/date/index.d.ts +3 -2
  83. package/dist/features/details/index.d.ts +3 -2
  84. package/dist/features/details/lib/BaseDetailsPlugin.js +25 -12
  85. package/dist/features/field/index.d.ts +2 -0
  86. package/dist/features/field/index.js +4 -0
  87. package/dist/features/field/lib/BaseFieldPlugin.js +360 -0
  88. package/dist/features/field/lib/applyFieldValues.js +188 -0
  89. package/dist/features/field/lib/fieldRules.internal.js +204 -0
  90. package/dist/features/find/index.d.ts +3 -2
  91. package/dist/features/footnote/index.d.ts +1 -1
  92. package/dist/features/ink/index.d.ts +1 -1
  93. package/dist/features/ink/lib/BaseInkPlugin.js +126 -103
  94. package/dist/features/ink/lib/inkCodec.internal.js +130 -0
  95. package/dist/features/ink/lib/inkGone.internal.js +229 -48
  96. package/dist/features/ink/lib/inkPlaces.internal.js +534 -64
  97. package/dist/features/layout/index.d.ts +1 -1
  98. package/dist/features/link/lib/BaseLinkPlugin.js +6 -4
  99. package/dist/features/lint/index.d.ts +3 -2
  100. package/dist/features/media/index.d.ts +2 -2
  101. package/dist/features/media/index.js +2 -2
  102. package/dist/features/media/lib/media/parseMediaUrl.js +11 -1
  103. package/dist/features/media/lib/media-embed/BaseMediaEmbedPlugin.js +206 -4
  104. package/dist/features/mention/index.d.ts +5 -4
  105. package/dist/features/suggestion/index.d.ts +1 -1
  106. package/dist/features/table/index.d.ts +1 -1
  107. package/dist/features/table/lib/BaseTablePlugin.js +64 -15
  108. package/dist/features/table/lib/internal/selection.js +4 -2
  109. package/dist/features/toc/index.d.ts +1 -1
  110. package/dist/features/upload/index.d.ts +2 -2
  111. package/dist/features/upload/lib/BaseUploadPlugin.js +464 -250
  112. package/dist/history/index.d.ts +2 -2
  113. package/dist/html/index.d.ts +3 -3
  114. package/dist/html/server/index.d.ts +1 -1
  115. package/dist/{htmlTypes-DjeFG3P8.d.ts → htmlTypes-eT2ZGDAj.d.ts} +2 -31
  116. package/dist/hyperscript/index.d.ts +1 -1
  117. package/dist/{index-DzD6BIFJ.d.ts → index-B33E1CRJ.d.ts} +295 -6
  118. package/dist/{index-Cl77E9yr.d.ts → index-B7moSod7.d.ts} +2 -2
  119. package/dist/{index-hsC_sOn2.d.ts → index-B8-5Sr7I.d.ts} +35 -5
  120. package/dist/{index-BiQerPJS.d.ts → index-B8QJDAmG.d.ts} +2 -2
  121. package/dist/{index-BBKGrsG3.d.ts → index-BTx5oK7k.d.ts} +1 -1
  122. package/dist/{index-iaZOhA76.d.ts → index-BWPcE4ac-Cto_IsNs.d.ts} +7 -1
  123. package/dist/{index-BU6k1Aev.d.ts → index-BXjLmx1n-CXk17buG.d.ts} +4781 -4584
  124. package/dist/{index-kLKMc-pI.d.ts → index-BXwU9QH6.d.ts} +2 -2
  125. package/dist/{index-DTgaSxKr.d.ts → index-BkwR-5hH.d.ts} +40 -4
  126. package/dist/{index-Bq3sdm3S.d.ts → index-BsV2McRr.d.ts} +5 -4
  127. package/dist/index-C9Fu-P4E.d.ts +773 -0
  128. package/dist/{index-D5W-p9bS.d.ts → index-Ca48r1ng.d.ts} +49 -4
  129. package/dist/{index-CcZbqPqk.d.ts → index-Cob_iBdu.d.ts} +116 -41
  130. package/dist/{index-Bxfvicyy.d.ts → index-CpY7Xnxw.d.ts} +4 -3
  131. package/dist/{index-BkR7-GLI.d.ts → index-DNQXzU-X.d.ts} +2 -2
  132. package/dist/{index-D1CFXA-i.d.ts → index-DPowErHa.d.ts} +471 -141
  133. package/dist/{index-DaM37gF6.d.ts → index-DShbzec2.d.ts} +38 -23
  134. package/dist/{index-DC_MNGyu.d.ts → index-DYdObeFQ.d.ts} +3 -3
  135. package/dist/{index-Bc2Yg_2j.d.ts → index-Dq3Jlj8O-BJ6NxZm0.d.ts} +2 -2
  136. package/dist/{index-CT7miqQN.d.ts → index-DwWQVwQt.d.ts} +29 -11
  137. package/dist/{index-xdmCBvlT.d.ts → index-JWgwL5l3.d.ts} +5 -2
  138. package/dist/{index-DLA2DUR2.d.ts → index-M781pyx9.d.ts} +4 -3
  139. package/dist/{index-DdIRduDG.d.ts → index-d7io3MzC.d.ts} +3 -2
  140. package/dist/index.d.ts +4 -4
  141. package/dist/{react/internal → internal}/penStroke.internal.js +1 -1
  142. package/dist/internal/plugin/OverridePlugin.js +101 -119
  143. package/dist/internal/plugin/collectPlateNodeMappings.js +5 -1
  144. package/dist/internal/plugin/pastedUploadFiles.js +4 -0
  145. package/dist/internal/plugin/resolvePlugin.js +1 -1
  146. package/dist/internal/plugin/resolvePlugins.js +3 -0
  147. package/dist/internal/plugin/uploadDrafts.js +6 -0
  148. package/dist/internal/utils/mergePlugins.js +1 -0
  149. package/dist/lib/plugins/element-id/ElementIdPlugin.js +14 -18
  150. package/dist/lib/plugins/html/HtmlPlugin.js +9 -0
  151. package/dist/lib/plugins/html/htmlSafety.js +1 -1
  152. package/dist/lib/plugins/input-rules/InputRulesPlugin.js +77 -28
  153. package/dist/lib/plugins/input-rules/inputRuleVeto.internal.js +16 -0
  154. package/dist/markdown/index.d.ts +2 -2
  155. package/dist/markdown/index.js +3 -1
  156. package/dist/markdown/lib/MarkdownPlugin.js +14 -11
  157. package/dist/markdown/lib/RawHtmlPlugin.js +26 -0
  158. package/dist/markdown/lib/deserializer/mdastToSlate.js +12 -2
  159. package/dist/markdown/lib/internal/markdownConversion.js +776 -74
  160. package/dist/markdown/lib/internal/markdownDiagnostics.js +18 -7
  161. package/dist/markdown/lib/internal/markdownFootnoteWriter.js +35 -0
  162. package/dist/markdown/lib/internal/markdownFrontmatter.js +102 -0
  163. package/dist/markdown/lib/internal/markdownIntrinsics.js +125 -43
  164. package/dist/markdown/lib/internal/markdownLine.js +5 -3
  165. package/dist/markdown/lib/internal/markdownMappings.js +19 -7
  166. package/dist/markdown/lib/internal/markdownOpenTail.js +381 -0
  167. package/dist/markdown/lib/internal/markdownRawHtml.js +353 -0
  168. package/dist/markdown/lib/internal/markdownSegments.js +57 -48
  169. package/dist/markdown/lib/internal/markdownTags.js +29 -9
  170. package/dist/markdown/lib/serializer/convertNodesSerialize.js +10 -8
  171. package/dist/markdown/lib/serializer/convertPhrasingSerialize.js +226 -0
  172. package/dist/markdown/lib/serializer/listToMdastTree.js +2 -0
  173. package/dist/math/index.d.ts +3 -2
  174. package/dist/math/react/index.d.ts +4 -3
  175. package/dist/migrations/index.d.ts +3 -3
  176. package/dist/migrations/migratePlateV54.js +4 -6
  177. package/dist/migrations/migratePlateV54RetiredMarks.internal.js +23 -8
  178. package/dist/migrations/migratePlateV54Suggestions.internal.js +2 -2
  179. package/dist/migrations/v53-manifest.internal.js +4 -0
  180. package/dist/pagination/index.d.ts +1 -1
  181. package/dist/pagination/react/index.d.ts +4 -4
  182. package/dist/react/components/EditorContentView.internal.js +42 -23
  183. package/dist/react/components/EditorProvider.js +5 -5
  184. package/dist/react/components/Plate.js +102 -128
  185. package/dist/react/components/PlateContainer.js +40 -32
  186. package/dist/react/components/PlateController.js +28 -16
  187. package/dist/react/components/PlateRoot.internal.js +3 -2
  188. package/dist/react/core.js +4 -4
  189. package/dist/react/features/board/BoardSurface.js +116 -87
  190. package/dist/react/features/board/boardClipboard.internal.js +15 -5
  191. package/dist/react/features/board/boardExport.internal.js +470 -0
  192. package/dist/react/features/board/boardGesture.internal.js +6 -3
  193. package/dist/react/features/board/boardIndex.internal.js +7 -18
  194. package/dist/react/features/board/boardPictureStyles.internal.js +415 -0
  195. package/dist/react/features/board/boardPictures.internal.js +19 -13
  196. package/dist/react/features/board/boardView.internal.js +190 -50
  197. package/dist/react/features/board/boardVoice.internal.js +10 -11
  198. package/dist/react/features/board/index.d.ts +108 -18
  199. package/dist/react/features/callout/index.d.ts +3 -2
  200. package/dist/react/features/combobox/index.d.ts +8 -8
  201. package/dist/react/features/comments/index.d.ts +6 -5
  202. package/dist/react/features/date/index.d.ts +4 -3
  203. package/dist/react/features/details/index.d.ts +4 -3
  204. package/dist/react/features/field/FieldPlugin.js +35 -0
  205. package/dist/react/features/field/index.d.ts +315 -0
  206. package/dist/react/features/field/index.js +2 -0
  207. package/dist/react/features/footnote/index.d.ts +7 -6
  208. package/dist/react/features/format-painter/index.d.ts +4 -3
  209. package/dist/react/features/ink/InkContent.js +313 -0
  210. package/dist/react/features/ink/index.d.ts +57 -33
  211. package/dist/react/features/ink/index.js +2 -2
  212. package/dist/react/features/ink/inkCapture.internal.js +39 -17
  213. package/dist/react/features/ink/inkInput.internal.js +1 -1
  214. package/dist/react/features/ink/inkLayout.internal.js +6 -3
  215. package/dist/react/features/ink/inkPaint.internal.js +78 -2
  216. package/dist/react/features/ink/inkPlacement.internal.js +332 -104
  217. package/dist/react/features/ink/inkWatch.internal.js +7 -5
  218. package/dist/react/features/layout/index.d.ts +5 -4
  219. package/dist/react/features/media/index.d.ts +5 -4
  220. package/dist/react/features/mention/index.d.ts +6 -5
  221. package/dist/react/features/resizable/Resizable.js +4 -6
  222. package/dist/react/features/resizable/index.d.ts +5 -5
  223. package/dist/react/features/suggestion/index.d.ts +6 -5
  224. package/dist/react/features/table/TablePlugin.js +4 -11
  225. package/dist/react/features/table/index.d.ts +10 -8
  226. package/dist/react/features/table/useTableResize.js +10 -1
  227. package/dist/react/features/toc/index.d.ts +2 -2
  228. package/dist/react/features/upload/index.d.ts +11 -6
  229. package/dist/react/index.d.ts +6 -6
  230. package/dist/react/index.js +4 -4
  231. package/dist/react/internal/plate-context.js +434 -188
  232. package/dist/react/plite-react.js +2 -2
  233. package/dist/react/plugins/navigation-feedback/NavigationFeedbackPlugin.js +12 -10
  234. package/dist/react/stores/plate/useEditor.js +32 -14
  235. package/dist/react/stores/plate/useEditorHistory.js +55 -0
  236. package/dist/react/stores/plate/useEditorSelector.js +79 -21
  237. package/dist/react/utils/BlockPlaceholderPlugin.js +5 -2
  238. package/dist/react/utils/pipeRenderElement.internal.js +1 -1
  239. package/dist/react/utils/pluginRenderElement.internal.js +1 -1
  240. package/dist/react/virtualized.d.ts +4 -4
  241. package/dist/{source-CNKy99Th.d.ts → source-BGohy8vH.d.ts} +4 -4
  242. package/dist/static/index.d.ts +18 -18
  243. package/dist/static/internal/renderStaticHtmlWithOverrides.js +2 -2
  244. package/dist/static/internal/staticPresentation.js +4 -4
  245. package/dist/static/internal/writeStaticSelectionClipboardData.js +7 -2
  246. package/dist/tabbable/index.d.ts +1 -1
  247. package/dist/tabbable/react/index.d.ts +3 -3
  248. package/dist/testing/index.d.ts +3 -3
  249. package/dist/yjs/index.d.ts +1 -1
  250. package/dist/yjs/react/index.d.ts +6 -6
  251. package/package.json +20 -2
  252. package/dist/markdown/lib/serializer/convertTextsSerialize.js +0 -163
  253. package/dist/react/components/EditorSelectionView.js +0 -40
  254. package/dist/react/features/ink/InkLayer.js +0 -218
  255. package/dist/react/internal/createPlateTargetScope.js +0 -62
@@ -24,6 +24,13 @@ const freezeDiagnostic = (diagnostic) => {
24
24
  ...contextual.source ? { source: freezeSourceLocation(contextual.source) } : {}
25
25
  });
26
26
  };
27
+ const sourceStart = (diagnostic) => "source" in diagnostic ? diagnostic.source?.start : void 0;
28
+ const bySource = (a, b) => {
29
+ const left = sourceStart(a);
30
+ const right = sourceStart(b);
31
+ if (!left || !right) return left ? -1 : right ? 1 : 0;
32
+ return left.line - right.line || left.column - right.column;
33
+ };
27
34
  var MarkdownDiagnostics = class {
28
35
  #diagnostics = [];
29
36
  #keys = /* @__PURE__ */ new Set();
@@ -37,15 +44,19 @@ var MarkdownDiagnostics = class {
37
44
  this.#keys.add(key);
38
45
  this.#diagnostics.push(frozen);
39
46
  };
47
+ #ordered() {
48
+ return [...this.#diagnostics].sort(bySource);
49
+ }
40
50
  all() {
41
- return Object.freeze([...this.#diagnostics]);
51
+ return Object.freeze(this.#ordered());
42
52
  }
43
53
  failure() {
44
54
  if (this.#diagnostics.findIndex((diagnostic) => diagnostic.severity === "error") === -1) return null;
45
- return Object.freeze([...this.#diagnostics.filter((diagnostic) => diagnostic.severity === "error"), ...this.#diagnostics.filter((diagnostic) => diagnostic.severity === "warning")]);
55
+ const ordered = this.#ordered();
56
+ return Object.freeze([...ordered.filter((diagnostic) => diagnostic.severity === "error"), ...ordered.filter((diagnostic) => diagnostic.severity === "warning")]);
46
57
  }
47
58
  warnings() {
48
- return Object.freeze(this.#diagnostics.filter((diagnostic) => diagnostic.severity === "warning"));
59
+ return Object.freeze(this.#ordered().filter((diagnostic) => diagnostic.severity === "warning"));
49
60
  }
50
61
  };
51
62
  const pointFromPosition = (point) => Object.freeze({
@@ -64,11 +75,11 @@ const createMarkdownSourceLocation = (node, source, positionsReferToSource) => {
64
75
  start: pointFromPosition(start)
65
76
  });
66
77
  };
67
- const createMarkdownModelLocator = (document) => {
78
+ const createMarkdownModelLocator = (document, { from = 0 } = {}) => {
68
79
  const locations = /* @__PURE__ */ new WeakMap();
69
- const visit = (nodes, path, root) => {
80
+ const visit = (nodes, path, root, offset = 0) => {
70
81
  nodes.forEach((node, index) => {
71
- const nodePath = Object.freeze([...path, index]);
82
+ const nodePath = Object.freeze([...path, offset + index]);
72
83
  locations.set(node, Object.freeze({
73
84
  path: nodePath,
74
85
  ...root === void 0 ? {} : { root }
@@ -76,7 +87,7 @@ const createMarkdownModelLocator = (document) => {
76
87
  if (ElementApi.isElement(node)) visit(node.children, nodePath, root);
77
88
  });
78
89
  };
79
- visit(document.children, []);
90
+ visit(document.children.slice(from), [], void 0, from);
80
91
  Object.entries(document.roots ?? {}).forEach(([root, nodes]) => {
81
92
  visit(nodes, [], root);
82
93
  });
@@ -0,0 +1,35 @@
1
+ import { decideUrl } from "../../../internal/utils/urlPolicy.js";
2
+ //#region src/markdown/lib/internal/markdownFootnoteWriter.ts
3
+ const DEFINITION_START = /^(\[\^(?:\\.|[^\\\]])*\]:)(?: |\n {4})/;
4
+ const definesRefusedUrl = (node) => node.type === "definition" ? !decideUrl("navigation", node.url).ok : "children" in node && node.children.some(definesRefusedUrl);
5
+ const findHandler = (extensions, name) => {
6
+ let found;
7
+ for (const extension of extensions) {
8
+ found = findHandler(extension.extensions ?? [], name) ?? found;
9
+ found = extension.handlers?.[name] ?? found;
10
+ }
11
+ return found;
12
+ };
13
+ /**
14
+ * CommonMark without footnotes reads `[^1]: javascript:alert(1)` as a link
15
+ * definition, which turns every `[^1]` into a link to that destination, and
16
+ * the definitions after it, such as a `$$` block's TeX lines, read the same
17
+ * way. A footnote that `read`, the CommonMark reader, reads as defining a URL
18
+ * the navigation policy refuses starts its content after a blank line, which
19
+ * both readers read as the same footnote and CommonMark as indented code.
20
+ */
21
+ const remarkFootnoteDefinitionWriter = function({ read }) {
22
+ const data = this.data();
23
+ data.toMarkdownExtensions ??= [];
24
+ const extensions = data.toMarkdownExtensions;
25
+ const write = findHandler(extensions, "footnoteDefinition");
26
+ if (!write) return;
27
+ extensions.push({ handlers: { footnoteDefinition: (...args) => {
28
+ const value = write(...args);
29
+ const start = DEFINITION_START.exec(value);
30
+ if (!start || !definesRefusedUrl(read(value))) return value;
31
+ return `${start[1]}\n\n ${value.slice(start[0].length)}`;
32
+ } } });
33
+ };
34
+ //#endregion
35
+ export { remarkFootnoteDefinitionWriter };
@@ -0,0 +1,102 @@
1
+ //#region src/markdown/lib/internal/markdownFrontmatter.ts
2
+ const MARKDOWN_FRONTMATTER_KEY = "markdown.frontmatter";
3
+ const FORMATS = ["yaml", "toml"];
4
+ const FENCES = {
5
+ toml: "+++",
6
+ yaml: "---"
7
+ };
8
+ const BLANK_LINE = /^[ \t]*$/;
9
+ const COMMENT_LINE = /^[ \t]*#/;
10
+ const TOML_KEY = String.raw`(?:[A-Za-z0-9_-]+|"(?:[^"\\]|\\.)*"|'[^']*')`;
11
+ const TOML_DOTTED_KEY = String.raw`${TOML_KEY}(?:[ \t]*\.[ \t]*${TOML_KEY})*`;
12
+ const GRAMMAR = {
13
+ toml: {
14
+ close: /^\+\+\+[ \t]*$/,
15
+ open: /^\+\+\+[ \t]*$/,
16
+ setting: new RegExp([String.raw`^[ \t]*(?:\[\[[ \t]*${TOML_DOTTED_KEY}[ \t]*\]\]|\[[ \t]*${TOML_DOTTED_KEY}[ \t]*\])[ \t]*(?:#.*)?$`, String.raw`^[ \t]*${TOML_DOTTED_KEY}[ \t]*=`].join("|"))
17
+ },
18
+ yaml: {
19
+ close: /^(?:---|\.\.\.)[ \t]*$/,
20
+ open: /^---[ \t]*$/,
21
+ setting: new RegExp([
22
+ String.raw`^[ \t]*(?:"(?:[^"\\]|\\.)*"|'(?:[^']|'')*')[ \t]*:(?:[ \t]|$)`,
23
+ String.raw`^[ \t]*\{`,
24
+ String.raw`^[ \t]*<<[ \t]*:(?:[ \t]|$)`,
25
+ String.raw`^[ \t]*(?!\d+[.)](?:[ \t]|$))[^\s#\-?:,\[\]{}&*!|>'"%@\x60<+=~][^\r\n]*?:(?:[ \t]|$)`
26
+ ].join("|"))
27
+ }
28
+ };
29
+ const readMarkdownFrontmatter = (source) => {
30
+ const start = source.startsWith("") ? 1 : 0;
31
+ const format = FORMATS.find((name) => source.startsWith(FENCES[name], start));
32
+ if (!format) return null;
33
+ const grammar = GRAMMAR[format];
34
+ const lineEnding = /\r\n|\r|\n/g;
35
+ const lineAt = (from) => {
36
+ if (from >= source.length) return null;
37
+ lineEnding.lastIndex = from;
38
+ const ending = lineEnding.exec(source);
39
+ return ending ? {
40
+ end: ending.index + ending[0].length,
41
+ ended: true,
42
+ start: from,
43
+ text: source.slice(from, ending.index)
44
+ } : {
45
+ end: source.length,
46
+ ended: false,
47
+ start: from,
48
+ text: source.slice(from)
49
+ };
50
+ };
51
+ const opener = lineAt(start);
52
+ if (!opener?.ended || !grammar.open.test(opener.text)) return null;
53
+ let line = lineAt(opener.end);
54
+ if (line && grammar.open.test(line.text)) return {
55
+ end: line.end,
56
+ format,
57
+ value: ""
58
+ };
59
+ while (line && (BLANK_LINE.test(line.text) || COMMENT_LINE.test(line.text))) line = lineAt(line.end);
60
+ if (!line || !grammar.setting.test(line.text)) return null;
61
+ do
62
+ line = lineAt(line.end);
63
+ while (line && !grammar.close.test(line.text));
64
+ if (!line) return null;
65
+ const value = source.slice(opener.end, line.start).replaceAll(/\r\n?/g, "\n").replace(/\n$/u, "");
66
+ return value.isWellFormed() ? {
67
+ end: line.end,
68
+ format,
69
+ value
70
+ } : null;
71
+ };
72
+ const blankMarkdownFrontmatter = (source, end) => source.slice(0, end).replaceAll("\r\n", " \n").replaceAll("\r", "\n").replaceAll(/[^\n]/g, " ") + source.slice(end);
73
+ const opensMarkdownFrontmatterFence = (text) => {
74
+ const start = text.startsWith("") ? 1 : 0;
75
+ const format = FORMATS.find((name) => text.startsWith(FENCES[name], start));
76
+ if (!format) return false;
77
+ const [opener = "", ...lines] = text.slice(start).split(/\r\n|\r|\n/);
78
+ return GRAMMAR[format].open.test(opener) && lines.some((line) => GRAMMAR[format].close.test(line));
79
+ };
80
+ const writeMarkdownFrontmatter = ({ format, value }) => {
81
+ const fence = FENCES[format];
82
+ const block = `${fence}\n${value && `${value}\n`}${fence}\n`;
83
+ return readMarkdownFrontmatter(block)?.value === value ? block : null;
84
+ };
85
+ const hasMarkdownFrontmatterShape = (stored) => {
86
+ if (typeof stored !== "object" || stored === null) return false;
87
+ const entries = new Map(Object.entries(stored));
88
+ return entries.size === 2 && typeof entries.get("format") === "string" && typeof entries.get("value") === "string";
89
+ };
90
+ const isMarkdownFrontmatter = (stored) => hasMarkdownFrontmatterShape(stored) && (stored.format === "yaml" || stored.format === "toml");
91
+ /**
92
+ * The front matter block that a Markdown document parse stored on the
93
+ * document, or `undefined` when the stored value is missing or is not a
94
+ * `MarkdownFrontmatter`. The block comes back as stored; a document serialize
95
+ * is what checks that it can be written.
96
+ */
97
+ const getMarkdownFrontmatter = (document) => {
98
+ const stored = document.meta?.[MARKDOWN_FRONTMATTER_KEY];
99
+ return isMarkdownFrontmatter(stored) ? stored : void 0;
100
+ };
101
+ //#endregion
102
+ export { MARKDOWN_FRONTMATTER_KEY, blankMarkdownFrontmatter, getMarkdownFrontmatter, hasMarkdownFrontmatterShape, isMarkdownFrontmatter, opensMarkdownFrontmatterFence, readMarkdownFrontmatter, writeMarkdownFrontmatter };
@@ -2,14 +2,34 @@ import { PLUGINS } from "../../../utils/plate-keys.js";
2
2
  import { ElementApi, TextApi } from "../../../core.js";
3
3
  import { getMarkdownParagraphType } from "../../../internal/plugin/markdownParagraph.js";
4
4
  import { convertChildrenDeserialize } from "../deserializer/convertChildrenDeserialize.js";
5
+ import { RawHtmlPlugin } from "../RawHtmlPlugin.js";
5
6
  import { getMarkdownTagRepair } from "./markdownTags.js";
6
7
  import { convertNodesSerialize } from "../serializer/convertNodesSerialize.js";
7
8
  //#region src/markdown/lib/internal/markdownIntrinsics.ts
8
9
  const BR_TAG = /<br\s*\/?>/gi;
9
10
  const ONLY_BR_TAGS = /^(?:\s*<br\s*\/?>)+\s*$/i;
10
- const HTML_COMMENT = /^\s*<!--[\s\S]*-->\s*$/;
11
+ const HTML_COMMENT = /^[\t\n\f\r ]*(?:<!--(?!-?>)(?:(?!--!?>)[\s\S])*-->[\t\n\f\r ]*)+$/;
11
12
  const TAG_NAME = /^<\/?([A-Za-z][\w-]*)/;
12
13
  const BARE_URL_HTML = /^[a-z][a-z\d+.-]*:\S*$/i;
14
+ const isMarkdownBreakHtml = (value) => ONLY_BR_TAGS.test(value);
15
+ /** Encode marked leaves as raw HTML that their block's decision may keep. */
16
+ const encodeRawHtmlLeaves = (leaves) => {
17
+ return {
18
+ data: { rawHtmlPiece: { leaves } },
19
+ type: "html",
20
+ value: leaves.map((leaf) => leaf.text).join("")
21
+ };
22
+ };
23
+ const rawHtmlDataOf = (node) => node.data;
24
+ const rawHtmlPieceOf = (node) => node.type === "html" ? rawHtmlDataOf(node)?.rawHtmlPiece : void 0;
25
+ const markRawHtmlFlow = (paragraph) => {
26
+ const data = { rawHtmlFlow: true };
27
+ paragraph.data = {
28
+ ...paragraph.data,
29
+ ...data
30
+ };
31
+ };
32
+ const isRawHtmlFlow = (node) => rawHtmlPieceOf(node) !== void 0 || node.type === "paragraph" && rawHtmlDataOf(node)?.rawHtmlFlow === true;
13
33
  /** Markdown drops an empty paragraph, so the writer gives it this text. */
14
34
  const EMPTY_PARAGRAPH_TEXT = "​";
15
35
  /** The text an empty paragraph is written with, read back as empty. */
@@ -17,6 +37,10 @@ const readEmptyParagraphMarker = (child) => TextApi.isText(child) && child.text
17
37
  ...child,
18
38
  text: ""
19
39
  } : child;
40
+ const withoutLoneBreakSeparators = (children) => children.filter((child, index) => {
41
+ const previous = children[index - 1];
42
+ return !(child.type === "html" && child.value === WRITER_SEPARATOR && previous?.type === "html" && previous.value === WRITTEN_BREAK_HTML);
43
+ });
20
44
  const withoutTrailingBreakMarkup = (children) => {
21
45
  const last = children.at(-1);
22
46
  const previous = children.at(-2);
@@ -46,6 +70,45 @@ const withUnmarkedLineEndings = (text, marks) => text.includes("\n") ? text.spli
46
70
  ...marks,
47
71
  text
48
72
  };
73
+ const FLOW_CONTAINERS = /* @__PURE__ */ new Set([
74
+ "blockquote",
75
+ "list",
76
+ "listItem"
77
+ ]);
78
+ const WRITER_SEPARATOR = "<!---->";
79
+ const isBlockWrapper = (node) => node.type === "mdxJsxFlowElement" && node.name === "block";
80
+ const wholeHtmlBlockRefusal = (value, follows) => {
81
+ if (value === WRITER_SEPARATOR) return "comment";
82
+ if (/^ *\t/.test(value) || follows !== "block" && /^[\t ]/.test(value)) return "flow-shape";
83
+ };
84
+ const isMarkdownHtmlComment = (value) => HTML_COMMENT.test(value);
85
+ const isEmptyBlockWrapper = (node) => isBlockWrapper(node) && node.children.every((child) => isEmptyBlockWrapper(child));
86
+ const wrapsOneHtmlBlock = (wrapper) => {
87
+ const [only, ...rest] = wrapper.children.filter((child) => !isEmptyBlockWrapper(child));
88
+ if (!only || rest.length > 0) return false;
89
+ return only.type === "html" || isBlockWrapper(only) && wrapsOneHtmlBlock(only);
90
+ };
91
+ const findMarkdownRawHtml = (tree, before = "block") => {
92
+ const blocks = /* @__PURE__ */ new Set();
93
+ const visit = (parent, split, held, start) => {
94
+ let follows = start;
95
+ for (const child of parent.children) if (child.type === "html") {
96
+ if (split && !held && !wholeHtmlBlockRefusal(child.value, follows)) {
97
+ blocks.add(child);
98
+ follows = "block";
99
+ } else if (!HTML_COMMENT.test(child.value)) follows = "block";
100
+ } else if (isBlockWrapper(child)) follows = visit(child, split, held || !wrapsOneHtmlBlock(child), follows);
101
+ else {
102
+ if ("children" in child) visit(child, split && FLOW_CONTAINERS.has(child.type), held, child.type === "listItem" ? "marker" : "block");
103
+ follows = child.type === "list" ? "list" : "block";
104
+ }
105
+ return follows;
106
+ };
107
+ return {
108
+ blocks,
109
+ follows: visit(tree, true, false, before)
110
+ };
111
+ };
49
112
  /**
50
113
  * Language-level decoders every editor needs, even without feature plugins.
51
114
  * They run after every feature mapping on the same MDAST kind has declined.
@@ -70,7 +133,8 @@ const markdownIntrinsicDecoders = {
70
133
  };
71
134
  }
72
135
  if (ONLY_BR_TAGS.test(value)) return { text: value.replace(BR_TAG, "\n") };
73
- if (HTML_COMMENT.test(value)) {
136
+ const { rawHtml } = options;
137
+ if (HTML_COMMENT.test(value) && !rawHtml?.blocks.has(node)) {
74
138
  options.report({
75
139
  action: "dropped",
76
140
  code: "markdown-unsupported-node",
@@ -95,15 +159,24 @@ const markdownIntrinsicDecoders = {
95
159
  severity: "warning",
96
160
  source: options.sourceLocation(node)
97
161
  });
98
- return {
162
+ if (!rawHtml) return {
99
163
  ...marks,
100
164
  text: value
101
165
  };
166
+ const text = {
167
+ ...marks,
168
+ [RawHtmlPlugin.name]: true,
169
+ text: value
170
+ };
171
+ return rawHtml.blocks.has(node) ? {
172
+ children: [text],
173
+ type: getMarkdownParagraphType(options.registry)
174
+ } : text;
102
175
  },
103
176
  paragraph: (node, marks, options) => {
104
177
  const paragraphType = getMarkdownParagraphType(options.registry);
105
178
  const imageType = options.registry.type(PLUGINS.image);
106
- const children = convertChildrenDeserialize(withoutTrailingBreakMarkup(node.children), marks, options).map(readEmptyParagraphMarker);
179
+ const children = convertChildrenDeserialize(withoutTrailingBreakMarkup(withoutLoneBreakSeparators(node.children)), marks, options).map(readEmptyParagraphMarker);
107
180
  const elements = [];
108
181
  let inlineNodes = [];
109
182
  const flushInlineNodes = () => {
@@ -152,6 +225,25 @@ const writtenStart = (node) => {
152
225
  default: return node.type.startsWith("mdxJsx") ? "<" : JOINS_URL;
153
226
  }
154
227
  };
228
+ const writtenEnd = (node) => {
229
+ switch (node.type) {
230
+ case "break": return "\n";
231
+ case "delete": return "~";
232
+ case "emphasis":
233
+ case "strong": return "*";
234
+ case "html":
235
+ case "text": return node.value;
236
+ default: return JOINS_URL;
237
+ }
238
+ };
239
+ const ENDS_IN_LETTER = /[a-z]$/i;
240
+ const writtenBefore = (parent, index) => {
241
+ for (let previous = index - 1; previous >= 0; previous -= 1) {
242
+ const written = writtenEnd(parent.children[previous]);
243
+ if (written) return written;
244
+ }
245
+ return parent.type === "link" || parent.type.startsWith("mdxJsx") ? JOINS_URL : "";
246
+ };
155
247
  const writtenAfter = (parent, index) => {
156
248
  let written = "";
157
249
  for (let next = index + 1; next < parent.children.length; next += 1) {
@@ -176,7 +268,7 @@ const writeBareUrlsSafely = (parent) => {
176
268
  writeBareUrlsSafely(child);
177
269
  return;
178
270
  }
179
- if (child.type !== "html" || !BARE_URL_HTML.test(child.value) || staysBare(child.value, writtenAfter(parent, index))) return;
271
+ if (child.type !== "html" || rawHtmlPieceOf(child) || !BARE_URL_HTML.test(child.value) || !ENDS_IN_LETTER.test(writtenBefore(parent, index)) && staysBare(child.value, writtenAfter(parent, index))) return;
180
272
  parent.children[index] = {
181
273
  children: [{
182
274
  type: "text",
@@ -242,55 +334,45 @@ const remarkSeparateEscapes = function() {
242
334
  if (/[&\\]/.test(String(file.value))) joinTexts(tree);
243
335
  };
244
336
  };
245
- /** The `<br />` that keeps a paragraph's trailing line break, after its line ending. */
246
337
  const TRAILING_BREAK_HTML = "\n<br />";
338
+ const WRITTEN_BREAK_HTML = "<br />";
339
+ /**
340
+ * The first break of a paragraph that holds only hard breaks. `<br />` alone
341
+ * on a paragraph's first line opens an HTML block, which reads back as an
342
+ * empty paragraph; the comment after it keeps the line a paragraph and drops
343
+ * on import.
344
+ */
345
+ const LONE_BREAK_HTML = `${WRITTEN_BREAK_HTML}${WRITER_SEPARATOR}`;
247
346
  /**
248
347
  * Encode the paragraph type when no feature mapping claims it. Newlines in text
249
348
  * become hard breaks; a trailing one is kept as `<br />` because Markdown drops
250
349
  * a hard break at the end of a paragraph.
251
350
  */
252
351
  const encodeMarkdownParagraph = (node, options) => {
253
- const lines = [[]];
254
- const origins = /* @__PURE__ */ new WeakMap();
255
- const part = (child, text) => {
256
- const copy = {
257
- ...child,
258
- text
259
- };
260
- origins.set(copy, child);
261
- return copy;
262
- };
263
- const isEmpty = node.children.length === 1 && TextApi.isText(node.children[0]) && node.children[0].text === "";
264
- for (const child of node.children) {
265
- if (!TextApi.isText(child) || !child.text.includes("\n")) {
266
- lines.at(-1)?.push(isEmpty && options.preserveEmptyParagraphs !== false ? part(child, EMPTY_PARAGRAPH_TEXT) : child);
267
- continue;
268
- }
269
- child.text.split(/\r?\n/).forEach((text, index) => {
270
- if (index > 0) lines.push([]);
271
- if (text) lines.at(-1)?.push(part(child, text));
272
- });
273
- }
274
- const lineOptions = {
352
+ const [only] = node.children;
353
+ const placeholder = node.children.length === 1 && TextApi.isText(only) && only.text === "" && options.preserveEmptyParagraphs !== false ? {
354
+ ...only,
355
+ ...options.rawHtml ? { [RawHtmlPlugin.name]: void 0 } : {},
356
+ text: EMPTY_PARAGRAPH_TEXT
357
+ } : void 0;
358
+ const children = convertNodesSerialize(placeholder ? [placeholder] : node.children, placeholder ? {
275
359
  ...options,
276
- modelLocation: (child) => options.modelLocation(origins.get(child) ?? child)
277
- };
278
- const children = lines.flatMap((line, index) => {
279
- const nodes = convertNodesSerialize(line, lineOptions);
280
- return [...index > 0 ? [{ type: "break" }] : [], ...nodes];
281
- });
282
- if (lines.length > 1 && lines.at(-1)?.length === 0) {
283
- for (let index = children.length - 2; index >= 0; index -= 1) {
284
- if (children[index].type !== "break") break;
285
- children[index] = {
286
- type: "html",
287
- value: "<br />"
288
- };
289
- }
360
+ modelLocation: (child) => options.modelLocation(child === placeholder ? only : child)
361
+ } : options, { hardBreaks: true });
362
+ if (children.at(-1)?.type === "break") {
363
+ let index = children.length - 2;
364
+ for (; index >= 0 && children[index].type === "break"; index -= 1) children[index] = {
365
+ type: "html",
366
+ value: WRITTEN_BREAK_HTML
367
+ };
290
368
  children[children.length - 1] = {
291
369
  type: "html",
292
370
  value: TRAILING_BREAK_HTML
293
371
  };
372
+ if (index < 0) children[0] = {
373
+ type: "html",
374
+ value: LONE_BREAK_HTML
375
+ };
294
376
  }
295
377
  return {
296
378
  children,
@@ -298,4 +380,4 @@ const encodeMarkdownParagraph = (node, options) => {
298
380
  };
299
381
  };
300
382
  //#endregion
301
- export { BARE_URL_HTML, TRAILING_BREAK_HTML, encodeMarkdownParagraph, getMarkdownParagraphLanding, markdownIntrinsicDecoders, readEmptyParagraphMarker, remarkSeparateEscapes, writeBareUrlsSafely };
383
+ export { BARE_URL_HTML, LONE_BREAK_HTML, TRAILING_BREAK_HTML, encodeMarkdownParagraph, encodeRawHtmlLeaves, findMarkdownRawHtml, getMarkdownParagraphLanding, isMarkdownBreakHtml, isMarkdownHtmlComment, isRawHtmlFlow, markRawHtmlFlow, markdownIntrinsicDecoders, rawHtmlPieceOf, readEmptyParagraphMarker, remarkSeparateEscapes, wholeHtmlBlockRefusal, writeBareUrlsSafely };
@@ -1,6 +1,6 @@
1
1
  import { ElementApi, TextApi } from "../../../core.js";
2
2
  import { toMarkdownBlockContent } from "./markdownDocument.js";
3
- import { BARE_URL_HTML, readEmptyParagraphMarker } from "./markdownIntrinsics.js";
3
+ import { BARE_URL_HTML, LONE_BREAK_HTML, rawHtmlPieceOf, readEmptyParagraphMarker } from "./markdownIntrinsics.js";
4
4
  import { isMdPhrasingContent } from "../serializer/mdastContent.js";
5
5
  import { ReportedMarkdownFailureError } from "./markdownDiagnostics.js";
6
6
  import { getMarkdownDecoders, runMarkdownDecoders } from "./markdownMappings.js";
@@ -152,7 +152,8 @@ const lowerPhrasing = (children, line, beforeBreak = false) => children.flatMap(
152
152
  }];
153
153
  }
154
154
  case "html": {
155
- if (child.value === "\n<br />") return [html("<br />")];
155
+ if (rawHtmlPieceOf(child)) return [child];
156
+ if (child.value === "\n<br />" || child.value === LONE_BREAK_HTML) return [html("<br />")];
156
157
  const value = child.value.replace(LINE_ENDINGS, " ");
157
158
  if (value !== child.value) reportReplaced(line, "line endings in raw HTML became spaces.", "html");
158
159
  if (!value.includes("|")) return [{
@@ -364,6 +365,7 @@ const readListToken = (node) => {
364
365
  kind: "checkbox"
365
366
  };
366
367
  };
368
+ const isMarkdownLineListToken = (node) => readListToken(node) !== null;
367
369
  const isBlank = (node) => node?.type === "text" && /^[ \t\r\n]*$/.test(node.value);
368
370
  /**
369
371
  * Match every list tag of one line in a single pass, so malformed input stays
@@ -545,4 +547,4 @@ const decodeMarkdownLine = (nodes, marks, options) => {
545
547
  return blocks;
546
548
  };
547
549
  //#endregion
548
- export { decodeMarkdownLine, encodeMarkdownLine };
550
+ export { decodeMarkdownLine, encodeMarkdownLine, isMarkdownLineListToken };
@@ -616,11 +616,22 @@ const refusedTagText = (node, options) => {
616
616
  }];
617
617
  };
618
618
  const getMarkdownDecoders = (compiled, node) => isTagNode(node) ? compiled.decodeByTag.get(node.name ?? "") : compiled.decodeByNode.get(node.type);
619
- /**
620
- * Claim decoders run in priority order until one claims or refuses. Mark
621
- * decoders all contribute to one mark set, then the children decode once.
622
- * `undefined` means no decoder accepted the node.
623
- */
619
+ const isValidFragmentNode = (node, options) => {
620
+ try {
621
+ options.state.schema.assertFragment([node]);
622
+ return true;
623
+ } catch {
624
+ return false;
625
+ }
626
+ };
627
+ const withInlineText = (decoded, options) => decoded.map((item) => {
628
+ if (!ElementApi.isElement(item) || item.children.length > 0 || !options.isInline(item)) return item;
629
+ const filled = {
630
+ ...item,
631
+ children: [{ text: "" }]
632
+ };
633
+ return !isValidFragmentNode(item, options) && isValidFragmentNode(filled, options) ? filled : item;
634
+ });
624
635
  const runMarkdownDecoders = (decoders, node, marks, options, previousSibling = null) => {
625
636
  if (decoders[0]?.mark) {
626
637
  let marked;
@@ -636,9 +647,10 @@ const runMarkdownDecoders = (decoders, node, marks, options, previousSibling = n
636
647
  }
637
648
  if (!marked) return void 0;
638
649
  if ("children" in node && Array.isArray(node.children)) return convertChildrenDeserialize(node.children, marked, options);
650
+ const value = "value" in node && typeof node.value === "string" ? node.value : "";
639
651
  return [{
640
652
  ...marked,
641
- text: "value" in node && typeof node.value === "string" ? node.value : ""
653
+ text: node.type === "inlineCode" ? value.replaceAll(/\r\n?|\n/g, " ") : value
642
654
  }];
643
655
  }
644
656
  for (const decoder of decoders) {
@@ -658,7 +670,7 @@ const runMarkdownDecoders = (decoders, node, marks, options, previousSibling = n
658
670
  });
659
671
  return isTagNode(node) ? refusedTagText(node, options) : [];
660
672
  }
661
- if (decoded !== void 0) return Array.isArray(decoded) ? decoded : [decoded];
673
+ if (decoded !== void 0) return withInlineText(Array.isArray(decoded) ? decoded : [decoded], options);
662
674
  }
663
675
  };
664
676
  //#endregion