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.
- package/README.md +1 -1
- package/THIRD_PARTY_NOTICES.md +56 -0
- package/dist/{AIChatRequestContext-BMVWhfPV.d.ts → AIChatRequestContext-DrUahdTc.d.ts} +2 -2
- package/dist/{BaseSuggestionPlugin-DBl3fqnm.d.ts → BaseSuggestionPlugin-_7Ka6nY3.d.ts} +7 -3
- package/dist/{PlateStatic-DhNv2ejp.d.ts → PlateStatic-D3Cb-QwH.d.ts} +7 -7
- package/dist/ai/index.d.ts +1 -1
- package/dist/ai/react/AIChatPlugin.js +113 -78
- package/dist/ai/react/CopilotPlugin.js +1 -1
- package/dist/ai/react/index.d.ts +23 -16
- package/dist/authored/index.d.ts +4 -4
- package/dist/code-block/codemirror/createCodeMirrorAdapter.js +12 -9
- package/dist/code-block/codemirror/index.d.ts +1 -1
- package/dist/{combobox-DkBGgyd3.d.ts → combobox-Dr0OYZIE.d.ts} +2 -2
- package/dist/{comparison-types-CYnJ35qU.d.ts → comparison-types-DdwE6mYq-BogsHoUO.d.ts} +10 -2
- package/dist/compiler/index.d.ts +2 -2
- package/dist/{core-BJG8l_mk.d.ts → core-BqR-fEDp.d.ts} +493 -116
- package/dist/diff/index.d.ts +2 -2
- package/dist/docs/ai.md +4 -3
- package/dist/docs/api/core/plate-components.md +0 -19
- package/dist/docs/api/core/plate-controller.md +5 -3
- package/dist/docs/api/core/plate-plugin.md +3 -0
- package/dist/docs/api/core/plate-store.md +3 -3
- package/dist/docs/api/core.md +45 -5
- package/dist/docs/api/dom.md +11 -4
- package/dist/docs/api/editor-transforms.md +2 -1
- package/dist/docs/api/react-hooks.md +45 -13
- package/dist/docs/authored-changes.md +9 -4
- package/dist/docs/basic-blocks.md +42 -29
- package/dist/docs/block-placeholder.md +4 -2
- package/dist/docs/blockquote.md +5 -3
- package/dist/docs/board.md +118 -10
- package/dist/docs/code-block.md +47 -0
- package/dist/docs/comment.md +2 -2
- package/dist/docs/discussion.md +1 -1
- package/dist/docs/document-model.md +15 -3
- package/dist/docs/docx.md +1 -1
- package/dist/docs/dom-coverage.md +5 -0
- package/dist/docs/editor.md +50 -4
- package/dist/docs/examples/editable-voids.md +2 -3
- package/dist/docs/external-text.md +2 -0
- package/dist/docs/field.md +370 -0
- package/dist/docs/find.md +9 -6
- package/dist/docs/form.md +2 -0
- package/dist/docs/heading.md +6 -0
- package/dist/docs/installation/manual.md +0 -4
- package/dist/docs/link.md +3 -3
- package/dist/docs/llms.txt +4 -2
- package/dist/docs/markdown.md +270 -14
- package/dist/docs/plugin-components.md +13 -10
- package/dist/docs/plugin-input-rules.md +66 -15
- package/dist/docs/prompt-editor.md +205 -0
- package/dist/docs/roots.md +8 -6
- package/dist/docs/rtl.md +6 -4
- package/dist/docs/selection.md +7 -4
- package/dist/docs/suggestion.md +1 -1
- package/dist/docs/toolbar.md +26 -30
- package/dist/docs/upload.md +106 -17
- package/dist/docx/export/index.d.ts +3 -3
- package/dist/docx/export/lib/docxStaticComponents.js +12 -2
- package/dist/docx/export/lib/exportDocx.js +46 -17
- package/dist/docx/export/lib/internal/xml-builder.js +11 -16
- package/dist/docx/export/lib/reviewPart.js +1 -1
- package/dist/docx/export/lib/reviewProjection.js +52 -10
- package/dist/docx/import/index.d.ts +4 -4
- package/dist/docx/paste/index.d.ts +4 -4
- package/dist/dom/index.d.ts +1 -6
- package/dist/dom/plite-dom.internal.js +2 -2
- package/dist/excalidraw/index.d.ts +3 -2
- package/dist/excalidraw/react/index.d.ts +3 -2
- package/dist/facade.js +2 -2
- package/dist/features/basic-nodes/lib/BaseBlockPlugins.js +13 -5
- package/dist/features/basic-nodes/lib/BaseHeadingPlugins.js +32 -10
- package/dist/features/board/index.d.ts +2 -2
- package/dist/features/board/lib/BaseBoardPlugin.js +14 -1
- package/dist/features/board/lib/boardStroke.internal.js +13 -1
- package/dist/features/board/lib/boardTypes.internal.js +16 -0
- package/dist/features/callout/index.d.ts +3 -2
- package/dist/features/code-block/lib/BaseCodeBlockPlugin.js +14 -5
- package/dist/features/combobox/index.d.ts +2 -2
- package/dist/features/comments/BaseCommentsPlugin.js +33 -17
- package/dist/features/comments/index.d.ts +1 -1
- package/dist/features/date/index.d.ts +3 -2
- package/dist/features/details/index.d.ts +3 -2
- package/dist/features/details/lib/BaseDetailsPlugin.js +25 -12
- package/dist/features/field/index.d.ts +2 -0
- package/dist/features/field/index.js +4 -0
- package/dist/features/field/lib/BaseFieldPlugin.js +360 -0
- package/dist/features/field/lib/applyFieldValues.js +188 -0
- package/dist/features/field/lib/fieldRules.internal.js +204 -0
- package/dist/features/find/index.d.ts +3 -2
- package/dist/features/footnote/index.d.ts +1 -1
- package/dist/features/ink/index.d.ts +1 -1
- package/dist/features/ink/lib/BaseInkPlugin.js +126 -103
- package/dist/features/ink/lib/inkCodec.internal.js +130 -0
- package/dist/features/ink/lib/inkGone.internal.js +229 -48
- package/dist/features/ink/lib/inkPlaces.internal.js +534 -64
- package/dist/features/layout/index.d.ts +1 -1
- package/dist/features/link/lib/BaseLinkPlugin.js +6 -4
- package/dist/features/lint/index.d.ts +3 -2
- package/dist/features/media/index.d.ts +2 -2
- package/dist/features/media/index.js +2 -2
- package/dist/features/media/lib/media/parseMediaUrl.js +11 -1
- package/dist/features/media/lib/media-embed/BaseMediaEmbedPlugin.js +206 -4
- package/dist/features/mention/index.d.ts +5 -4
- package/dist/features/suggestion/index.d.ts +1 -1
- package/dist/features/table/index.d.ts +1 -1
- package/dist/features/table/lib/BaseTablePlugin.js +64 -15
- package/dist/features/table/lib/internal/selection.js +4 -2
- package/dist/features/toc/index.d.ts +1 -1
- package/dist/features/upload/index.d.ts +2 -2
- package/dist/features/upload/lib/BaseUploadPlugin.js +464 -250
- package/dist/history/index.d.ts +2 -2
- package/dist/html/index.d.ts +3 -3
- package/dist/html/server/index.d.ts +1 -1
- package/dist/{htmlTypes-DjeFG3P8.d.ts → htmlTypes-eT2ZGDAj.d.ts} +2 -31
- package/dist/hyperscript/index.d.ts +1 -1
- package/dist/{index-DzD6BIFJ.d.ts → index-B33E1CRJ.d.ts} +295 -6
- package/dist/{index-Cl77E9yr.d.ts → index-B7moSod7.d.ts} +2 -2
- package/dist/{index-hsC_sOn2.d.ts → index-B8-5Sr7I.d.ts} +35 -5
- package/dist/{index-BiQerPJS.d.ts → index-B8QJDAmG.d.ts} +2 -2
- package/dist/{index-BBKGrsG3.d.ts → index-BTx5oK7k.d.ts} +1 -1
- package/dist/{index-iaZOhA76.d.ts → index-BWPcE4ac-Cto_IsNs.d.ts} +7 -1
- package/dist/{index-BU6k1Aev.d.ts → index-BXjLmx1n-CXk17buG.d.ts} +4781 -4584
- package/dist/{index-kLKMc-pI.d.ts → index-BXwU9QH6.d.ts} +2 -2
- package/dist/{index-DTgaSxKr.d.ts → index-BkwR-5hH.d.ts} +40 -4
- package/dist/{index-Bq3sdm3S.d.ts → index-BsV2McRr.d.ts} +5 -4
- package/dist/index-C9Fu-P4E.d.ts +773 -0
- package/dist/{index-D5W-p9bS.d.ts → index-Ca48r1ng.d.ts} +49 -4
- package/dist/{index-CcZbqPqk.d.ts → index-Cob_iBdu.d.ts} +116 -41
- package/dist/{index-Bxfvicyy.d.ts → index-CpY7Xnxw.d.ts} +4 -3
- package/dist/{index-BkR7-GLI.d.ts → index-DNQXzU-X.d.ts} +2 -2
- package/dist/{index-D1CFXA-i.d.ts → index-DPowErHa.d.ts} +471 -141
- package/dist/{index-DaM37gF6.d.ts → index-DShbzec2.d.ts} +38 -23
- package/dist/{index-DC_MNGyu.d.ts → index-DYdObeFQ.d.ts} +3 -3
- package/dist/{index-Bc2Yg_2j.d.ts → index-Dq3Jlj8O-BJ6NxZm0.d.ts} +2 -2
- package/dist/{index-CT7miqQN.d.ts → index-DwWQVwQt.d.ts} +29 -11
- package/dist/{index-xdmCBvlT.d.ts → index-JWgwL5l3.d.ts} +5 -2
- package/dist/{index-DLA2DUR2.d.ts → index-M781pyx9.d.ts} +4 -3
- package/dist/{index-DdIRduDG.d.ts → index-d7io3MzC.d.ts} +3 -2
- package/dist/index.d.ts +4 -4
- package/dist/{react/internal → internal}/penStroke.internal.js +1 -1
- package/dist/internal/plugin/OverridePlugin.js +101 -119
- package/dist/internal/plugin/collectPlateNodeMappings.js +5 -1
- package/dist/internal/plugin/pastedUploadFiles.js +4 -0
- package/dist/internal/plugin/resolvePlugin.js +1 -1
- package/dist/internal/plugin/resolvePlugins.js +3 -0
- package/dist/internal/plugin/uploadDrafts.js +6 -0
- package/dist/internal/utils/mergePlugins.js +1 -0
- package/dist/lib/plugins/element-id/ElementIdPlugin.js +14 -18
- package/dist/lib/plugins/html/HtmlPlugin.js +9 -0
- package/dist/lib/plugins/html/htmlSafety.js +1 -1
- package/dist/lib/plugins/input-rules/InputRulesPlugin.js +77 -28
- package/dist/lib/plugins/input-rules/inputRuleVeto.internal.js +16 -0
- package/dist/markdown/index.d.ts +2 -2
- package/dist/markdown/index.js +3 -1
- package/dist/markdown/lib/MarkdownPlugin.js +14 -11
- package/dist/markdown/lib/RawHtmlPlugin.js +26 -0
- package/dist/markdown/lib/deserializer/mdastToSlate.js +12 -2
- package/dist/markdown/lib/internal/markdownConversion.js +776 -74
- package/dist/markdown/lib/internal/markdownDiagnostics.js +18 -7
- package/dist/markdown/lib/internal/markdownFootnoteWriter.js +35 -0
- package/dist/markdown/lib/internal/markdownFrontmatter.js +102 -0
- package/dist/markdown/lib/internal/markdownIntrinsics.js +125 -43
- package/dist/markdown/lib/internal/markdownLine.js +5 -3
- package/dist/markdown/lib/internal/markdownMappings.js +19 -7
- package/dist/markdown/lib/internal/markdownOpenTail.js +381 -0
- package/dist/markdown/lib/internal/markdownRawHtml.js +353 -0
- package/dist/markdown/lib/internal/markdownSegments.js +57 -48
- package/dist/markdown/lib/internal/markdownTags.js +29 -9
- package/dist/markdown/lib/serializer/convertNodesSerialize.js +10 -8
- package/dist/markdown/lib/serializer/convertPhrasingSerialize.js +226 -0
- package/dist/markdown/lib/serializer/listToMdastTree.js +2 -0
- package/dist/math/index.d.ts +3 -2
- package/dist/math/react/index.d.ts +4 -3
- package/dist/migrations/index.d.ts +3 -3
- package/dist/migrations/migratePlateV54.js +4 -6
- package/dist/migrations/migratePlateV54RetiredMarks.internal.js +23 -8
- package/dist/migrations/migratePlateV54Suggestions.internal.js +2 -2
- package/dist/migrations/v53-manifest.internal.js +4 -0
- package/dist/pagination/index.d.ts +1 -1
- package/dist/pagination/react/index.d.ts +4 -4
- package/dist/react/components/EditorContentView.internal.js +42 -23
- package/dist/react/components/EditorProvider.js +5 -5
- package/dist/react/components/Plate.js +102 -128
- package/dist/react/components/PlateContainer.js +40 -32
- package/dist/react/components/PlateController.js +28 -16
- package/dist/react/components/PlateRoot.internal.js +3 -2
- package/dist/react/core.js +4 -4
- package/dist/react/features/board/BoardSurface.js +116 -87
- package/dist/react/features/board/boardClipboard.internal.js +15 -5
- package/dist/react/features/board/boardExport.internal.js +470 -0
- package/dist/react/features/board/boardGesture.internal.js +6 -3
- package/dist/react/features/board/boardIndex.internal.js +7 -18
- package/dist/react/features/board/boardPictureStyles.internal.js +415 -0
- package/dist/react/features/board/boardPictures.internal.js +19 -13
- package/dist/react/features/board/boardView.internal.js +190 -50
- package/dist/react/features/board/boardVoice.internal.js +10 -11
- package/dist/react/features/board/index.d.ts +108 -18
- package/dist/react/features/callout/index.d.ts +3 -2
- package/dist/react/features/combobox/index.d.ts +8 -8
- package/dist/react/features/comments/index.d.ts +6 -5
- package/dist/react/features/date/index.d.ts +4 -3
- package/dist/react/features/details/index.d.ts +4 -3
- package/dist/react/features/field/FieldPlugin.js +35 -0
- package/dist/react/features/field/index.d.ts +315 -0
- package/dist/react/features/field/index.js +2 -0
- package/dist/react/features/footnote/index.d.ts +7 -6
- package/dist/react/features/format-painter/index.d.ts +4 -3
- package/dist/react/features/ink/InkContent.js +313 -0
- package/dist/react/features/ink/index.d.ts +57 -33
- package/dist/react/features/ink/index.js +2 -2
- package/dist/react/features/ink/inkCapture.internal.js +39 -17
- package/dist/react/features/ink/inkInput.internal.js +1 -1
- package/dist/react/features/ink/inkLayout.internal.js +6 -3
- package/dist/react/features/ink/inkPaint.internal.js +78 -2
- package/dist/react/features/ink/inkPlacement.internal.js +332 -104
- package/dist/react/features/ink/inkWatch.internal.js +7 -5
- package/dist/react/features/layout/index.d.ts +5 -4
- package/dist/react/features/media/index.d.ts +5 -4
- package/dist/react/features/mention/index.d.ts +6 -5
- package/dist/react/features/resizable/Resizable.js +4 -6
- package/dist/react/features/resizable/index.d.ts +5 -5
- package/dist/react/features/suggestion/index.d.ts +6 -5
- package/dist/react/features/table/TablePlugin.js +4 -11
- package/dist/react/features/table/index.d.ts +10 -8
- package/dist/react/features/table/useTableResize.js +10 -1
- package/dist/react/features/toc/index.d.ts +2 -2
- package/dist/react/features/upload/index.d.ts +11 -6
- package/dist/react/index.d.ts +6 -6
- package/dist/react/index.js +4 -4
- package/dist/react/internal/plate-context.js +434 -188
- package/dist/react/plite-react.js +2 -2
- package/dist/react/plugins/navigation-feedback/NavigationFeedbackPlugin.js +12 -10
- package/dist/react/stores/plate/useEditor.js +32 -14
- package/dist/react/stores/plate/useEditorHistory.js +55 -0
- package/dist/react/stores/plate/useEditorSelector.js +79 -21
- package/dist/react/utils/BlockPlaceholderPlugin.js +5 -2
- package/dist/react/utils/pipeRenderElement.internal.js +1 -1
- package/dist/react/utils/pluginRenderElement.internal.js +1 -1
- package/dist/react/virtualized.d.ts +4 -4
- package/dist/{source-CNKy99Th.d.ts → source-BGohy8vH.d.ts} +4 -4
- package/dist/static/index.d.ts +18 -18
- package/dist/static/internal/renderStaticHtmlWithOverrides.js +2 -2
- package/dist/static/internal/staticPresentation.js +4 -4
- package/dist/static/internal/writeStaticSelectionClipboardData.js +7 -2
- package/dist/tabbable/index.d.ts +1 -1
- package/dist/tabbable/react/index.d.ts +3 -3
- package/dist/testing/index.d.ts +3 -3
- package/dist/yjs/index.d.ts +1 -1
- package/dist/yjs/react/index.d.ts +6 -6
- package/package.json +20 -2
- package/dist/markdown/lib/serializer/convertTextsSerialize.js +0 -163
- package/dist/react/components/EditorSelectionView.js +0 -40
- package/dist/react/features/ink/InkLayer.js +0 -218
- package/dist/react/internal/createPlateTargetScope.js +0 -62
|
@@ -0,0 +1,353 @@
|
|
|
1
|
+
import { decideUrl } from "../../../internal/utils/urlPolicy.js";
|
|
2
|
+
import { HTML_SURROUNDING_WHITESPACE, decideHtmlAttribute } from "../../../lib/plugins/html/htmlSafety.js";
|
|
3
|
+
import { getMarkdownTagRepair } from "./markdownTags.js";
|
|
4
|
+
import { encodeRawHtmlLeaves, isMarkdownBreakHtml, isMarkdownHtmlComment, markRawHtmlFlow, rawHtmlPieceOf, wholeHtmlBlockRefusal } from "./markdownIntrinsics.js";
|
|
5
|
+
import { isMarkdownLineListToken } from "./markdownLine.js";
|
|
6
|
+
import { Tokenizer } from "parse5";
|
|
7
|
+
//#region src/markdown/lib/internal/markdownRawHtml.ts
|
|
8
|
+
const RAW_HTML_TAGS = new Set(`a abbr b bdo blockquote br caption center cite code col colgroup dd del
|
|
9
|
+
details dfn div dl dt em figcaption figure h1 h2 h3 h4 h5 h6 hr i img
|
|
10
|
+
ins kbd li mark ol p picture pre q rp rt ruby s samp small source span
|
|
11
|
+
strike strong sub summary sup table tbody td tfoot th thead tr tt u ul
|
|
12
|
+
var wbr`.split(/\s+/));
|
|
13
|
+
const RAW_HTML_ATTRIBUTES = new Set(`align alt border cellpadding cellspacing cite colspan datetime dir
|
|
14
|
+
height href lang media open rel reversed rowspan scope src srcset start
|
|
15
|
+
target title type valign width`.split(/\s+/));
|
|
16
|
+
const PASS = Object.freeze({ ok: true });
|
|
17
|
+
const refuse = (reason) => Object.freeze({
|
|
18
|
+
ok: false,
|
|
19
|
+
reason
|
|
20
|
+
});
|
|
21
|
+
const hasOnlySafeImageCandidates = (srcset) => {
|
|
22
|
+
const nextUrl = /[\t\n\f\r ,]*([^\t\n\f\r ,][^\t\n\f\r ]*)/y;
|
|
23
|
+
const atMostOneDescriptor = /[\t\n\f\r ]*(?:(?:0*[1-9]\d*w|(-?(?:\d+(?:\.\d+)?|\.\d+)(?:[Ee][+-]?\d+)?)x)[\t\n\f\r ]*)?(?:,|$)/y;
|
|
24
|
+
let count = 0;
|
|
25
|
+
for (let candidate = nextUrl.exec(srcset); candidate; candidate = nextUrl.exec(srcset)) {
|
|
26
|
+
const url = candidate[1].replace(/,+$/, "");
|
|
27
|
+
if (!decideUrl("image", url).ok) return false;
|
|
28
|
+
if (url === candidate[1]) {
|
|
29
|
+
atMostOneDescriptor.lastIndex = nextUrl.lastIndex;
|
|
30
|
+
const descriptor = atMostOneDescriptor.exec(srcset);
|
|
31
|
+
const density = Number(descriptor?.[1] ?? 0);
|
|
32
|
+
if (!descriptor || !(Number.isFinite(density) && density >= 0)) return false;
|
|
33
|
+
nextUrl.lastIndex = atMostOneDescriptor.lastIndex;
|
|
34
|
+
}
|
|
35
|
+
count += 1;
|
|
36
|
+
}
|
|
37
|
+
return count > 0;
|
|
38
|
+
};
|
|
39
|
+
const decideRawHtmlAttribute = (tag, name, value) => {
|
|
40
|
+
if (!(RAW_HTML_ATTRIBUTES.has(name) || name === "name" && tag === "a")) return `attr:${name}`;
|
|
41
|
+
if (name === "cite") return decideUrl("navigation", value).ok ? void 0 : "url:cite";
|
|
42
|
+
if (name === "srcset") return hasOnlySafeImageCandidates(value) ? void 0 : "url:srcset";
|
|
43
|
+
const decision = decideHtmlAttribute(tag, name, value);
|
|
44
|
+
return !decision || "value" in decision && decision.value === value.replaceAll(HTML_SURROUNDING_WHITESPACE, "") ? void 0 : `safety:${name}`;
|
|
45
|
+
};
|
|
46
|
+
const decideRawHtml = (value) => {
|
|
47
|
+
let refusal;
|
|
48
|
+
const ignore = () => {};
|
|
49
|
+
new Tokenizer({}, {
|
|
50
|
+
onCharacter: ignore,
|
|
51
|
+
onComment: ignore,
|
|
52
|
+
onDoctype: () => {
|
|
53
|
+
refusal ??= "doctype";
|
|
54
|
+
},
|
|
55
|
+
onEndTag: ({ tagName }) => {
|
|
56
|
+
if (!RAW_HTML_TAGS.has(tagName)) refusal ??= `tag:${tagName}`;
|
|
57
|
+
},
|
|
58
|
+
onEof: ignore,
|
|
59
|
+
onNullCharacter: ignore,
|
|
60
|
+
onParseError: ({ code }) => {
|
|
61
|
+
refusal ??= `parse:${code}`;
|
|
62
|
+
},
|
|
63
|
+
onStartTag: ({ attrs, tagName }) => {
|
|
64
|
+
if (!RAW_HTML_TAGS.has(tagName)) refusal ??= `tag:${tagName}`;
|
|
65
|
+
for (const { name, value: attribute } of attrs) refusal ??= decideRawHtmlAttribute(tagName, name, attribute);
|
|
66
|
+
},
|
|
67
|
+
onWhitespaceCharacter: ignore
|
|
68
|
+
}).write(value, true);
|
|
69
|
+
return refusal ? refuse(refusal) : PASS;
|
|
70
|
+
};
|
|
71
|
+
const INLINE_MARKUP_AFTER_LT = /[A-Za-z][A-Za-z\d-]*(?:\s*\/?>|\s+[A-Za-z_:])|\/[A-Za-z][A-Za-z\d-]*\s*>|!(?:--|[A-Za-z]|\[CDATA\[)|\?|[A-Za-z][A-Za-z\d+.-]{1,31}:[^\0- <>]*(?:>|$)/.source;
|
|
72
|
+
const MARKUP_LT = new RegExp(`<(?=${INLINE_MARKUP_AFTER_LT})`, "g");
|
|
73
|
+
const TEXT_LT = new RegExp(`<(?!${INLINE_MARKUP_AFTER_LT})`, "g");
|
|
74
|
+
const TO_END_TAG_LINE = /^([\t ]{0,3})<(?=(?:pre|script|style|textarea)(?:[\s>]|$))/gim;
|
|
75
|
+
const TEX_INDENT = " ";
|
|
76
|
+
const TEXT_LINE = /^(?=.*\S)/gm;
|
|
77
|
+
const INDENTED_TEXT_LINE = /^ {4}(?=.*\S)/gm;
|
|
78
|
+
const isIndentedTex = (value) => {
|
|
79
|
+
const lines = value.split("\n").filter((line) => /\S/.test(line));
|
|
80
|
+
return lines.length > 0 && lines.every((line) => line.startsWith(TEX_INDENT));
|
|
81
|
+
};
|
|
82
|
+
/**
|
|
83
|
+
* A reader without math reads `$…$` and a `$$` block as Markdown text, where
|
|
84
|
+
* TeX such as `<img src=x onerror=alert(1)>` is raw HTML and
|
|
85
|
+
* `<javascript:alert(1)>` is a link, and a tag the TeX leaves open takes its
|
|
86
|
+
* `>` from the text after it. TeX whose tags the raw HTML rule refuses writes
|
|
87
|
+
* a space after each `<` that could start a tag, which TeX ignores in math
|
|
88
|
+
* mode. A `$$` block that CommonMark does not read as one paragraph, such as
|
|
89
|
+
* TeX holding a blank line and then `[a]: javascript:alert(1)`, indents each
|
|
90
|
+
* TeX line that holds text by four spaces, so every line reads as paragraph
|
|
91
|
+
* text or indented code.
|
|
92
|
+
*/
|
|
93
|
+
const writeMathSafely = (node, read) => {
|
|
94
|
+
if ((node.type === "math" || node.type === "inlineMath") && node.value.includes("<")) {
|
|
95
|
+
const after = node.type === "math" ? "\n$$" : "$";
|
|
96
|
+
const written = `${node.value}${after}`;
|
|
97
|
+
if (!decideRawHtml(written.replaceAll(TEXT_LT, "<")).ok) node.value = written.replaceAll(MARKUP_LT, "< ").slice(0, -after.length);
|
|
98
|
+
}
|
|
99
|
+
if (node.type === "math") {
|
|
100
|
+
node.value = node.value.replaceAll(TO_END_TAG_LINE, "$1< ");
|
|
101
|
+
const [first, ...rest] = read(`$$\n${node.value}\n$$`).children;
|
|
102
|
+
if (/\S/.test(node.value) && (first?.type !== "paragraph" || rest.length > 0) || isIndentedTex(node.value)) node.value = node.value.replaceAll(TEXT_LINE, TEX_INDENT);
|
|
103
|
+
}
|
|
104
|
+
if ("children" in node) for (const child of node.children) writeMathSafely(child, read);
|
|
105
|
+
};
|
|
106
|
+
const readMathSafely = (node) => {
|
|
107
|
+
if (node.type === "math" && isIndentedTex(node.value)) node.value = node.value.replaceAll(INDENTED_TEXT_LINE, "");
|
|
108
|
+
if ("children" in node) node.children.forEach(readMathSafely);
|
|
109
|
+
};
|
|
110
|
+
const ONE_LINE_CONTAINERS = /* @__PURE__ */ new Set([
|
|
111
|
+
"atxHeading",
|
|
112
|
+
"registeredTag",
|
|
113
|
+
"setextHeading",
|
|
114
|
+
"tableCell"
|
|
115
|
+
]);
|
|
116
|
+
const PREFIXED_CONTAINERS = /* @__PURE__ */ new Set([
|
|
117
|
+
"atxHeading",
|
|
118
|
+
"tableCell",
|
|
119
|
+
"taskItemFirstParagraph"
|
|
120
|
+
]);
|
|
121
|
+
const isRawHtml = (node) => node.type === "html" && !getMarkdownTagRepair(node) && !isMarkdownBreakHtml(node.value);
|
|
122
|
+
const readsAsHtmlBlock = (read, piece) => {
|
|
123
|
+
const [html, paragraph, ...rest] = read(`${piece}\n\nx`).children;
|
|
124
|
+
return html !== void 0 && isRawHtml(html) && html.value === piece && paragraph?.type === "paragraph" && rest.length === 0;
|
|
125
|
+
};
|
|
126
|
+
const decideInlineRead = (read, piece, container, before, after) => {
|
|
127
|
+
const [paragraph, ...rest] = read(`${before}${piece}${after}`).children;
|
|
128
|
+
if (paragraph?.type !== "paragraph" || rest.length > 0) return before ? "block-shape" : "opens-block";
|
|
129
|
+
let html = "";
|
|
130
|
+
let text = "";
|
|
131
|
+
for (const child of paragraph.children) if (isRawHtml(child) && !(container === "tableCell" && isMarkdownLineListToken(child))) {
|
|
132
|
+
if (isMarkdownHtmlComment(child.value)) return "comment";
|
|
133
|
+
html += child.value;
|
|
134
|
+
} else if (child.type === "text") text += child.value;
|
|
135
|
+
else return "inline-shape";
|
|
136
|
+
return html === piece && text === before + after ? void 0 : "inline-shape";
|
|
137
|
+
};
|
|
138
|
+
/**
|
|
139
|
+
* Create the placement rule for one import or export call. The returned
|
|
140
|
+
* function decides whether a piece writes as raw HTML at its place: the piece
|
|
141
|
+
* must pass `decideRawHtml`, and plain CommonMark and the editor's import
|
|
142
|
+
* reader must each read it there as raw HTML and nothing else. A whole block
|
|
143
|
+
* in the document, a quote or a plain list item is read as an HTML block that
|
|
144
|
+
* a blank line ends. Any other piece is read inside one paragraph between
|
|
145
|
+
* stand-ins for its neighbors, so it can neither open a block nor turn into
|
|
146
|
+
* Markdown syntax. Decisions are cached by piece and place for the life of the
|
|
147
|
+
* returned function, so create one per call.
|
|
148
|
+
*/
|
|
149
|
+
const createRawHtmlPlacementDecider = ({ commonmark, editor }) => {
|
|
150
|
+
const readers = [commonmark, editor];
|
|
151
|
+
const decisions = /* @__PURE__ */ new Map();
|
|
152
|
+
const decideOnce = (key, piece, decideRead) => {
|
|
153
|
+
const cached = decisions.get(key);
|
|
154
|
+
if (cached) return cached;
|
|
155
|
+
const safety = decideRawHtml(piece);
|
|
156
|
+
let refusal = safety.ok ? void 0 : safety.reason;
|
|
157
|
+
for (const read of readers) refusal ??= decideRead(read);
|
|
158
|
+
const decision = refusal ? refuse(refusal) : PASS;
|
|
159
|
+
decisions.set(key, decision);
|
|
160
|
+
return decision;
|
|
161
|
+
};
|
|
162
|
+
return (piece, place) => {
|
|
163
|
+
const { container } = place;
|
|
164
|
+
if (!piece) return refuse("empty");
|
|
165
|
+
if (piece.includes("\r")) return refuse("line-ending");
|
|
166
|
+
if (container === "footnoteDefinition") return refuse("footnote");
|
|
167
|
+
if (place.wholeBlock) {
|
|
168
|
+
const refusal = wholeHtmlBlockRefusal(piece, place.follows);
|
|
169
|
+
if (refusal) return refuse(refusal);
|
|
170
|
+
return decideOnce(`flow\u0000${piece}`, piece, (read) => readsAsHtmlBlock(read, piece) ? void 0 : "flow-shape");
|
|
171
|
+
}
|
|
172
|
+
if (isMarkdownHtmlComment(piece)) return refuse("comment");
|
|
173
|
+
if (/[`|]/.test(piece)) return refuse("char");
|
|
174
|
+
if (ONE_LINE_CONTAINERS.has(container) && piece.includes("\n")) return refuse("line-ending");
|
|
175
|
+
const { next, start } = place;
|
|
176
|
+
const before = start === "line" ? "x\n" : start === "mid" || PREFIXED_CONTAINERS.has(container) ? "x" : "";
|
|
177
|
+
const after = next === "text" ? "x" : next === "break" ? "\\" : "";
|
|
178
|
+
return decideOnce(`${container}\u0000${before}\u0000${after}\u0000${piece}`, piece, (read) => decideInlineRead(read, piece, container, before, after));
|
|
179
|
+
};
|
|
180
|
+
};
|
|
181
|
+
const FLOW_PARENTS = /* @__PURE__ */ new Set([
|
|
182
|
+
"blockquote",
|
|
183
|
+
"footnoteDefinition",
|
|
184
|
+
"list",
|
|
185
|
+
"listItem",
|
|
186
|
+
"mdxJsxFlowElement",
|
|
187
|
+
"table",
|
|
188
|
+
"tableRow"
|
|
189
|
+
]);
|
|
190
|
+
const isPiece = (node) => rawHtmlPieceOf(node) !== void 0;
|
|
191
|
+
const leavesOf = (node) => rawHtmlPieceOf(node)?.leaves ?? [];
|
|
192
|
+
const isChanged = (node) => node.value !== leavesOf(node).map((leaf) => leaf.text).join("");
|
|
193
|
+
const isBreak = (node) => node.type === "break" || node.type === "html" && isMarkdownBreakHtml(node.value);
|
|
194
|
+
const isText = (node) => node.type === "html" || node.type === "text";
|
|
195
|
+
const printsNoText = (node) => isText(node) ? node.value === "" : node.type === "table" ? node.children.every((row) => row.children.length === 0) : node.type === "list" || node.type === "tableRow" ? node.children.length === 0 : (node.type === "emphasis" || node.type === "paragraph" || node.type === "strong" || node.type === "tableCell") && node.children.every(printsNoText);
|
|
196
|
+
const isWhitespace = (node) => node.type === "text" && /^[\t ]+$/.test(node.value);
|
|
197
|
+
const isBlank = (node) => printsNoText(node) || isWhitespace(node);
|
|
198
|
+
const holdsHtmlLineEnding = (node) => node.type === "html" && /[\n\r]/.test(node.value) || "children" in node && node.children.some(holdsHtmlLineEnding);
|
|
199
|
+
const holdsLineEnding = (node, ending) => node.type === "break" || "value" in node && ending.test(node.value) || "children" in node && node.children.some((child) => holdsLineEnding(child, ending));
|
|
200
|
+
/**
|
|
201
|
+
* Merge each run of adjacent unchanged pieces in phrasing into one node that
|
|
202
|
+
* holds all their leaves. The writer turns a line ending before most `html`
|
|
203
|
+
* nodes into a space, so only one node prints the leaves' own text.
|
|
204
|
+
*/
|
|
205
|
+
const mergeAdjacentPieces = (parent) => {
|
|
206
|
+
const children = parent.children;
|
|
207
|
+
const phrasing = parent.type !== "root" && !FLOW_PARENTS.has(parent.type);
|
|
208
|
+
for (let index = 0; index < children.length; index += 1) {
|
|
209
|
+
const child = children[index];
|
|
210
|
+
if ("children" in child) mergeAdjacentPieces(child);
|
|
211
|
+
if (!phrasing) continue;
|
|
212
|
+
let end = index;
|
|
213
|
+
while (end < children.length && isPiece(children[end])) end += 1;
|
|
214
|
+
const run = children.slice(index, end);
|
|
215
|
+
if (run.length < 2) continue;
|
|
216
|
+
if (run.some(isChanged)) index = end - 1;
|
|
217
|
+
else children.splice(index, run.length, encodeRawHtmlLeaves(run.flatMap(leavesOf)));
|
|
218
|
+
}
|
|
219
|
+
};
|
|
220
|
+
/**
|
|
221
|
+
* Find each piece of marked text on the tree the writer prints, and its place
|
|
222
|
+
* on the lines it prints there. A piece is one `html` node that marked leaves
|
|
223
|
+
* encode.
|
|
224
|
+
*/
|
|
225
|
+
const findRawHtmlPieces = (tree, { previousKind, setext, unwritable }) => {
|
|
226
|
+
const pieces = [];
|
|
227
|
+
const pushPiece = (node, block, place = "unmodeled") => {
|
|
228
|
+
pieces.push({
|
|
229
|
+
block,
|
|
230
|
+
changed: isChanged(node),
|
|
231
|
+
node,
|
|
232
|
+
place
|
|
233
|
+
});
|
|
234
|
+
};
|
|
235
|
+
const visitUnknown = (parent, block) => {
|
|
236
|
+
for (const child of parent.children) if (isPiece(child)) pushPiece(child, block);
|
|
237
|
+
else if ("children" in child) visitUnknown(child, block);
|
|
238
|
+
};
|
|
239
|
+
const visitPhrasing = (parent, container, leaf, writing) => {
|
|
240
|
+
const children = parent.children;
|
|
241
|
+
const atLeaf = parent === leaf;
|
|
242
|
+
const last = children.at(-1);
|
|
243
|
+
const endsInReference = atLeaf && writing.finalReference && last !== void 0 && isWhitespace(last);
|
|
244
|
+
let before;
|
|
245
|
+
for (const [index, child] of children.entries()) {
|
|
246
|
+
if (!isPiece(child)) {
|
|
247
|
+
if ("children" in child) visitPhrasing(child, child.type === "mdxJsxTextElement" && container !== "footnoteDefinition" ? "registeredTag" : container, leaf, writing);
|
|
248
|
+
if (!printsNoText(child)) before = child;
|
|
249
|
+
continue;
|
|
250
|
+
}
|
|
251
|
+
let afterIndex = index + 1;
|
|
252
|
+
while (afterIndex < children.length && isBlank(children[afterIndex])) afterIndex += 1;
|
|
253
|
+
const after = children.at(afterIndex);
|
|
254
|
+
const nextOf = () => {
|
|
255
|
+
if (!after) return !atLeaf || endsInReference ? "text" : "none";
|
|
256
|
+
if (writing.lineEndings && isText(after) && /^[\n\r]/.test(after.value)) return "none";
|
|
257
|
+
if (!isBreak(after) || leaf.type === "tableCell") return "text";
|
|
258
|
+
return leaf.type === "heading" ? "none" : "break";
|
|
259
|
+
};
|
|
260
|
+
const startOf = () => {
|
|
261
|
+
if (!before) return atLeaf ? "block" : "mid";
|
|
262
|
+
return writing.lineEndings && (before.type === "break" || isText(before) && /[\n\r]$/.test(before.value)) ? "line" : "mid";
|
|
263
|
+
};
|
|
264
|
+
pushPiece(child, leaf, {
|
|
265
|
+
container,
|
|
266
|
+
next: nextOf(),
|
|
267
|
+
start: startOf()
|
|
268
|
+
});
|
|
269
|
+
before = child;
|
|
270
|
+
}
|
|
271
|
+
};
|
|
272
|
+
const visitFlow = (parent, container, outer) => {
|
|
273
|
+
const inFootnote = outer === "footnoteDefinition" ? outer : void 0;
|
|
274
|
+
let follows = parent.type === "listItem" ? "marker" : parent === tree && previousKind === "list" ? "list" : "block";
|
|
275
|
+
for (const [index, child] of parent.children.entries()) {
|
|
276
|
+
const flowPlace = {
|
|
277
|
+
container,
|
|
278
|
+
follows,
|
|
279
|
+
wholeBlock: true
|
|
280
|
+
};
|
|
281
|
+
if (isPiece(child)) pushPiece(child, child, outer ? {
|
|
282
|
+
container: outer,
|
|
283
|
+
next: "none",
|
|
284
|
+
start: "block"
|
|
285
|
+
} : flowPlace);
|
|
286
|
+
else if (child.type === "heading") {
|
|
287
|
+
const setextHeading = child.depth < 3 && (setext || holdsLineEnding(child, /[\n\r]/));
|
|
288
|
+
if (!setextHeading && holdsHtmlLineEnding(child)) visitUnknown(child, child);
|
|
289
|
+
else visitPhrasing(child, inFootnote ?? (setextHeading ? "setextHeading" : "atxHeading"), child, {
|
|
290
|
+
finalReference: true,
|
|
291
|
+
lineEndings: setextHeading
|
|
292
|
+
});
|
|
293
|
+
} else if (child.type === "paragraph") {
|
|
294
|
+
const taskItemFirstParagraph = index === 0 && parent.type === "listItem" && typeof parent.checked === "boolean";
|
|
295
|
+
const onTagLine = parent.type === "mdxJsxFlowElement" && parent.children.length === 1 && !holdsLineEnding(child, /\n/);
|
|
296
|
+
const first = pieces.length;
|
|
297
|
+
visitPhrasing(child, outer ?? (taskItemFirstParagraph ? "taskItemFirstParagraph" : container), child, {
|
|
298
|
+
finalReference: !onTagLine,
|
|
299
|
+
lineEndings: true
|
|
300
|
+
});
|
|
301
|
+
if (!outer && !taskItemFirstParagraph && child.children.length > 0 && child.children.every(isPiece)) pieces[first] = {
|
|
302
|
+
...pieces[first],
|
|
303
|
+
wholeParagraph: {
|
|
304
|
+
paragraph: child,
|
|
305
|
+
place: flowPlace
|
|
306
|
+
}
|
|
307
|
+
};
|
|
308
|
+
} else if (child.type === "tableCell" && (parent.type !== "tableRow" || holdsHtmlLineEnding(child))) visitUnknown(child, child);
|
|
309
|
+
else if (child.type === "tableCell") visitPhrasing(child, inFootnote ?? "tableCell", child, {
|
|
310
|
+
finalReference: false,
|
|
311
|
+
lineEndings: false
|
|
312
|
+
});
|
|
313
|
+
else if (FLOW_PARENTS.has(child.type) && "children" in child) visitFlow(child, child.type === "blockquote" ? "quote" : child.type === "listItem" ? "listItem" : container, child.type === "footnoteDefinition" ? child.type : child.type === "mdxJsxFlowElement" ? outer ?? "registeredTag" : outer);
|
|
314
|
+
else if ("children" in child) visitUnknown(child, child);
|
|
315
|
+
if (!printsNoText(child) && !unwritable?.has(child.type)) follows = child.type === "list" ? "list" : "block";
|
|
316
|
+
}
|
|
317
|
+
};
|
|
318
|
+
visitFlow(tree, "document");
|
|
319
|
+
return pieces;
|
|
320
|
+
};
|
|
321
|
+
/**
|
|
322
|
+
* Decide each block of marked text on the tree the writer prints. A block is
|
|
323
|
+
* the nearest paragraph, heading or table cell, a flow `html` node, or a node
|
|
324
|
+
* in flow position whose printing the decision does not model. It fails when
|
|
325
|
+
* any piece in it fails, and a piece fails when its node no longer holds its
|
|
326
|
+
* leaves' text, when its block is one the decision does not model, or when
|
|
327
|
+
* the placement rule refuses it at its place. A paragraph that holds only one
|
|
328
|
+
* piece also passes as an HTML block, which the writer then sets apart with
|
|
329
|
+
* blank lines, or from the next item of its list with that item's marker line.
|
|
330
|
+
*/
|
|
331
|
+
const decideRawHtmlBlocks = (tree, decide, options) => {
|
|
332
|
+
const blocks = /* @__PURE__ */ new Map();
|
|
333
|
+
mergeAdjacentPieces(tree);
|
|
334
|
+
for (const { block, changed, node, place, wholeParagraph } of findRawHtmlPieces(tree, options)) {
|
|
335
|
+
const decided = blocks.get(block) ?? { leaves: [] };
|
|
336
|
+
decided.leaves.push(...leavesOf(node));
|
|
337
|
+
blocks.set(block, decided);
|
|
338
|
+
if (decided.reason) continue;
|
|
339
|
+
if (changed) decided.reason = "changed";
|
|
340
|
+
else if (place === "unmodeled") decided.reason = "container";
|
|
341
|
+
else if (wholeParagraph && decide(node.value, wholeParagraph.place).ok) markRawHtmlFlow(wholeParagraph.paragraph);
|
|
342
|
+
else {
|
|
343
|
+
const decision = decide(node.value, place);
|
|
344
|
+
if (!decision.ok) decided.reason = decision.reason;
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
return [...blocks.values()].flatMap(({ leaves, reason }) => reason ? [{
|
|
348
|
+
leaves,
|
|
349
|
+
reason
|
|
350
|
+
}] : []);
|
|
351
|
+
};
|
|
352
|
+
//#endregion
|
|
353
|
+
export { createRawHtmlPlacementDecider, decideRawHtml, decideRawHtmlBlocks, printsNoText, readMathSafely, writeMathSafely };
|
|
@@ -1,20 +1,33 @@
|
|
|
1
|
+
import { markdownTextEnd } from "./markdownTags.js";
|
|
1
2
|
//#region src/markdown/lib/internal/markdownSegments.ts
|
|
2
3
|
const LIST = /^ {0,3}(?:[*+-]|\d{1,9}[.)])(?:[ \t]|$)/;
|
|
4
|
+
const LIST_START = /^\d{1,9}$/;
|
|
3
5
|
const INDENTED = /^(?: {4}|\t)/;
|
|
4
6
|
const LEADING_WHITESPACE = /^[ \t]/;
|
|
5
|
-
const
|
|
6
|
-
const
|
|
7
|
-
const
|
|
8
|
-
const RAW_HTML = /^ {0,3}<(
|
|
9
|
-
const
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
}
|
|
16
|
-
/**
|
|
17
|
-
|
|
7
|
+
const BLANK = /^[ \t]*$/;
|
|
8
|
+
const LINE_END = /\r\n?|\n/g;
|
|
9
|
+
const FENCE = /^ {0,3}(`{3,}|~{3,}|\${2,})/;
|
|
10
|
+
const RAW_HTML = [[/^ {0,3}<(?:pre|script|style|textarea)(?=[ \t>]|$)/i, /<\/(?:pre|script|style|textarea)>/i], [/^ {0,3}<!--/, /-->/]];
|
|
11
|
+
const BYTE_ORDER_MARK = "";
|
|
12
|
+
/**
|
|
13
|
+
* Link and footnote definitions resolve across the whole source. A definition
|
|
14
|
+
* can sit in a quote or list item, escape a bracket or break its label across
|
|
15
|
+
* lines. micromark skips a byte order mark that starts the source.
|
|
16
|
+
*/
|
|
17
|
+
const MARKDOWN_DEFINITION = /(?:^\uFEFF?|[\n\r])(?:[ \t>]|[*+-][ \t]|\d{1,9}[.)][ \t])*\[(?:[^[\]\\]|\\[\s\S])+\]:/;
|
|
18
|
+
/**
|
|
19
|
+
* Whether a continued parse reads `source` whole instead of reusing segments:
|
|
20
|
+
* a definition resolves across the whole source, and a segment without the
|
|
21
|
+
* source's carriage returns or leading byte order mark reads its line endings
|
|
22
|
+
* and offsets differently.
|
|
23
|
+
*/
|
|
24
|
+
const readsMarkdownWhole = (source) => MARKDOWN_DEFINITION.test(source) || source.includes("\r") || source.startsWith(BYTE_ORDER_MARK);
|
|
25
|
+
/** GFM's footnote syntax, which acts only in a source holding a definition. */
|
|
26
|
+
const DEFINITION_SYNTAX = [
|
|
27
|
+
"gfmFootnoteCall",
|
|
28
|
+
"gfmFootnoteDefinition",
|
|
29
|
+
"gfmPotentialFootnoteCall"
|
|
30
|
+
];
|
|
18
31
|
const escapeTag = (name) => name.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
19
32
|
const createTagPatterns = (tags) => {
|
|
20
33
|
const names = [...tags].filter(([, kind]) => kind === "block").map(([name]) => escapeTag(name));
|
|
@@ -27,11 +40,7 @@ const scanLine = (state, line, tags) => {
|
|
|
27
40
|
state.previousNonBlank = line;
|
|
28
41
|
if (state.fence) {
|
|
29
42
|
const close = FENCE.exec(line);
|
|
30
|
-
if (close?.[1][0] === state.fence.char && close[1].length >= state.fence.length && line.
|
|
31
|
-
return;
|
|
32
|
-
}
|
|
33
|
-
if (state.math) {
|
|
34
|
-
if (MATH.test(line)) state.math = false;
|
|
43
|
+
if (close?.[1][0] === state.fence.char && close[1].length >= state.fence.length && BLANK.test(line.slice(close[0].length))) state.fence = null;
|
|
35
44
|
return;
|
|
36
45
|
}
|
|
37
46
|
if (state.rawEnd) {
|
|
@@ -40,21 +49,16 @@ const scanLine = (state, line, tags) => {
|
|
|
40
49
|
}
|
|
41
50
|
if (LIST.test(line)) state.openList = true;
|
|
42
51
|
const fence = FENCE.exec(line);
|
|
43
|
-
if (fence) {
|
|
52
|
+
if (fence && (fence[1].startsWith("~") || !line.slice(fence[0].length).includes(fence[1][0]))) {
|
|
44
53
|
state.fence = {
|
|
45
54
|
char: fence[1][0],
|
|
46
55
|
length: fence[1].length
|
|
47
56
|
};
|
|
48
57
|
return;
|
|
49
58
|
}
|
|
50
|
-
|
|
51
|
-
state.math = true;
|
|
52
|
-
return;
|
|
53
|
-
}
|
|
54
|
-
const raw = RAW_HTML.exec(line);
|
|
59
|
+
const raw = RAW_HTML.find(([start]) => start.test(line));
|
|
55
60
|
if (raw) {
|
|
56
|
-
|
|
57
|
-
if (!end.test(line.slice(raw.index + raw[0].length))) state.rawEnd = end;
|
|
61
|
+
if (!raw[1].test(line)) state.rawEnd = raw[1];
|
|
58
62
|
return;
|
|
59
63
|
}
|
|
60
64
|
if (!tags) return;
|
|
@@ -66,10 +70,11 @@ const scanLine = (state, line, tags) => {
|
|
|
66
70
|
* how either side parses: a blank line followed by a new block, with no open
|
|
67
71
|
* fence, math block or raw HTML block, no open list and no indented line before
|
|
68
72
|
* it that the next block could continue. The next block must start unindented
|
|
69
|
-
* and not with a list marker
|
|
70
|
-
*
|
|
71
|
-
*
|
|
72
|
-
*
|
|
73
|
+
* and not with a list marker or a last line of digits that can still become
|
|
74
|
+
* one, since either can continue an earlier list item or list. A list stays
|
|
75
|
+
* open until such a block starts after a blank line, because a streamed chunk
|
|
76
|
+
* can end inside the next item's marker. `from` must be such a boundary itself
|
|
77
|
+
* (or 0).
|
|
73
78
|
*
|
|
74
79
|
* The count of registered block tags only skips offsets inside a balanced tag.
|
|
75
80
|
* It reads no pairing, so it can reach 0 while a tag is open, and the caller
|
|
@@ -80,29 +85,41 @@ const findMarkdownSegmentBoundaries = (source, from, tags) => {
|
|
|
80
85
|
const state = {
|
|
81
86
|
fence: null,
|
|
82
87
|
openList: false,
|
|
83
|
-
math: false,
|
|
84
88
|
previousNonBlank: null,
|
|
85
89
|
rawEnd: null,
|
|
86
90
|
tagDepth: 0
|
|
87
91
|
};
|
|
88
92
|
const boundaries = [];
|
|
93
|
+
const textEnd = markdownTextEnd(source);
|
|
89
94
|
let pendingBlank = false;
|
|
90
95
|
let offset = from;
|
|
91
96
|
while (offset < source.length) {
|
|
92
|
-
|
|
93
|
-
const
|
|
94
|
-
const line = source.slice(offset,
|
|
95
|
-
const blank =
|
|
96
|
-
if (!blank && pendingBlank && !LEADING_WHITESPACE.test(line) && !LIST.test(line) && state.previousNonBlank !== null && !state.fence && !state.
|
|
97
|
-
if (!state.openList && !INDENTED.test(state.previousNonBlank)) boundaries.push(offset);
|
|
97
|
+
LINE_END.lastIndex = offset;
|
|
98
|
+
const lineEnd = LINE_END.exec(source);
|
|
99
|
+
const line = source.slice(offset, lineEnd?.index ?? source.length);
|
|
100
|
+
const blank = BLANK.test(line);
|
|
101
|
+
if (!blank && pendingBlank && !LEADING_WHITESPACE.test(line) && !LIST.test(line) && !(lineEnd === null && LIST_START.test(line)) && state.previousNonBlank !== null && !state.fence && !state.rawEnd && state.tagDepth === 0) {
|
|
102
|
+
if (!state.openList && !INDENTED.test(state.previousNonBlank) && !line.startsWith(BYTE_ORDER_MARK) && offset < textEnd) boundaries.push(offset);
|
|
98
103
|
state.openList = false;
|
|
99
104
|
}
|
|
100
105
|
pendingBlank = blank;
|
|
101
106
|
if (!blank) scanLine(state, line, tagPatterns);
|
|
102
|
-
offset =
|
|
107
|
+
offset = lineEnd ? LINE_END.lastIndex : source.length;
|
|
103
108
|
}
|
|
104
109
|
return boundaries;
|
|
105
110
|
};
|
|
111
|
+
/**
|
|
112
|
+
* Whether the last root node of a segment's own parse can take what the stream
|
|
113
|
+
* adds after the segment: a block that runs past the segment's last line of
|
|
114
|
+
* text, such as an unclosed fence, comment or raw tag.
|
|
115
|
+
*/
|
|
116
|
+
const holdsMarkdownSegmentOpen = (segment, last) => {
|
|
117
|
+
if (!last?.position) return true;
|
|
118
|
+
const lines = segment.split(LINE_END);
|
|
119
|
+
let text = lines.length;
|
|
120
|
+
while (text > 0 && BLANK.test(lines[text - 1])) text -= 1;
|
|
121
|
+
return last.position.end.line > text;
|
|
122
|
+
};
|
|
106
123
|
const shiftPoint = (point, lines, offset) => point ? {
|
|
107
124
|
...point,
|
|
108
125
|
line: point.line + lines,
|
|
@@ -121,14 +138,6 @@ const shiftMarkdownDiagnostic = (diagnostic, lines, offset) => {
|
|
|
121
138
|
}
|
|
122
139
|
};
|
|
123
140
|
};
|
|
124
|
-
|
|
125
|
-
const countMarkdownLines = (source, offset) => {
|
|
126
|
-
let lines = 0;
|
|
127
|
-
for (let index = source.indexOf("\n"); index !== -1 && index < offset;) {
|
|
128
|
-
lines += 1;
|
|
129
|
-
index = source.indexOf("\n", index + 1);
|
|
130
|
-
}
|
|
131
|
-
return lines;
|
|
132
|
-
};
|
|
141
|
+
const countMarkdownLines = (source, offset) => source.slice(0, offset).match(LINE_END)?.length ?? 0;
|
|
133
142
|
//#endregion
|
|
134
|
-
export { MARKDOWN_DEFINITION, countMarkdownLines, findMarkdownSegmentBoundaries, shiftMarkdownDiagnostic };
|
|
143
|
+
export { DEFINITION_SYNTAX, MARKDOWN_DEFINITION, countMarkdownLines, findMarkdownSegmentBoundaries, holdsMarkdownSegmentOpen, readsMarkdownWhole, shiftMarkdownDiagnostic };
|
|
@@ -212,6 +212,9 @@ const findHtmlFlow = (context) => {
|
|
|
212
212
|
if (!htmlFlow) throw new Error("Markdown tag grammar requires the CommonMark htmlFlow construct.");
|
|
213
213
|
return htmlFlow;
|
|
214
214
|
};
|
|
215
|
+
const tagSyntaxes = /* @__PURE__ */ new WeakSet();
|
|
216
|
+
/** Whether a micromark extension is the tag syntax `remarkMarkdownTags` adds. */
|
|
217
|
+
const isMarkdownTagSyntax = (extension) => tagSyntaxes.has(extension);
|
|
215
218
|
const createSyntax = (tags) => {
|
|
216
219
|
const isRegistered = (name) => tags.has(name);
|
|
217
220
|
const isBlock = (name) => tags.get(name) === "block";
|
|
@@ -351,6 +354,7 @@ const createFromMarkdown = () => {
|
|
|
351
354
|
}
|
|
352
355
|
for (const inner of stack.splice(index + 1).reverse()) closeUnclosed(inner);
|
|
353
356
|
const element = stack.pop();
|
|
357
|
+
if (element?.data?.markdownTag) element.data.markdownTag.close = position;
|
|
354
358
|
if (element?.position) element.position = {
|
|
355
359
|
end: { ...position.end },
|
|
356
360
|
start: element.position.start
|
|
@@ -468,10 +472,16 @@ const rereadLegacyIndentation = (tree, source, index, parse) => {
|
|
|
468
472
|
start: remapPoint(node.position.start)
|
|
469
473
|
};
|
|
470
474
|
const tagData = node.data?.markdownTag;
|
|
471
|
-
if (tagData)
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
+
if (tagData) {
|
|
476
|
+
tagData.open = {
|
|
477
|
+
end: remapPoint(tagData.open.end),
|
|
478
|
+
start: remapPoint(tagData.open.start)
|
|
479
|
+
};
|
|
480
|
+
if (tagData.close) tagData.close = {
|
|
481
|
+
end: remapPoint(tagData.close.end),
|
|
482
|
+
start: remapPoint(tagData.close.start)
|
|
483
|
+
};
|
|
484
|
+
}
|
|
475
485
|
node.children?.forEach(remap);
|
|
476
486
|
};
|
|
477
487
|
const children = fragment.children ?? [];
|
|
@@ -587,6 +597,7 @@ const normalizeBlocks = (tree, tags, index) => {
|
|
|
587
597
|
markdownTagClose: true,
|
|
588
598
|
markdownTagRepair: "misplaced"
|
|
589
599
|
},
|
|
600
|
+
position: tag.close,
|
|
590
601
|
type: "html",
|
|
591
602
|
value: `</${child.name}>`
|
|
592
603
|
}];
|
|
@@ -605,8 +616,9 @@ const normalizeBlocks = (tree, tags, index) => {
|
|
|
605
616
|
};
|
|
606
617
|
visit(tree);
|
|
607
618
|
};
|
|
619
|
+
const markdownTextEnd = (source) => source.trimEnd().length;
|
|
608
620
|
const markUnclosedAtEnd = (tree, source) => {
|
|
609
|
-
const end = source
|
|
621
|
+
const end = markdownTextEnd(source);
|
|
610
622
|
const visit = (node) => {
|
|
611
623
|
const tag = node.data?.markdownTag;
|
|
612
624
|
if (tag?.repair === "unclosed" && (node.position?.end.offset ?? 0) >= end) tag.atEnd = true;
|
|
@@ -621,6 +633,12 @@ const serializeAttributes = (attributes) => attributes.map((attribute) => {
|
|
|
621
633
|
const text = typeof value === "object" ? value.value : value;
|
|
622
634
|
return ` ${name}="${escapeMarkdownTagAttribute(text)}"`;
|
|
623
635
|
}).join("");
|
|
636
|
+
const countTagOpeners = (text) => text.match(/<[!/?A-Za-z]/g)?.length ?? 0;
|
|
637
|
+
const countMarkupTagOpeners = (nodes) => nodes.reduce((count, node) => {
|
|
638
|
+
if (node.type === "html") return count + countTagOpeners(node.value);
|
|
639
|
+
const selfClosing = "children" in node && node.children.length === 0;
|
|
640
|
+
return count + (node.type === "mdxJsxTextElement" ? (selfClosing ? 1 : 2) + countTagOpeners(serializeAttributes(node.attributes)) : 0) + ("children" in node ? countMarkupTagOpeners(node.children) : 0);
|
|
641
|
+
}, 0);
|
|
624
642
|
const toMarkdown = { handlers: {
|
|
625
643
|
mdxJsxFlowElement(node, _parent, state, info) {
|
|
626
644
|
const open = `<${node.name}${serializeAttributes(node.attributes)}`;
|
|
@@ -633,7 +651,7 @@ const toMarkdown = { handlers: {
|
|
|
633
651
|
after: "<",
|
|
634
652
|
before: ">"
|
|
635
653
|
});
|
|
636
|
-
if (!line.includes("\n")) {
|
|
654
|
+
if (!line.includes("\n") && countTagOpeners(line) === countMarkupTagOpeners(only.children)) {
|
|
637
655
|
exit();
|
|
638
656
|
return `${open}>${line}</${node.name}>`;
|
|
639
657
|
}
|
|
@@ -663,7 +681,9 @@ const toMarkdown = { handlers: {
|
|
|
663
681
|
*/
|
|
664
682
|
const remarkMarkdownTags = function({ tags }) {
|
|
665
683
|
const data = this.data();
|
|
666
|
-
|
|
684
|
+
const syntax = createSyntax(tags);
|
|
685
|
+
tagSyntaxes.add(syntax);
|
|
686
|
+
(data.micromarkExtensions ??= []).push(syntax);
|
|
667
687
|
(data.fromMarkdownExtensions ??= []).push(createFromMarkdown());
|
|
668
688
|
(data.toMarkdownExtensions ??= []).push(toMarkdown);
|
|
669
689
|
return (tree, file) => {
|
|
@@ -685,11 +705,11 @@ const remarkMarkdownTagWriter = function() {
|
|
|
685
705
|
/** Streaming preview only: hide a trailing `<…` that may still become a tag. */
|
|
686
706
|
const trimIncompleteMarkdownTag = (source, tags) => {
|
|
687
707
|
const lt = source.lastIndexOf("<");
|
|
688
|
-
if (lt === -1 ||
|
|
708
|
+
if (lt === -1 || /[>\n\r]/.test(source.slice(lt))) return source;
|
|
689
709
|
const match = /^<\/?([A-Za-z][\w-]*)?/.exec(source.slice(lt));
|
|
690
710
|
if (!match) return source;
|
|
691
711
|
const name = match[1] ?? "";
|
|
692
712
|
return (match[0].length === source.length - lt ? [...tags.keys()].some((tag) => tag.startsWith(name)) : tags.has(name)) ? source.slice(0, lt) : source;
|
|
693
713
|
};
|
|
694
714
|
//#endregion
|
|
695
|
-
export { MARKDOWN_TAG_NAME, decodeMarkdownTagAttribute, escapeMarkdownTagAttribute, getMarkdownTagRepair, remarkMarkdownTagWriter, remarkMarkdownTags, trimIncompleteMarkdownTag };
|
|
715
|
+
export { MARKDOWN_TAG_NAME, decodeMarkdownTagAttribute, escapeMarkdownTagAttribute, getMarkdownTagRepair, isMarkdownTagSyntax, markdownTextEnd, remarkMarkdownTagWriter, remarkMarkdownTags, trimIncompleteMarkdownTag };
|