@temelj/mdx 0.14.0 → 0.15.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.
- package/README.md +468 -82
- package/dist/character-entities.d.mts +4 -0
- package/dist/character-entities.d.mts.map +1 -0
- package/dist/character-entities.mjs +2132 -0
- package/dist/character-entities.mjs.map +1 -0
- package/dist/character-reference.d.mts +6 -0
- package/dist/character-reference.d.mts.map +1 -0
- package/dist/character-reference.mjs +18 -0
- package/dist/character-reference.mjs.map +1 -0
- package/dist/compile-analysis.d.mts +28 -0
- package/dist/compile-analysis.d.mts.map +1 -0
- package/dist/compile-analysis.mjs +74 -0
- package/dist/compile-analysis.mjs.map +1 -0
- package/dist/compile-emit.d.mts +9 -0
- package/dist/compile-emit.d.mts.map +1 -0
- package/dist/compile-emit.mjs +187 -0
- package/dist/compile-emit.mjs.map +1 -0
- package/dist/compile-esm.d.mts +34 -0
- package/dist/compile-esm.d.mts.map +1 -0
- package/dist/compile-esm.mjs +187 -0
- package/dist/compile-esm.mjs.map +1 -0
- package/dist/compile-names.d.mts +13 -0
- package/dist/compile-names.d.mts.map +1 -0
- package/dist/compile-names.mjs +32 -0
- package/dist/compile-names.mjs.map +1 -0
- package/dist/compiler.d.mts +69 -85
- package/dist/compiler.d.mts.map +1 -1
- package/dist/compiler.mjs +185 -418
- package/dist/compiler.mjs.map +1 -1
- package/dist/diagnostics.d.mts +37 -0
- package/dist/diagnostics.d.mts.map +1 -0
- package/dist/diagnostics.mjs +57 -0
- package/dist/diagnostics.mjs.map +1 -0
- package/dist/errors.d.mts +157 -0
- package/dist/errors.d.mts.map +1 -0
- package/dist/errors.mjs +263 -0
- package/dist/errors.mjs.map +1 -0
- package/dist/footnote-numbering.d.mts +27 -0
- package/dist/footnote-numbering.d.mts.map +1 -0
- package/dist/footnote-numbering.mjs +49 -0
- package/dist/footnote-numbering.mjs.map +1 -0
- package/dist/format-context.d.mts +46 -0
- package/dist/format-context.d.mts.map +1 -0
- package/dist/format-context.mjs +26 -0
- package/dist/format-context.mjs.map +1 -0
- package/dist/format-directive.d.mts +9 -0
- package/dist/format-directive.d.mts.map +1 -0
- package/dist/format-directive.mjs +99 -0
- package/dist/format-directive.mjs.map +1 -0
- package/dist/format-jsx.d.mts +8 -0
- package/dist/format-jsx.d.mts.map +1 -0
- package/dist/format-jsx.mjs +47 -0
- package/dist/format-jsx.mjs.map +1 -0
- package/dist/format-math.d.mts +9 -0
- package/dist/format-math.d.mts.map +1 -0
- package/dist/format-math.mjs +79 -0
- package/dist/format-math.mjs.map +1 -0
- package/dist/format-safe.d.mts +25 -0
- package/dist/format-safe.d.mts.map +1 -0
- package/dist/format-safe.mjs +366 -0
- package/dist/format-safe.mjs.map +1 -0
- package/dist/format-table.d.mts +8 -0
- package/dist/format-table.d.mts.map +1 -0
- package/dist/format-table.mjs +55 -0
- package/dist/format-table.mjs.map +1 -0
- package/dist/format-track.d.mts +18 -0
- package/dist/format-track.d.mts.map +1 -0
- package/dist/format-track.mjs +34 -0
- package/dist/format-track.mjs.map +1 -0
- package/dist/format.d.mts +7 -0
- package/dist/format.d.mts.map +1 -0
- package/dist/format.mjs +549 -0
- package/dist/format.mjs.map +1 -0
- package/dist/frontmatter.d.mts +15 -0
- package/dist/frontmatter.d.mts.map +1 -0
- package/dist/frontmatter.mjs +187 -0
- package/dist/frontmatter.mjs.map +1 -0
- package/dist/heading.d.mts +6 -0
- package/dist/heading.d.mts.map +1 -0
- package/dist/heading.mjs +19 -0
- package/dist/heading.mjs.map +1 -0
- package/dist/html-format.d.mts +32 -0
- package/dist/html-format.d.mts.map +1 -0
- package/dist/html-format.mjs +127 -0
- package/dist/html-format.mjs.map +1 -0
- package/dist/html-lower.d.mts +78 -0
- package/dist/html-lower.d.mts.map +1 -0
- package/dist/html-lower.mjs +583 -0
- package/dist/html-lower.mjs.map +1 -0
- package/dist/html-stream.d.mts +13 -0
- package/dist/html-stream.d.mts.map +1 -0
- package/dist/html-stream.mjs +32 -0
- package/dist/html-stream.mjs.map +1 -0
- package/dist/html.d.mts +120 -0
- package/dist/html.d.mts.map +1 -0
- package/dist/html.mjs +77 -0
- package/dist/html.mjs.map +1 -0
- package/dist/identifier.d.mts +4 -0
- package/dist/identifier.d.mts.map +1 -0
- package/dist/identifier.mjs +8 -0
- package/dist/identifier.mjs.map +1 -0
- package/dist/javascript-ast.d.mts +16 -0
- package/dist/javascript-ast.d.mts.map +1 -0
- package/dist/javascript-ast.mjs +39 -0
- package/dist/javascript-ast.mjs.map +1 -0
- package/dist/javascript-jsx.d.mts +24 -0
- package/dist/javascript-jsx.d.mts.map +1 -0
- package/dist/javascript-jsx.mjs +189 -0
- package/dist/javascript-jsx.mjs.map +1 -0
- package/dist/javascript.d.mts +9 -0
- package/dist/javascript.d.mts.map +1 -0
- package/dist/javascript.mjs +31 -0
- package/dist/javascript.mjs.map +1 -0
- package/dist/jsx-reference.d.mts +15 -0
- package/dist/jsx-reference.d.mts.map +1 -0
- package/dist/jsx-reference.mjs +31 -0
- package/dist/jsx-reference.mjs.map +1 -0
- package/dist/mod.d.mts +25 -9
- package/dist/mod.mjs +19 -8
- package/dist/model.d.mts +196 -0
- package/dist/model.d.mts.map +1 -0
- package/dist/model.mjs +113 -0
- package/dist/model.mjs.map +1 -0
- package/dist/parser/block-container-recognizers.d.mts +40 -0
- package/dist/parser/block-container-recognizers.d.mts.map +1 -0
- package/dist/parser/block-container-recognizers.mjs +90 -0
- package/dist/parser/block-container-recognizers.mjs.map +1 -0
- package/dist/parser/block-directive-recognizers.d.mts +22 -0
- package/dist/parser/block-directive-recognizers.d.mts.map +1 -0
- package/dist/parser/block-directive-recognizers.mjs +77 -0
- package/dist/parser/block-directive-recognizers.mjs.map +1 -0
- package/dist/parser/block-leaf-recognizers.d.mts +53 -0
- package/dist/parser/block-leaf-recognizers.d.mts.map +1 -0
- package/dist/parser/block-leaf-recognizers.mjs +386 -0
- package/dist/parser/block-leaf-recognizers.mjs.map +1 -0
- package/dist/parser/block-parser.d.mts +38 -0
- package/dist/parser/block-parser.d.mts.map +1 -0
- package/dist/parser/block-parser.mjs +895 -0
- package/dist/parser/block-parser.mjs.map +1 -0
- package/dist/parser/block-syntax.d.mts +240 -0
- package/dist/parser/block-syntax.d.mts.map +1 -0
- package/dist/parser/characters.d.mts +19 -0
- package/dist/parser/characters.d.mts.map +1 -0
- package/dist/parser/characters.mjs +52 -0
- package/dist/parser/characters.mjs.map +1 -0
- package/dist/parser/config.d.mts +16 -0
- package/dist/parser/config.d.mts.map +1 -0
- package/dist/parser/config.mjs +25 -0
- package/dist/parser/config.mjs.map +1 -0
- package/dist/parser/directive-attributes.d.mts +9 -0
- package/dist/parser/directive-attributes.d.mts.map +1 -0
- package/dist/parser/directive-attributes.mjs +24 -0
- package/dist/parser/directive-attributes.mjs.map +1 -0
- package/dist/parser/embedded-scanner.d.mts +18 -0
- package/dist/parser/embedded-scanner.d.mts.map +1 -0
- package/dist/parser/embedded-scanner.mjs +199 -0
- package/dist/parser/embedded-scanner.mjs.map +1 -0
- package/dist/parser/inline-autolink.d.mts +7 -0
- package/dist/parser/inline-autolink.d.mts.map +1 -0
- package/dist/parser/inline-autolink.mjs +42 -0
- package/dist/parser/inline-autolink.mjs.map +1 -0
- package/dist/parser/inline-character-reference.d.mts +7 -0
- package/dist/parser/inline-character-reference.d.mts.map +1 -0
- package/dist/parser/inline-character-reference.mjs +22 -0
- package/dist/parser/inline-character-reference.mjs.map +1 -0
- package/dist/parser/inline-code.d.mts +7 -0
- package/dist/parser/inline-code.d.mts.map +1 -0
- package/dist/parser/inline-code.mjs +29 -0
- package/dist/parser/inline-code.mjs.map +1 -0
- package/dist/parser/inline-directive.d.mts +9 -0
- package/dist/parser/inline-directive.d.mts.map +1 -0
- package/dist/parser/inline-directive.mjs +45 -0
- package/dist/parser/inline-directive.mjs.map +1 -0
- package/dist/parser/inline-embedded.d.mts +19 -0
- package/dist/parser/inline-embedded.d.mts.map +1 -0
- package/dist/parser/inline-embedded.mjs +92 -0
- package/dist/parser/inline-embedded.mjs.map +1 -0
- package/dist/parser/inline-emphasis.d.mts +11 -0
- package/dist/parser/inline-emphasis.d.mts.map +1 -0
- package/dist/parser/inline-emphasis.mjs +79 -0
- package/dist/parser/inline-emphasis.mjs.map +1 -0
- package/dist/parser/inline-input.d.mts +46 -0
- package/dist/parser/inline-input.d.mts.map +1 -0
- package/dist/parser/inline-input.mjs +137 -0
- package/dist/parser/inline-input.mjs.map +1 -0
- package/dist/parser/inline-link.d.mts +12 -0
- package/dist/parser/inline-link.d.mts.map +1 -0
- package/dist/parser/inline-link.mjs +187 -0
- package/dist/parser/inline-link.mjs.map +1 -0
- package/dist/parser/inline-math.d.mts +10 -0
- package/dist/parser/inline-math.d.mts.map +1 -0
- package/dist/parser/inline-math.mjs +46 -0
- package/dist/parser/inline-math.mjs.map +1 -0
- package/dist/parser/inline-output.d.mts +34 -0
- package/dist/parser/inline-output.d.mts.map +1 -0
- package/dist/parser/inline-output.mjs +62 -0
- package/dist/parser/inline-output.mjs.map +1 -0
- package/dist/parser/inline-parser.d.mts +14 -0
- package/dist/parser/inline-parser.d.mts.map +1 -0
- package/dist/parser/inline-parser.mjs +328 -0
- package/dist/parser/inline-parser.mjs.map +1 -0
- package/dist/parser/inline-pending.d.mts +23 -0
- package/dist/parser/inline-pending.d.mts.map +1 -0
- package/dist/parser/inline-pending.mjs +100 -0
- package/dist/parser/inline-pending.mjs.map +1 -0
- package/dist/parser/inline-syntax.d.mts +118 -0
- package/dist/parser/inline-syntax.d.mts.map +1 -0
- package/dist/parser/inline-syntax.mjs +1 -0
- package/dist/parser/javascript-boundary.d.mts +16 -0
- package/dist/parser/javascript-boundary.d.mts.map +1 -0
- package/dist/parser/javascript-boundary.mjs +42 -0
- package/dist/parser/javascript-boundary.mjs.map +1 -0
- package/dist/parser/javascript-scanner.d.mts +22 -0
- package/dist/parser/javascript-scanner.d.mts.map +1 -0
- package/dist/parser/javascript-scanner.mjs +122 -0
- package/dist/parser/javascript-scanner.mjs.map +1 -0
- package/dist/parser/materialize-cache.d.mts +34 -0
- package/dist/parser/materialize-cache.d.mts.map +1 -0
- package/dist/parser/materialize-cache.mjs +12 -0
- package/dist/parser/materialize-cache.mjs.map +1 -0
- package/dist/parser/materialize-jsx.d.mts +14 -0
- package/dist/parser/materialize-jsx.d.mts.map +1 -0
- package/dist/parser/materialize-jsx.mjs +76 -0
- package/dist/parser/materialize-jsx.mjs.map +1 -0
- package/dist/parser/materialize.d.mts +61 -0
- package/dist/parser/materialize.d.mts.map +1 -0
- package/dist/parser/materialize.mjs +736 -0
- package/dist/parser/materialize.mjs.map +1 -0
- package/dist/parser/recognizer.d.mts +20 -0
- package/dist/parser/recognizer.d.mts.map +1 -0
- package/dist/parser/recognizer.mjs +24 -0
- package/dist/parser/recognizer.mjs.map +1 -0
- package/dist/parser/session.d.mts +42 -0
- package/dist/parser/session.d.mts.map +1 -0
- package/dist/parser/session.mjs +89 -0
- package/dist/parser/session.mjs.map +1 -0
- package/dist/parser.d.mts +25 -0
- package/dist/parser.d.mts.map +1 -0
- package/dist/parser.mjs +37 -0
- package/dist/parser.mjs.map +1 -0
- package/dist/plugin-internal.d.mts +25 -0
- package/dist/plugin-internal.d.mts.map +1 -0
- package/dist/plugin-internal.mjs +99 -0
- package/dist/plugin-internal.mjs.map +1 -0
- package/dist/plugin.d.mts +37 -0
- package/dist/plugin.d.mts.map +1 -0
- package/dist/plugin.mjs +1 -0
- package/dist/plugins/code-block-metadata.d.mts +42 -0
- package/dist/plugins/code-block-metadata.d.mts.map +1 -0
- package/dist/plugins/code-block-metadata.mjs +133 -0
- package/dist/plugins/code-block-metadata.mjs.map +1 -0
- package/dist/plugins/heading-id.d.mts +15 -0
- package/dist/plugins/heading-id.d.mts.map +1 -0
- package/dist/plugins/heading-id.mjs +45 -0
- package/dist/plugins/heading-id.mjs.map +1 -0
- package/dist/plugins/lower-heading.d.mts +16 -0
- package/dist/plugins/lower-heading.d.mts.map +1 -0
- package/dist/plugins/lower-heading.mjs +28 -0
- package/dist/plugins/lower-heading.mjs.map +1 -0
- package/dist/plugins/math-render.d.mts +26 -0
- package/dist/plugins/math-render.d.mts.map +1 -0
- package/dist/plugins/math-render.mjs +51 -0
- package/dist/plugins/math-render.mjs.map +1 -0
- package/dist/plugins/mod.d.mts +9 -6
- package/dist/plugins/mod.mjs +8 -6
- package/dist/plugins/remove-esm.d.mts +10 -0
- package/dist/plugins/remove-esm.d.mts.map +1 -0
- package/dist/plugins/remove-esm.mjs +12 -0
- package/dist/plugins/remove-esm.mjs.map +1 -0
- package/dist/plugins/shiki.d.mts +21 -0
- package/dist/plugins/shiki.d.mts.map +1 -0
- package/dist/plugins/shiki.mjs +144 -0
- package/dist/plugins/shiki.mjs.map +1 -0
- package/dist/plugins/syntax-highlight.d.mts +38 -0
- package/dist/plugins/syntax-highlight.d.mts.map +1 -0
- package/dist/plugins/syntax-highlight.mjs +106 -0
- package/dist/plugins/syntax-highlight.mjs.map +1 -0
- package/dist/processor.d.mts +60 -0
- package/dist/processor.d.mts.map +1 -0
- package/dist/processor.mjs +106 -0
- package/dist/processor.mjs.map +1 -0
- package/dist/ranges.d.mts +10 -0
- package/dist/ranges.d.mts.map +1 -0
- package/dist/ranges.mjs +22 -0
- package/dist/ranges.mjs.map +1 -0
- package/dist/source-map.d.mts +28 -0
- package/dist/source-map.d.mts.map +1 -0
- package/dist/source-map.mjs +90 -0
- package/dist/source-map.mjs.map +1 -0
- package/dist/source-text.d.mts +35 -0
- package/dist/source-text.d.mts.map +1 -0
- package/dist/source-text.mjs +132 -0
- package/dist/source-text.mjs.map +1 -0
- package/dist/source.d.mts +86 -0
- package/dist/source.d.mts.map +1 -0
- package/dist/source.mjs +264 -0
- package/dist/source.mjs.map +1 -0
- package/dist/stream-events.d.mts +59 -0
- package/dist/stream-events.d.mts.map +1 -0
- package/dist/stream-events.mjs +115 -0
- package/dist/stream-events.mjs.map +1 -0
- package/dist/stream-session.d.mts +79 -0
- package/dist/stream-session.d.mts.map +1 -0
- package/dist/stream-session.mjs +904 -0
- package/dist/stream-session.mjs.map +1 -0
- package/dist/stream-tail.d.mts +84 -0
- package/dist/stream-tail.d.mts.map +1 -0
- package/dist/stream-tail.mjs +359 -0
- package/dist/stream-tail.mjs.map +1 -0
- package/dist/stream-types.d.mts +89 -0
- package/dist/stream-types.d.mts.map +1 -0
- package/dist/stream-types.mjs +1 -0
- package/dist/stream.d.mts +28 -0
- package/dist/stream.d.mts.map +1 -0
- package/dist/stream.mjs +68 -0
- package/dist/stream.mjs.map +1 -0
- package/dist/syntax-options.d.mts +21 -0
- package/dist/syntax-options.d.mts.map +1 -0
- package/dist/syntax-options.mjs +26 -0
- package/dist/syntax-options.mjs.map +1 -0
- package/dist/traversal.d.mts +25 -0
- package/dist/traversal.d.mts.map +1 -0
- package/dist/traversal.mjs +136 -0
- package/dist/traversal.mjs.map +1 -0
- package/dist/tree.d.mts +18 -0
- package/dist/tree.d.mts.map +1 -0
- package/dist/tree.mjs +43 -0
- package/dist/tree.mjs.map +1 -0
- package/dist/unified/adapter.d.mts +24 -0
- package/dist/unified/adapter.d.mts.map +1 -0
- package/dist/unified/adapter.mjs +44 -0
- package/dist/unified/adapter.mjs.map +1 -0
- package/dist/unified/mod.d.mts +2 -0
- package/dist/unified/mod.mjs +1 -0
- package/dist/unified/runtime.d.mts +10 -0
- package/dist/unified/runtime.d.mts.map +1 -0
- package/dist/unified/runtime.mjs +115 -0
- package/dist/unified/runtime.mjs.map +1 -0
- package/dist/unified/tree-bridge.d.mts +63 -0
- package/dist/unified/tree-bridge.d.mts.map +1 -0
- package/dist/unified/tree-bridge.mjs +876 -0
- package/dist/unified/tree-bridge.mjs.map +1 -0
- package/dist/uri.d.mts +5 -0
- package/dist/uri.d.mts.map +1 -0
- package/dist/uri.mjs +32 -0
- package/dist/uri.mjs.map +1 -0
- package/package.json +56 -30
- package/dist/plugins/heading-id/plugin.d.mts +0 -17
- package/dist/plugins/heading-id/plugin.d.mts.map +0 -1
- package/dist/plugins/heading-id/plugin.mjs +0 -23
- package/dist/plugins/heading-id/plugin.mjs.map +0 -1
- package/dist/plugins/katex/plugin.d.mts +0 -39
- package/dist/plugins/katex/plugin.d.mts.map +0 -1
- package/dist/plugins/katex/plugin.mjs +0 -107
- package/dist/plugins/katex/plugin.mjs.map +0 -1
- package/dist/plugins/remove-imports-exports.d.mts +0 -10
- package/dist/plugins/remove-imports-exports.d.mts.map +0 -1
- package/dist/plugins/remove-imports-exports.mjs +0 -15
- package/dist/plugins/remove-imports-exports.mjs.map +0 -1
- package/dist/plugins/syntax-highlight/code-meta.d.mts +0 -22
- package/dist/plugins/syntax-highlight/code-meta.d.mts.map +0 -1
- package/dist/plugins/syntax-highlight/code-meta.mjs +0 -38
- package/dist/plugins/syntax-highlight/code-meta.mjs.map +0 -1
- package/dist/plugins/syntax-highlight/plugin.d.mts +0 -57
- package/dist/plugins/syntax-highlight/plugin.d.mts.map +0 -1
- package/dist/plugins/syntax-highlight/plugin.mjs +0 -93
- package/dist/plugins/syntax-highlight/plugin.mjs.map +0 -1
- package/dist/plugins/tree-processor/plugin.d.mts +0 -19
- package/dist/plugins/tree-processor/plugin.d.mts.map +0 -1
- package/dist/plugins/tree-processor/plugin.mjs +0 -21
- package/dist/plugins/tree-processor/plugin.mjs.map +0 -1
- package/dist/types.d.mts +0 -2
- /package/dist/{types.mjs → parser/block-syntax.mjs} +0 -0
|
@@ -0,0 +1,736 @@
|
|
|
1
|
+
import { DiagnosticBag, createDiagnostic } from "../diagnostics.mjs";
|
|
2
|
+
import { normalizeIdentifier } from "../identifier.mjs";
|
|
3
|
+
import { nodeText } from "../model.mjs";
|
|
4
|
+
import { sourceText } from "../source.mjs";
|
|
5
|
+
import { decodeFrontmatter } from "../frontmatter.mjs";
|
|
6
|
+
import { parseFlowRange } from "./block-parser.mjs";
|
|
7
|
+
import { InlineInput } from "./inline-input.mjs";
|
|
8
|
+
import { parseInline } from "./inline-parser.mjs";
|
|
9
|
+
import { isFlowJsxChildren, jsxParts } from "./materialize-jsx.mjs";
|
|
10
|
+
//#region src/parser/materialize.ts
|
|
11
|
+
function materializeDocument(file, blocks, syntax, limits, options = {}) {
|
|
12
|
+
const indexes = {
|
|
13
|
+
definitions: /* @__PURE__ */ new Set(),
|
|
14
|
+
footnotes: /* @__PURE__ */ new Set()
|
|
15
|
+
};
|
|
16
|
+
const context = createContext(file, syntax, limits, options, indexes);
|
|
17
|
+
if (!context.keepReferences) collectIndexes(blocks, context, indexes);
|
|
18
|
+
const children = [];
|
|
19
|
+
const materializedBlocks = [];
|
|
20
|
+
for (const block of blocks) {
|
|
21
|
+
if (context.limit !== void 0) break;
|
|
22
|
+
const before = context.nodes;
|
|
23
|
+
context.references = /* @__PURE__ */ new Set();
|
|
24
|
+
const child = materializeBlock(block, context);
|
|
25
|
+
if (child !== void 0) {
|
|
26
|
+
children.push(child);
|
|
27
|
+
materializedBlocks.push(Object.freeze({
|
|
28
|
+
node: child,
|
|
29
|
+
nodeCount: context.nodes - before,
|
|
30
|
+
references: [...context.references]
|
|
31
|
+
}));
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
if (context.limit !== void 0) {
|
|
35
|
+
const diagnostic = limitDiagnostic(file, context.limit);
|
|
36
|
+
const diagnostics = Object.freeze([diagnostic]);
|
|
37
|
+
return Object.freeze({
|
|
38
|
+
kind: "limitExceeded",
|
|
39
|
+
limit: context.limit,
|
|
40
|
+
diagnostics
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
const diagnostics = context.diagnostics.snapshot();
|
|
44
|
+
if (context.authoredInvalid || diagnostics.some((diagnostic) => diagnostic.severity === "error")) {
|
|
45
|
+
const errors = diagnostics.filter((diagnostic) => diagnostic.severity === "error");
|
|
46
|
+
if (errors.length === 0) throw new Error("Invalid MDX draft did not produce an error diagnostic");
|
|
47
|
+
const [first, ...rest] = errors;
|
|
48
|
+
if (first === void 0) throw new Error("Invalid MDX diagnostic collection changed unexpectedly");
|
|
49
|
+
const invalidDiagnostics = Object.freeze([first, ...rest]);
|
|
50
|
+
return Object.freeze({
|
|
51
|
+
kind: "invalid",
|
|
52
|
+
diagnostics: invalidDiagnostics
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
const document = counted(context, {
|
|
56
|
+
kind: "document",
|
|
57
|
+
children,
|
|
58
|
+
origin: range(0, file.length)
|
|
59
|
+
});
|
|
60
|
+
if (document === void 0 || context.limit !== void 0) {
|
|
61
|
+
const diagnostic = limitDiagnostic(file, "maximumNodes");
|
|
62
|
+
const diagnostics = Object.freeze([diagnostic]);
|
|
63
|
+
return Object.freeze({
|
|
64
|
+
kind: "limitExceeded",
|
|
65
|
+
limit: "maximumNodes",
|
|
66
|
+
diagnostics
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
return Object.freeze({
|
|
70
|
+
kind: "ok",
|
|
71
|
+
document,
|
|
72
|
+
blocks: Object.freeze(materializedBlocks),
|
|
73
|
+
diagnostics
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Materializes parsed inline syntax that a stream renders outside a block,
|
|
78
|
+
* keeping references.
|
|
79
|
+
*/
|
|
80
|
+
function materializePhrasing(file, nodes, syntax, limits) {
|
|
81
|
+
const context = createContext(file, syntax, limits, {
|
|
82
|
+
keepReferences: true,
|
|
83
|
+
allowIncomplete: true
|
|
84
|
+
});
|
|
85
|
+
const children = [];
|
|
86
|
+
for (const node of nodes) {
|
|
87
|
+
const child = materializeInlineNode(node, context);
|
|
88
|
+
if (child !== void 0) children.push(child);
|
|
89
|
+
}
|
|
90
|
+
return {
|
|
91
|
+
nodes: children,
|
|
92
|
+
nodeCount: context.nodes
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
/** Independent inline groups share one context while retaining their own node budgets. */
|
|
96
|
+
function materializePhrasingGroups(file, nodes, syntax, limits, cache) {
|
|
97
|
+
const context = createContext(file, syntax, limits, {
|
|
98
|
+
keepReferences: true,
|
|
99
|
+
allowIncomplete: true
|
|
100
|
+
});
|
|
101
|
+
return nodes.map((node) => {
|
|
102
|
+
const hit = cache.get(node);
|
|
103
|
+
if (hit !== void 0) return hit;
|
|
104
|
+
context.nodes = 0;
|
|
105
|
+
context.limit = void 0;
|
|
106
|
+
context.usedEndOfInput = false;
|
|
107
|
+
const child = materializeInlineNode(node, context);
|
|
108
|
+
const materialized = {
|
|
109
|
+
nodes: child === void 0 ? [] : [child],
|
|
110
|
+
nodeCount: context.nodes
|
|
111
|
+
};
|
|
112
|
+
if (context.limit === void 0 && !context.usedEndOfInput) cache.set(node, materialized);
|
|
113
|
+
return materialized;
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
/** Only whitespace and at most one line ending follow `offset`. */
|
|
117
|
+
function reachesEndOfInput(file, offset) {
|
|
118
|
+
return /^[ \t]*(?:\r\n?|\n)?[ \t]*$/u.test(file.slice({
|
|
119
|
+
start: offset,
|
|
120
|
+
end: file.length
|
|
121
|
+
}));
|
|
122
|
+
}
|
|
123
|
+
const emptyIndexes = {
|
|
124
|
+
definitions: /* @__PURE__ */ new Set(),
|
|
125
|
+
footnotes: /* @__PURE__ */ new Set()
|
|
126
|
+
};
|
|
127
|
+
function createContext(file, syntax, limits, options, indexes = emptyIndexes) {
|
|
128
|
+
return {
|
|
129
|
+
definitions: indexes.definitions,
|
|
130
|
+
definitionLookup: options.definitionLookup ?? indexes.definitions,
|
|
131
|
+
references: /* @__PURE__ */ new Set(),
|
|
132
|
+
diagnostics: new DiagnosticBag(),
|
|
133
|
+
file,
|
|
134
|
+
footnotes: indexes.footnotes,
|
|
135
|
+
limits,
|
|
136
|
+
syntax,
|
|
137
|
+
cache: options.keepReferences === true ? options.cache : void 0,
|
|
138
|
+
flowJsx: /* @__PURE__ */ new Map(),
|
|
139
|
+
grammarSource: void 0,
|
|
140
|
+
maximumDepth: 0,
|
|
141
|
+
usedEndOfInput: false,
|
|
142
|
+
allowIncomplete: options.allowIncomplete ?? false,
|
|
143
|
+
keepReferences: options.keepReferences ?? false,
|
|
144
|
+
open: options.open,
|
|
145
|
+
depth: 0,
|
|
146
|
+
nodes: 0,
|
|
147
|
+
authoredInvalid: false,
|
|
148
|
+
limit: void 0
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
function materializeBlock(block, context) {
|
|
152
|
+
return cached(context.cache?.blocks, block, context, () => materializeBlockContent(block, context));
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Replays a cached subtree, or materializes and caches it. Subtrees with
|
|
156
|
+
* diagnostics or input-dependent leniency are never cached.
|
|
157
|
+
*/
|
|
158
|
+
function cached(cache, key, context, materialize) {
|
|
159
|
+
if (cache === void 0) return materialize();
|
|
160
|
+
const hit = cache.get(key);
|
|
161
|
+
if (hit !== void 0 && context.depth + hit.depth <= context.limits.maximumNestingDepth && context.nodes + hit.nodeCount <= context.limits.maximumNodes) {
|
|
162
|
+
for (const identifier of hit.references) context.references.add(identifier);
|
|
163
|
+
context.nodes += hit.nodeCount;
|
|
164
|
+
context.maximumDepth = Math.max(context.maximumDepth, context.depth + hit.depth);
|
|
165
|
+
return hit.node;
|
|
166
|
+
}
|
|
167
|
+
const references = context.references;
|
|
168
|
+
context.references = /* @__PURE__ */ new Set();
|
|
169
|
+
const nodes = context.nodes;
|
|
170
|
+
const diagnostics = context.diagnostics.size;
|
|
171
|
+
const outerDepth = context.maximumDepth;
|
|
172
|
+
const outerEndOfInput = context.usedEndOfInput;
|
|
173
|
+
context.maximumDepth = context.depth;
|
|
174
|
+
context.usedEndOfInput = false;
|
|
175
|
+
const node = materialize();
|
|
176
|
+
if (node !== void 0 && context.diagnostics.size === diagnostics && !context.authoredInvalid && context.limit === void 0 && !context.usedEndOfInput) cache.set(key, {
|
|
177
|
+
node,
|
|
178
|
+
nodeCount: context.nodes - nodes,
|
|
179
|
+
depth: context.maximumDepth - context.depth,
|
|
180
|
+
references: [...context.references]
|
|
181
|
+
});
|
|
182
|
+
for (const identifier of context.references) references.add(identifier);
|
|
183
|
+
context.references = references;
|
|
184
|
+
context.maximumDepth = Math.max(outerDepth, context.maximumDepth);
|
|
185
|
+
context.usedEndOfInput ||= outerEndOfInput;
|
|
186
|
+
return node;
|
|
187
|
+
}
|
|
188
|
+
function materializeBlockContent(block, context) {
|
|
189
|
+
if (!enter(context)) return;
|
|
190
|
+
try {
|
|
191
|
+
const origin = range(block.range.start, block.range.end);
|
|
192
|
+
if (isOpenLeaf(block, context)) {
|
|
193
|
+
context.usedEndOfInput = true;
|
|
194
|
+
return counted(context, openLeafPlaceholder(block, origin));
|
|
195
|
+
}
|
|
196
|
+
switch (block.kind) {
|
|
197
|
+
case "paragraph": return counted(context, {
|
|
198
|
+
kind: "paragraph",
|
|
199
|
+
children: materializeInline(block.input, context),
|
|
200
|
+
origin
|
|
201
|
+
});
|
|
202
|
+
case "heading": return counted(context, {
|
|
203
|
+
kind: "heading",
|
|
204
|
+
depth: block.depth,
|
|
205
|
+
children: materializeInline(block.input, context),
|
|
206
|
+
origin
|
|
207
|
+
});
|
|
208
|
+
case "code": return counted(context, {
|
|
209
|
+
kind: "codeBlock",
|
|
210
|
+
value: trimFinalLineEnding(inlineInputText(context.file, block.value)),
|
|
211
|
+
language: sliceOptional(context.file, block.language),
|
|
212
|
+
meta: sliceOptional(context.file, block.meta),
|
|
213
|
+
origin
|
|
214
|
+
});
|
|
215
|
+
case "thematicBreak": return counted(context, {
|
|
216
|
+
kind: "thematicBreak",
|
|
217
|
+
origin
|
|
218
|
+
});
|
|
219
|
+
case "blockQuote": return counted(context, {
|
|
220
|
+
kind: "blockquote",
|
|
221
|
+
children: materializeBlocks(block.children, context),
|
|
222
|
+
origin
|
|
223
|
+
});
|
|
224
|
+
case "list": return counted(context, {
|
|
225
|
+
kind: "list",
|
|
226
|
+
ordered: block.ordered,
|
|
227
|
+
spread: block.spread,
|
|
228
|
+
start: block.start ?? 1,
|
|
229
|
+
children: materializeListItems(block, context),
|
|
230
|
+
origin
|
|
231
|
+
});
|
|
232
|
+
case "definition": return counted(context, {
|
|
233
|
+
kind: "definition",
|
|
234
|
+
identifier: normalizeIdentifier(block.identifier),
|
|
235
|
+
url: context.file.semanticSlice(block.destination),
|
|
236
|
+
title: semanticSliceOptional(context.file, block.title),
|
|
237
|
+
origin
|
|
238
|
+
});
|
|
239
|
+
case "footnoteDefinition": return counted(context, {
|
|
240
|
+
kind: "footnoteDefinition",
|
|
241
|
+
identifier: normalizeIdentifier(block.identifier),
|
|
242
|
+
label: block.identifier,
|
|
243
|
+
children: materializeBlocks(block.children, context),
|
|
244
|
+
origin
|
|
245
|
+
});
|
|
246
|
+
case "rawHtml": return counted(context, {
|
|
247
|
+
kind: "rawHtmlBlock",
|
|
248
|
+
value: context.file.semanticSlice(block.range),
|
|
249
|
+
origin
|
|
250
|
+
});
|
|
251
|
+
case "frontmatter": {
|
|
252
|
+
const rawValue = trimFinalLineEnding(context.file.slice(block.value));
|
|
253
|
+
const valueOrigin = range(block.value.start, block.value.start + rawValue.length);
|
|
254
|
+
const data = decodeFrontmatter({
|
|
255
|
+
diagnostics: context.diagnostics,
|
|
256
|
+
format: block.format,
|
|
257
|
+
source: rawValue,
|
|
258
|
+
span: span(context.file, valueOrigin.start, valueOrigin.end)
|
|
259
|
+
});
|
|
260
|
+
return counted(context, {
|
|
261
|
+
kind: "frontmatter",
|
|
262
|
+
format: block.format,
|
|
263
|
+
value: rawValue.replace(/\r\n?|\n/gu, "\n"),
|
|
264
|
+
data,
|
|
265
|
+
valueOrigin,
|
|
266
|
+
origin
|
|
267
|
+
});
|
|
268
|
+
}
|
|
269
|
+
case "directive": return counted(context, {
|
|
270
|
+
kind: "containerDirective",
|
|
271
|
+
name: block.name,
|
|
272
|
+
attributes: materializeDirectiveAttributes(block.attributes),
|
|
273
|
+
label: materializeDirectiveLabel(block.label, context),
|
|
274
|
+
children: materializeBlocks(block.children, context),
|
|
275
|
+
origin
|
|
276
|
+
});
|
|
277
|
+
case "leafDirective": return counted(context, {
|
|
278
|
+
kind: "leafDirective",
|
|
279
|
+
name: block.name,
|
|
280
|
+
attributes: materializeDirectiveAttributes(block.attributes),
|
|
281
|
+
label: materializeDirectiveLabel(block.label, context),
|
|
282
|
+
origin
|
|
283
|
+
});
|
|
284
|
+
case "math": {
|
|
285
|
+
const value = trimFinalLineEnding(inlineInputText(context.file, block.value));
|
|
286
|
+
return counted(context, block.format === "tex" ? {
|
|
287
|
+
kind: "displayMath",
|
|
288
|
+
value,
|
|
289
|
+
format: "tex",
|
|
290
|
+
origin
|
|
291
|
+
} : {
|
|
292
|
+
kind: "displayMath",
|
|
293
|
+
value,
|
|
294
|
+
format: "dollar",
|
|
295
|
+
meta: sliceOptional(context.file, block.meta),
|
|
296
|
+
origin
|
|
297
|
+
});
|
|
298
|
+
}
|
|
299
|
+
case "table": return counted(context, {
|
|
300
|
+
kind: "table",
|
|
301
|
+
alignments: block.alignments,
|
|
302
|
+
children: block.rows.map((row) => ({
|
|
303
|
+
kind: "tableRow",
|
|
304
|
+
children: row.cells.map((cell) => {
|
|
305
|
+
const cellRange = inlineSegmentsRange(cell, row.range);
|
|
306
|
+
return {
|
|
307
|
+
kind: "tableCell",
|
|
308
|
+
children: materializeInline(cell, context),
|
|
309
|
+
origin: range(cellRange.start, cellRange.end)
|
|
310
|
+
};
|
|
311
|
+
}),
|
|
312
|
+
origin: range(row.range.start, row.range.end)
|
|
313
|
+
})),
|
|
314
|
+
origin
|
|
315
|
+
});
|
|
316
|
+
case "expression": {
|
|
317
|
+
const authored = context.file.slice(block.range);
|
|
318
|
+
return counted(context, {
|
|
319
|
+
kind: "blockExpression",
|
|
320
|
+
code: authored.slice(authored.indexOf("{") + 1, authored.lastIndexOf("}")),
|
|
321
|
+
origin
|
|
322
|
+
});
|
|
323
|
+
}
|
|
324
|
+
case "esm": return counted(context, {
|
|
325
|
+
kind: "esm",
|
|
326
|
+
code: trimFinalLineEnding(context.file.slice(block.range)),
|
|
327
|
+
origin
|
|
328
|
+
});
|
|
329
|
+
case "jsx": return counted(context, materializeFlowJsx(block, context));
|
|
330
|
+
case "invalidMdx":
|
|
331
|
+
if (context.allowIncomplete && block.state === "incomplete" && reachesEndOfInput(context.file, block.range.end)) {
|
|
332
|
+
context.usedEndOfInput = true;
|
|
333
|
+
return counted(context, {
|
|
334
|
+
kind: "rawHtmlBlock",
|
|
335
|
+
value: context.file.slice(block.range),
|
|
336
|
+
origin
|
|
337
|
+
});
|
|
338
|
+
}
|
|
339
|
+
context.authoredInvalid = true;
|
|
340
|
+
context.diagnostics.add({
|
|
341
|
+
code: `mdx.${block.syntax}.invalid`,
|
|
342
|
+
message: `Invalid or unfinished MDX ${block.syntax}`,
|
|
343
|
+
severity: "error",
|
|
344
|
+
span: span(context.file, origin.start, origin.end)
|
|
345
|
+
});
|
|
346
|
+
return;
|
|
347
|
+
}
|
|
348
|
+
} finally {
|
|
349
|
+
context.depth--;
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
function isOpenLeaf(block, context) {
|
|
353
|
+
return context.open !== void 0 && block.range.start === context.open.start && block.range.end === context.open.end && (block.kind === "paragraph" || block.kind === "code" || block.kind === "math" || block.kind === "invalidMdx");
|
|
354
|
+
}
|
|
355
|
+
function openLeafPlaceholder(block, origin) {
|
|
356
|
+
switch (block.kind) {
|
|
357
|
+
case "paragraph": return {
|
|
358
|
+
kind: "paragraph",
|
|
359
|
+
children: [],
|
|
360
|
+
origin
|
|
361
|
+
};
|
|
362
|
+
case "code": return {
|
|
363
|
+
kind: "codeBlock",
|
|
364
|
+
value: "",
|
|
365
|
+
origin
|
|
366
|
+
};
|
|
367
|
+
case "math": return {
|
|
368
|
+
kind: "displayMath",
|
|
369
|
+
value: "",
|
|
370
|
+
format: "dollar",
|
|
371
|
+
origin
|
|
372
|
+
};
|
|
373
|
+
default: return {
|
|
374
|
+
kind: "rawHtmlBlock",
|
|
375
|
+
value: "",
|
|
376
|
+
origin
|
|
377
|
+
};
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
/**
|
|
381
|
+
* Materializes list items. A growing list reuses its unchanged leading items
|
|
382
|
+
* as one prefix, so an append materializes only the items after it. The list
|
|
383
|
+
* that contains a stream's open leaf is rebuilt on every append and never
|
|
384
|
+
* cached, so its children array is extended in place instead of copied.
|
|
385
|
+
*/
|
|
386
|
+
function materializeListItems(list, context) {
|
|
387
|
+
const items = list.items;
|
|
388
|
+
const closing = list.closing ?? [];
|
|
389
|
+
const count = items.length + closing.length;
|
|
390
|
+
const itemAt = (index) => items[index] ?? closing[index - items.length];
|
|
391
|
+
const open = context.open !== void 0 && containsRange(list.range, context.open);
|
|
392
|
+
const cache = context.cache;
|
|
393
|
+
const prefix = cache === void 0 ? void 0 : listPrefix(count, itemAt, cache.lists);
|
|
394
|
+
const reused = prefix !== void 0 && context.depth + prefix.depth <= context.limits.maximumNestingDepth && context.nodes + prefix.nodeCount <= context.limits.maximumNodes ? prefix : void 0;
|
|
395
|
+
let children = [];
|
|
396
|
+
if (reused !== void 0 && reused.live && open) {
|
|
397
|
+
children = reused.nodes;
|
|
398
|
+
children.length = reused.length;
|
|
399
|
+
} else if (reused !== void 0) children = reused.nodes.slice(0, reused.length);
|
|
400
|
+
let nodeCount = reused?.nodeCount ?? 0;
|
|
401
|
+
let depth = reused?.depth ?? 0;
|
|
402
|
+
let cacheable = true;
|
|
403
|
+
const references = new Set(reused?.references);
|
|
404
|
+
if (reused !== void 0) {
|
|
405
|
+
for (const identifier of reused.references) context.references.add(identifier);
|
|
406
|
+
context.nodes += reused.nodeCount;
|
|
407
|
+
context.maximumDepth = Math.max(context.maximumDepth, context.depth + reused.depth);
|
|
408
|
+
}
|
|
409
|
+
for (let index = (reused?.index ?? -1) + 1; index < count; index++) {
|
|
410
|
+
const item = itemAt(index);
|
|
411
|
+
const node = cached(cache?.items, item, context, () => ({
|
|
412
|
+
kind: "listItem",
|
|
413
|
+
checked: item.checked,
|
|
414
|
+
spread: item.spread,
|
|
415
|
+
children: materializeBlocks(item.children, context),
|
|
416
|
+
origin: range(item.range.start, item.range.end)
|
|
417
|
+
}));
|
|
418
|
+
if (node !== void 0) children.push(node);
|
|
419
|
+
const entry = cache?.items.get(item);
|
|
420
|
+
cacheable &&= entry?.node === node;
|
|
421
|
+
for (const identifier of entry?.references ?? []) references.add(identifier);
|
|
422
|
+
nodeCount += entry?.nodeCount ?? 0;
|
|
423
|
+
depth = Math.max(depth, entry?.depth ?? 0);
|
|
424
|
+
if (cacheable && index >= count - prefixWindow) cache?.lists.set(item, {
|
|
425
|
+
index,
|
|
426
|
+
nodes: children,
|
|
427
|
+
length: children.length,
|
|
428
|
+
live: open,
|
|
429
|
+
references: [...references],
|
|
430
|
+
nodeCount,
|
|
431
|
+
depth
|
|
432
|
+
});
|
|
433
|
+
}
|
|
434
|
+
return children;
|
|
435
|
+
}
|
|
436
|
+
/**
|
|
437
|
+
* How far back a list prefix can end. The parser closes an item only when the
|
|
438
|
+
* next line completes, so the last two items are rebuilt by each tail draft.
|
|
439
|
+
*/
|
|
440
|
+
const prefixWindow = 8;
|
|
441
|
+
/** The cached prefix ending closest to the end of the list. Closing items can still change. */
|
|
442
|
+
function listPrefix(count, itemAt, lists) {
|
|
443
|
+
for (let index = count - 1; index >= Math.max(0, count - prefixWindow); index--) {
|
|
444
|
+
const prefix = lists.get(itemAt(index));
|
|
445
|
+
if (prefix?.index === index) return prefix;
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
function containsRange(outer, inner) {
|
|
449
|
+
return outer.start <= inner.start && inner.end <= outer.end;
|
|
450
|
+
}
|
|
451
|
+
function materializeBlocks(blocks, context) {
|
|
452
|
+
const children = [];
|
|
453
|
+
for (const block of blocks) {
|
|
454
|
+
const child = materializeBlock(block, context);
|
|
455
|
+
if (child !== void 0) children.push(child);
|
|
456
|
+
}
|
|
457
|
+
return children;
|
|
458
|
+
}
|
|
459
|
+
function materializeInline(segments, context) {
|
|
460
|
+
const result = parseInline(new InlineInput(context.file, segments), context.syntax, context.definitionLookup);
|
|
461
|
+
for (const identifier of result.references) context.references.add(identifier);
|
|
462
|
+
for (const invalid of result.invalid) {
|
|
463
|
+
if (context.allowIncomplete && invalid.open && reachesEndOfInput(context.file, invalid.range.end)) {
|
|
464
|
+
context.usedEndOfInput = true;
|
|
465
|
+
continue;
|
|
466
|
+
}
|
|
467
|
+
context.authoredInvalid = true;
|
|
468
|
+
context.diagnostics.add({
|
|
469
|
+
code: `mdx.${invalid.syntax}.invalid`,
|
|
470
|
+
message: `Invalid or unfinished MDX ${invalid.syntax}`,
|
|
471
|
+
severity: "error",
|
|
472
|
+
span: span(context.file, invalid.range.start, invalid.range.end)
|
|
473
|
+
});
|
|
474
|
+
}
|
|
475
|
+
return materializeInlineNodes(result.nodes, context);
|
|
476
|
+
}
|
|
477
|
+
function materializeInlineNodes(nodes, context) {
|
|
478
|
+
const children = [];
|
|
479
|
+
for (const node of nodes) {
|
|
480
|
+
const child = materializeInlineNode(node, context);
|
|
481
|
+
if (child !== void 0) children.push(child);
|
|
482
|
+
}
|
|
483
|
+
return children;
|
|
484
|
+
}
|
|
485
|
+
function materializeInlineNode(node, context) {
|
|
486
|
+
const origin = range(node.range.start, node.range.end);
|
|
487
|
+
switch (node.kind) {
|
|
488
|
+
case "text": return counted(context, {
|
|
489
|
+
kind: "text",
|
|
490
|
+
value: node.value,
|
|
491
|
+
origin
|
|
492
|
+
});
|
|
493
|
+
case "emphasis":
|
|
494
|
+
case "strong":
|
|
495
|
+
case "delete": return counted(context, {
|
|
496
|
+
kind: node.kind,
|
|
497
|
+
children: materializeInlineNodes(node.children, context),
|
|
498
|
+
origin
|
|
499
|
+
});
|
|
500
|
+
case "code": return counted(context, {
|
|
501
|
+
kind: "inlineCode",
|
|
502
|
+
value: node.value,
|
|
503
|
+
origin
|
|
504
|
+
});
|
|
505
|
+
case "hardBreak": return counted(context, {
|
|
506
|
+
kind: "hardBreak",
|
|
507
|
+
origin
|
|
508
|
+
});
|
|
509
|
+
case "link": return counted(context, {
|
|
510
|
+
kind: "link",
|
|
511
|
+
url: node.destination,
|
|
512
|
+
title: node.title,
|
|
513
|
+
children: materializeInlineNodes(node.children, context),
|
|
514
|
+
origin
|
|
515
|
+
});
|
|
516
|
+
case "image": return counted(context, {
|
|
517
|
+
kind: "image",
|
|
518
|
+
url: node.destination,
|
|
519
|
+
alt: node.alt,
|
|
520
|
+
title: node.title,
|
|
521
|
+
origin
|
|
522
|
+
});
|
|
523
|
+
case "reference": {
|
|
524
|
+
const children = materializeInlineNodes(node.children, context);
|
|
525
|
+
if (!context.keepReferences && !context.definitions.has(node.identifier)) return counted(context, {
|
|
526
|
+
kind: "text",
|
|
527
|
+
value: context.file.slice(node.range),
|
|
528
|
+
origin
|
|
529
|
+
});
|
|
530
|
+
const identifier = normalizeIdentifier(node.identifier);
|
|
531
|
+
return counted(context, node.image ? {
|
|
532
|
+
kind: "imageReference",
|
|
533
|
+
identifier,
|
|
534
|
+
alt: children.map(nodeText).join(""),
|
|
535
|
+
referenceKind: node.referenceKind,
|
|
536
|
+
origin
|
|
537
|
+
} : {
|
|
538
|
+
kind: "linkReference",
|
|
539
|
+
identifier,
|
|
540
|
+
referenceKind: node.referenceKind,
|
|
541
|
+
children,
|
|
542
|
+
origin
|
|
543
|
+
});
|
|
544
|
+
}
|
|
545
|
+
case "rawHtml": return counted(context, {
|
|
546
|
+
kind: "rawHtmlInline",
|
|
547
|
+
value: node.value,
|
|
548
|
+
origin
|
|
549
|
+
});
|
|
550
|
+
case "expression": return counted(context, {
|
|
551
|
+
kind: "inlineExpression",
|
|
552
|
+
code: node.value,
|
|
553
|
+
origin
|
|
554
|
+
});
|
|
555
|
+
case "jsx": return counted(context, materializeTextJsx(node.value, origin, context));
|
|
556
|
+
case "math": return counted(context, {
|
|
557
|
+
kind: "inlineMath",
|
|
558
|
+
value: node.value,
|
|
559
|
+
format: node.format,
|
|
560
|
+
origin
|
|
561
|
+
});
|
|
562
|
+
case "footnoteReference": return context.keepReferences || context.footnotes.has(node.identifier) ? counted(context, {
|
|
563
|
+
kind: "footnoteReference",
|
|
564
|
+
identifier: normalizeIdentifier(node.identifier),
|
|
565
|
+
label: node.identifier,
|
|
566
|
+
origin
|
|
567
|
+
}) : counted(context, {
|
|
568
|
+
kind: "text",
|
|
569
|
+
value: context.file.slice(node.range),
|
|
570
|
+
origin
|
|
571
|
+
});
|
|
572
|
+
case "directive": return counted(context, {
|
|
573
|
+
kind: "textDirective",
|
|
574
|
+
name: node.name,
|
|
575
|
+
attributes: materializeDirectiveAttributes(node.attributes),
|
|
576
|
+
label: {
|
|
577
|
+
kind: "directiveLabel",
|
|
578
|
+
children: [],
|
|
579
|
+
origin
|
|
580
|
+
},
|
|
581
|
+
origin
|
|
582
|
+
});
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
function materializeDirectiveLabel(label, context) {
|
|
586
|
+
if (label === void 0) return;
|
|
587
|
+
const labelRange = inlineSegmentsRange(label, {
|
|
588
|
+
start: 0,
|
|
589
|
+
end: 0
|
|
590
|
+
});
|
|
591
|
+
return {
|
|
592
|
+
kind: "directiveLabel",
|
|
593
|
+
children: materializeInline(label, context),
|
|
594
|
+
origin: range(labelRange.start, labelRange.end)
|
|
595
|
+
};
|
|
596
|
+
}
|
|
597
|
+
function materializeDirectiveAttributes(attributes) {
|
|
598
|
+
return attributes.map((attribute) => ({
|
|
599
|
+
name: attribute.name,
|
|
600
|
+
value: attribute.value
|
|
601
|
+
}));
|
|
602
|
+
}
|
|
603
|
+
/**
|
|
604
|
+
* Children on following lines are Markdown flow content. Like MDX, children on
|
|
605
|
+
* the same line are phrasing: the element is text JSX in a paragraph, which
|
|
606
|
+
* lowering does not wrap in `p`.
|
|
607
|
+
*/
|
|
608
|
+
function materializeFlowJsx(block, context) {
|
|
609
|
+
const { parts, children } = prepareFlowJsx(block, context);
|
|
610
|
+
const origin = range(block.range.start, block.range.end);
|
|
611
|
+
if (parts.children !== void 0 && parts.children.start !== parts.children.end && children === void 0) return {
|
|
612
|
+
kind: "paragraph",
|
|
613
|
+
children: [materializeTextJsx(context.file.slice(block.range), origin, context, parts)],
|
|
614
|
+
origin
|
|
615
|
+
};
|
|
616
|
+
return {
|
|
617
|
+
kind: "jsxFlowElement",
|
|
618
|
+
name: parts.name,
|
|
619
|
+
attributes: parts.attributes,
|
|
620
|
+
children: children === void 0 ? [] : materializeBlocks(children, context),
|
|
621
|
+
origin
|
|
622
|
+
};
|
|
623
|
+
}
|
|
624
|
+
function prepareFlowJsx(block, context) {
|
|
625
|
+
const cached = context.flowJsx.get(block);
|
|
626
|
+
if (cached !== void 0) return cached;
|
|
627
|
+
const parts = jsxParts(context.file.slice(block.range), block.range.start);
|
|
628
|
+
const childRange = parts.children;
|
|
629
|
+
const prepared = {
|
|
630
|
+
parts,
|
|
631
|
+
children: childRange === void 0 || childRange.start === childRange.end || !isFlowJsxChildren(context.file, childRange) ? void 0 : parseFlowRange(grammarSource(context), childRange, context.syntax)
|
|
632
|
+
};
|
|
633
|
+
context.flowJsx.set(block, prepared);
|
|
634
|
+
return prepared;
|
|
635
|
+
}
|
|
636
|
+
function grammarSource(context) {
|
|
637
|
+
context.grammarSource ??= sourceText(context.file).grammar();
|
|
638
|
+
return context.grammarSource;
|
|
639
|
+
}
|
|
640
|
+
function materializeTextJsx(authored, jsxOrigin, context, parsed = jsxParts(authored, jsxOrigin.start)) {
|
|
641
|
+
return {
|
|
642
|
+
kind: "jsxTextElement",
|
|
643
|
+
name: parsed.name,
|
|
644
|
+
attributes: parsed.attributes,
|
|
645
|
+
children: parsed.children === void 0 ? [] : materializeInline([{
|
|
646
|
+
kind: "source",
|
|
647
|
+
range: parsed.children
|
|
648
|
+
}], context),
|
|
649
|
+
origin: jsxOrigin
|
|
650
|
+
};
|
|
651
|
+
}
|
|
652
|
+
function collectIndexes(blocks, context, indexes) {
|
|
653
|
+
const { definitions, footnotes } = indexes;
|
|
654
|
+
let nodes = 1;
|
|
655
|
+
const visit = (block, depth) => {
|
|
656
|
+
if (context.limit !== void 0) return;
|
|
657
|
+
if (depth > context.limits.maximumNestingDepth) {
|
|
658
|
+
context.limit = "maximumNestingDepth";
|
|
659
|
+
return;
|
|
660
|
+
}
|
|
661
|
+
nodes++;
|
|
662
|
+
if (nodes > context.limits.maximumNodes) {
|
|
663
|
+
context.limit = "maximumNodes";
|
|
664
|
+
return;
|
|
665
|
+
}
|
|
666
|
+
if (block.kind === "definition") definitions.add(normalizeIdentifier(block.identifier));
|
|
667
|
+
else if (block.kind === "footnoteDefinition") footnotes.add(normalizeIdentifier(block.identifier));
|
|
668
|
+
if (block.kind === "blockQuote" || block.kind === "footnoteDefinition" || block.kind === "directive") block.children.forEach((child) => visit(child, depth + 1));
|
|
669
|
+
else if (block.kind === "list") for (const item of [...block.items, ...block.closing ?? []]) item.children.forEach((child) => visit(child, depth + 1));
|
|
670
|
+
else if (block.kind === "jsx") prepareFlowJsx(block, context).children?.forEach((child) => visit(child, depth + 1));
|
|
671
|
+
};
|
|
672
|
+
blocks.forEach((block) => visit(block, 1));
|
|
673
|
+
}
|
|
674
|
+
function inlineInputText(file, segments) {
|
|
675
|
+
return new InlineInput(file, segments).toString();
|
|
676
|
+
}
|
|
677
|
+
function inlineSegmentsRange(segments, fallback) {
|
|
678
|
+
const first = segments.find((segment) => segment.kind === "source");
|
|
679
|
+
const last = segments.findLast((segment) => segment.kind === "source");
|
|
680
|
+
return first?.kind === "source" && last?.kind === "source" ? {
|
|
681
|
+
start: first.range.start,
|
|
682
|
+
end: last.range.end
|
|
683
|
+
} : fallback;
|
|
684
|
+
}
|
|
685
|
+
function sliceOptional(file, range) {
|
|
686
|
+
return range === void 0 ? void 0 : file.slice(range);
|
|
687
|
+
}
|
|
688
|
+
function semanticSliceOptional(file, range) {
|
|
689
|
+
return range === void 0 ? void 0 : file.semanticSlice(range);
|
|
690
|
+
}
|
|
691
|
+
function trimFinalLineEnding(value) {
|
|
692
|
+
return value.endsWith("\r\n") ? value.slice(0, -2) : value.endsWith("\n") || value.endsWith("\r") ? value.slice(0, -1) : value;
|
|
693
|
+
}
|
|
694
|
+
function span(file, start, end) {
|
|
695
|
+
return {
|
|
696
|
+
file,
|
|
697
|
+
start,
|
|
698
|
+
end
|
|
699
|
+
};
|
|
700
|
+
}
|
|
701
|
+
function range(start, end) {
|
|
702
|
+
return {
|
|
703
|
+
start,
|
|
704
|
+
end
|
|
705
|
+
};
|
|
706
|
+
}
|
|
707
|
+
function enter(context) {
|
|
708
|
+
context.depth++;
|
|
709
|
+
context.maximumDepth = Math.max(context.maximumDepth, context.depth);
|
|
710
|
+
if (context.depth > context.limits.maximumNestingDepth) {
|
|
711
|
+
context.limit = "maximumNestingDepth";
|
|
712
|
+
context.depth--;
|
|
713
|
+
return false;
|
|
714
|
+
}
|
|
715
|
+
return true;
|
|
716
|
+
}
|
|
717
|
+
function counted(context, node) {
|
|
718
|
+
context.nodes++;
|
|
719
|
+
if (context.nodes > context.limits.maximumNodes) {
|
|
720
|
+
context.limit = "maximumNodes";
|
|
721
|
+
return;
|
|
722
|
+
}
|
|
723
|
+
return node;
|
|
724
|
+
}
|
|
725
|
+
function limitDiagnostic(file, limit) {
|
|
726
|
+
return createDiagnostic({
|
|
727
|
+
code: `mdx.limit.${limit}`,
|
|
728
|
+
message: `Parser limit ${limit} exceeded`,
|
|
729
|
+
severity: "error",
|
|
730
|
+
span: span(file, 0, file.length)
|
|
731
|
+
});
|
|
732
|
+
}
|
|
733
|
+
//#endregion
|
|
734
|
+
export { materializeDocument, materializePhrasing, materializePhrasingGroups };
|
|
735
|
+
|
|
736
|
+
//# sourceMappingURL=materialize.mjs.map
|