@temelj/mdx 0.14.0 → 0.16.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 +470 -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 +167 -420
- 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-decode.d.mts +15 -0
- package/dist/frontmatter-decode.d.mts.map +1 -0
- package/dist/frontmatter-decode.mjs +187 -0
- package/dist/frontmatter-decode.mjs.map +1 -0
- package/dist/frontmatter-schema.d.mts +8 -0
- package/dist/frontmatter-schema.d.mts.map +1 -0
- package/dist/frontmatter-schema.mjs +28 -0
- package/dist/frontmatter-schema.mjs.map +1 -0
- package/dist/frontmatter.d.mts +40 -0
- package/dist/frontmatter.d.mts.map +1 -0
- package/dist/frontmatter.mjs +109 -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 +26 -9
- package/dist/mod.mjs +20 -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 +136 -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,876 @@
|
|
|
1
|
+
import { htmlChildren, htmlFallback, htmlText } from "../html.mjs";
|
|
2
|
+
import { normalizeIdentifier } from "../identifier.mjs";
|
|
3
|
+
import { isFlowContent, isPhrasingContent } from "../model.mjs";
|
|
4
|
+
import { isHeadingDepth } from "../heading.mjs";
|
|
5
|
+
import { isBoolean, isNumber, isPlainObject, isString } from "@temelj/value";
|
|
6
|
+
import { find, html, svg } from "property-information";
|
|
7
|
+
//#region src/unified/tree-bridge.ts
|
|
8
|
+
const unifiedDataKeys = {
|
|
9
|
+
directiveLabel: "temeljDirectiveLabel",
|
|
10
|
+
htmlNode: "temeljHtmlNode",
|
|
11
|
+
mathFormat: "temeljMathFormat"
|
|
12
|
+
};
|
|
13
|
+
var UnifiedBoundaryError = class extends Error {
|
|
14
|
+
constructor(message) {
|
|
15
|
+
super(message);
|
|
16
|
+
this.name = "UnifiedBoundaryError";
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
var UnifiedTreeBridge = class {
|
|
20
|
+
file;
|
|
21
|
+
visiting = /* @__PURE__ */ new WeakSet();
|
|
22
|
+
/** Semantic HTML nodes sent to rehype. HAST data refers to them by index. */
|
|
23
|
+
#owned = [];
|
|
24
|
+
constructor(file) {
|
|
25
|
+
this.file = file;
|
|
26
|
+
}
|
|
27
|
+
toMdast(document) {
|
|
28
|
+
const node = this.mdxToUnist(document);
|
|
29
|
+
if (node.type !== "root") throw new UnifiedBoundaryError("The document bridge did not produce an mdast root");
|
|
30
|
+
return node;
|
|
31
|
+
}
|
|
32
|
+
fromMdast(root) {
|
|
33
|
+
const node = this.unistToMdx(root);
|
|
34
|
+
if (node.kind !== "document") throw new UnifiedBoundaryError(`Remark returned a ${node.kind} node instead of an mdast root`);
|
|
35
|
+
return node;
|
|
36
|
+
}
|
|
37
|
+
toHast(document) {
|
|
38
|
+
const node = this.htmlToUnist(document);
|
|
39
|
+
if (node.type !== "root") throw new UnifiedBoundaryError("The HTML bridge did not produce a HAST root");
|
|
40
|
+
return node;
|
|
41
|
+
}
|
|
42
|
+
fromHast(root) {
|
|
43
|
+
const node = this.unistToHtml(root, false);
|
|
44
|
+
if (node.kind !== "root") throw new UnifiedBoundaryError(`Rehype returned a ${node.kind} node instead of a HAST root`);
|
|
45
|
+
return node;
|
|
46
|
+
}
|
|
47
|
+
mdxToUnist(node) {
|
|
48
|
+
const base = this.positionBase(node.origin);
|
|
49
|
+
const children = () => "children" in node ? node.children.map((child) => this.mdxToUnist(child)) : [];
|
|
50
|
+
switch (node.kind) {
|
|
51
|
+
case "document": return {
|
|
52
|
+
type: "root",
|
|
53
|
+
children: children(),
|
|
54
|
+
...base
|
|
55
|
+
};
|
|
56
|
+
case "text": return {
|
|
57
|
+
type: "text",
|
|
58
|
+
value: node.value,
|
|
59
|
+
...base
|
|
60
|
+
};
|
|
61
|
+
case "emphasis": return {
|
|
62
|
+
type: "emphasis",
|
|
63
|
+
children: children(),
|
|
64
|
+
...base
|
|
65
|
+
};
|
|
66
|
+
case "strong": return {
|
|
67
|
+
type: "strong",
|
|
68
|
+
children: children(),
|
|
69
|
+
...base
|
|
70
|
+
};
|
|
71
|
+
case "delete": return {
|
|
72
|
+
type: "delete",
|
|
73
|
+
children: children(),
|
|
74
|
+
...base
|
|
75
|
+
};
|
|
76
|
+
case "inlineCode": return {
|
|
77
|
+
type: "inlineCode",
|
|
78
|
+
value: node.value,
|
|
79
|
+
...base
|
|
80
|
+
};
|
|
81
|
+
case "link": return {
|
|
82
|
+
type: "link",
|
|
83
|
+
url: node.url,
|
|
84
|
+
title: node.title ?? null,
|
|
85
|
+
children: children(),
|
|
86
|
+
...base
|
|
87
|
+
};
|
|
88
|
+
case "image": return {
|
|
89
|
+
type: "image",
|
|
90
|
+
url: node.url,
|
|
91
|
+
alt: node.alt,
|
|
92
|
+
title: node.title ?? null,
|
|
93
|
+
...base
|
|
94
|
+
};
|
|
95
|
+
case "linkReference": return {
|
|
96
|
+
type: "linkReference",
|
|
97
|
+
identifier: node.identifier,
|
|
98
|
+
referenceType: node.referenceKind,
|
|
99
|
+
children: children(),
|
|
100
|
+
...base
|
|
101
|
+
};
|
|
102
|
+
case "imageReference": return {
|
|
103
|
+
type: "imageReference",
|
|
104
|
+
identifier: node.identifier,
|
|
105
|
+
referenceType: node.referenceKind,
|
|
106
|
+
alt: node.alt,
|
|
107
|
+
...base
|
|
108
|
+
};
|
|
109
|
+
case "hardBreak": return {
|
|
110
|
+
type: "break",
|
|
111
|
+
...base
|
|
112
|
+
};
|
|
113
|
+
case "rawHtmlInline":
|
|
114
|
+
case "rawHtmlBlock": return {
|
|
115
|
+
type: "html",
|
|
116
|
+
value: node.value,
|
|
117
|
+
...base
|
|
118
|
+
};
|
|
119
|
+
case "footnoteReference": return {
|
|
120
|
+
type: "footnoteReference",
|
|
121
|
+
identifier: node.identifier,
|
|
122
|
+
label: node.label,
|
|
123
|
+
...base
|
|
124
|
+
};
|
|
125
|
+
case "definition": return {
|
|
126
|
+
type: "definition",
|
|
127
|
+
identifier: node.identifier,
|
|
128
|
+
url: node.url,
|
|
129
|
+
title: node.title ?? null,
|
|
130
|
+
...base
|
|
131
|
+
};
|
|
132
|
+
case "footnoteDefinition": return {
|
|
133
|
+
type: "footnoteDefinition",
|
|
134
|
+
identifier: node.identifier,
|
|
135
|
+
label: node.label,
|
|
136
|
+
children: children(),
|
|
137
|
+
...base
|
|
138
|
+
};
|
|
139
|
+
case "frontmatter": return {
|
|
140
|
+
type: node.format,
|
|
141
|
+
value: node.value,
|
|
142
|
+
...base
|
|
143
|
+
};
|
|
144
|
+
case "inlineMath": return {
|
|
145
|
+
type: "inlineMath",
|
|
146
|
+
value: node.value,
|
|
147
|
+
data: { [unifiedDataKeys.mathFormat]: node.format },
|
|
148
|
+
...base
|
|
149
|
+
};
|
|
150
|
+
case "displayMath": return {
|
|
151
|
+
type: "math",
|
|
152
|
+
value: node.value,
|
|
153
|
+
meta: node.meta ?? null,
|
|
154
|
+
data: { [unifiedDataKeys.mathFormat]: node.format },
|
|
155
|
+
...base
|
|
156
|
+
};
|
|
157
|
+
case "paragraph": return {
|
|
158
|
+
type: "paragraph",
|
|
159
|
+
children: children(),
|
|
160
|
+
...base
|
|
161
|
+
};
|
|
162
|
+
case "heading": return {
|
|
163
|
+
type: "heading",
|
|
164
|
+
depth: node.depth,
|
|
165
|
+
children: children(),
|
|
166
|
+
...base
|
|
167
|
+
};
|
|
168
|
+
case "codeBlock": return {
|
|
169
|
+
type: "code",
|
|
170
|
+
value: node.value,
|
|
171
|
+
lang: node.language ?? null,
|
|
172
|
+
meta: node.meta ?? null,
|
|
173
|
+
...base
|
|
174
|
+
};
|
|
175
|
+
case "thematicBreak": return {
|
|
176
|
+
type: "thematicBreak",
|
|
177
|
+
...base
|
|
178
|
+
};
|
|
179
|
+
case "blockquote": return {
|
|
180
|
+
type: "blockquote",
|
|
181
|
+
children: children(),
|
|
182
|
+
...base
|
|
183
|
+
};
|
|
184
|
+
case "listItem": return {
|
|
185
|
+
type: "listItem",
|
|
186
|
+
checked: node.checked ?? null,
|
|
187
|
+
spread: node.spread,
|
|
188
|
+
children: children(),
|
|
189
|
+
...base
|
|
190
|
+
};
|
|
191
|
+
case "list": return {
|
|
192
|
+
type: "list",
|
|
193
|
+
ordered: node.ordered,
|
|
194
|
+
start: node.ordered ? node.start : null,
|
|
195
|
+
spread: node.spread,
|
|
196
|
+
children: children(),
|
|
197
|
+
...base
|
|
198
|
+
};
|
|
199
|
+
case "tableCell": return {
|
|
200
|
+
type: "tableCell",
|
|
201
|
+
children: children(),
|
|
202
|
+
...base
|
|
203
|
+
};
|
|
204
|
+
case "tableRow": return {
|
|
205
|
+
type: "tableRow",
|
|
206
|
+
children: children(),
|
|
207
|
+
...base
|
|
208
|
+
};
|
|
209
|
+
case "table": return {
|
|
210
|
+
type: "table",
|
|
211
|
+
align: node.alignments.map((value) => value === "none" ? null : value),
|
|
212
|
+
children: children(),
|
|
213
|
+
...base
|
|
214
|
+
};
|
|
215
|
+
case "directiveLabel": return {
|
|
216
|
+
type: "temeljDirectiveLabel",
|
|
217
|
+
children: children(),
|
|
218
|
+
...base
|
|
219
|
+
};
|
|
220
|
+
case "textDirective": return directiveToUnist("textDirective", node, base, this);
|
|
221
|
+
case "leafDirective": return directiveToUnist("leafDirective", node, base, this);
|
|
222
|
+
case "containerDirective": {
|
|
223
|
+
const value = directiveToUnist("containerDirective", node, base, this);
|
|
224
|
+
value.children = children();
|
|
225
|
+
if (node.label !== void 0) value.data = { [unifiedDataKeys.directiveLabel]: this.mdxToUnist(node.label) };
|
|
226
|
+
return value;
|
|
227
|
+
}
|
|
228
|
+
case "inlineExpression": return {
|
|
229
|
+
type: "mdxTextExpression",
|
|
230
|
+
value: node.code,
|
|
231
|
+
...base
|
|
232
|
+
};
|
|
233
|
+
case "blockExpression": return {
|
|
234
|
+
type: "mdxFlowExpression",
|
|
235
|
+
value: node.code,
|
|
236
|
+
...base
|
|
237
|
+
};
|
|
238
|
+
case "esm": return {
|
|
239
|
+
type: "mdxjsEsm",
|
|
240
|
+
value: node.code,
|
|
241
|
+
...base
|
|
242
|
+
};
|
|
243
|
+
case "jsxTextElement": return jsxElementToUnist("mdxJsxTextElement", node, base, this);
|
|
244
|
+
case "jsxFlowElement": return jsxElementToUnist("mdxJsxFlowElement", node, base, this);
|
|
245
|
+
default: return node;
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
unistToMdx(value, mode = "flow") {
|
|
249
|
+
const node = this.nodeRecord(value);
|
|
250
|
+
const origin = this.origin(node.position);
|
|
251
|
+
const children = (childMode = "flow") => this.nodeChildren(node).map((child) => this.unistToMdx(child, childMode));
|
|
252
|
+
const phrasingChildren = () => phrasing(children("phrasing"), node.type);
|
|
253
|
+
const flowChildren = () => flow(children(), node.type);
|
|
254
|
+
let result;
|
|
255
|
+
switch (node.type) {
|
|
256
|
+
case "root":
|
|
257
|
+
result = {
|
|
258
|
+
kind: "document",
|
|
259
|
+
children: flowChildren(),
|
|
260
|
+
origin
|
|
261
|
+
};
|
|
262
|
+
break;
|
|
263
|
+
case "text":
|
|
264
|
+
result = {
|
|
265
|
+
kind: "text",
|
|
266
|
+
value: stringField(node, "value"),
|
|
267
|
+
origin
|
|
268
|
+
};
|
|
269
|
+
break;
|
|
270
|
+
case "emphasis":
|
|
271
|
+
case "strong":
|
|
272
|
+
case "delete":
|
|
273
|
+
result = {
|
|
274
|
+
kind: node.type,
|
|
275
|
+
children: phrasingChildren(),
|
|
276
|
+
origin
|
|
277
|
+
};
|
|
278
|
+
break;
|
|
279
|
+
case "inlineCode":
|
|
280
|
+
result = {
|
|
281
|
+
kind: "inlineCode",
|
|
282
|
+
value: stringField(node, "value"),
|
|
283
|
+
origin
|
|
284
|
+
};
|
|
285
|
+
break;
|
|
286
|
+
case "link":
|
|
287
|
+
result = {
|
|
288
|
+
kind: "link",
|
|
289
|
+
url: stringField(node, "url"),
|
|
290
|
+
title: nullableString(node.title),
|
|
291
|
+
children: phrasingChildren(),
|
|
292
|
+
origin
|
|
293
|
+
};
|
|
294
|
+
break;
|
|
295
|
+
case "image":
|
|
296
|
+
result = {
|
|
297
|
+
kind: "image",
|
|
298
|
+
url: stringField(node, "url"),
|
|
299
|
+
alt: nullableString(node.alt) ?? "",
|
|
300
|
+
title: nullableString(node.title),
|
|
301
|
+
origin
|
|
302
|
+
};
|
|
303
|
+
break;
|
|
304
|
+
case "linkReference":
|
|
305
|
+
result = {
|
|
306
|
+
kind: "linkReference",
|
|
307
|
+
identifier: normalizeIdentifier(stringField(node, "identifier")),
|
|
308
|
+
referenceKind: referenceKind(node.referenceType),
|
|
309
|
+
children: phrasingChildren(),
|
|
310
|
+
origin
|
|
311
|
+
};
|
|
312
|
+
break;
|
|
313
|
+
case "imageReference":
|
|
314
|
+
result = {
|
|
315
|
+
kind: "imageReference",
|
|
316
|
+
identifier: normalizeIdentifier(stringField(node, "identifier")),
|
|
317
|
+
alt: nullableString(node.alt) ?? "",
|
|
318
|
+
referenceKind: referenceKind(node.referenceType),
|
|
319
|
+
origin
|
|
320
|
+
};
|
|
321
|
+
break;
|
|
322
|
+
case "break":
|
|
323
|
+
result = {
|
|
324
|
+
kind: "hardBreak",
|
|
325
|
+
origin
|
|
326
|
+
};
|
|
327
|
+
break;
|
|
328
|
+
case "html":
|
|
329
|
+
result = {
|
|
330
|
+
kind: mode === "phrasing" ? "rawHtmlInline" : "rawHtmlBlock",
|
|
331
|
+
value: stringField(node, "value"),
|
|
332
|
+
origin
|
|
333
|
+
};
|
|
334
|
+
break;
|
|
335
|
+
case "footnoteReference": {
|
|
336
|
+
const identifier = stringField(node, "identifier");
|
|
337
|
+
result = {
|
|
338
|
+
kind: "footnoteReference",
|
|
339
|
+
identifier: normalizeIdentifier(identifier),
|
|
340
|
+
label: nullableString(node.label) ?? identifier,
|
|
341
|
+
origin
|
|
342
|
+
};
|
|
343
|
+
break;
|
|
344
|
+
}
|
|
345
|
+
case "definition":
|
|
346
|
+
result = {
|
|
347
|
+
kind: "definition",
|
|
348
|
+
identifier: normalizeIdentifier(stringField(node, "identifier")),
|
|
349
|
+
url: stringField(node, "url"),
|
|
350
|
+
title: nullableString(node.title),
|
|
351
|
+
origin
|
|
352
|
+
};
|
|
353
|
+
break;
|
|
354
|
+
case "footnoteDefinition": {
|
|
355
|
+
const identifier = stringField(node, "identifier");
|
|
356
|
+
result = {
|
|
357
|
+
kind: "footnoteDefinition",
|
|
358
|
+
identifier: normalizeIdentifier(identifier),
|
|
359
|
+
label: nullableString(node.label) ?? identifier,
|
|
360
|
+
children: flowChildren(),
|
|
361
|
+
origin
|
|
362
|
+
};
|
|
363
|
+
break;
|
|
364
|
+
}
|
|
365
|
+
case "yaml":
|
|
366
|
+
case "toml":
|
|
367
|
+
result = {
|
|
368
|
+
kind: "frontmatter",
|
|
369
|
+
format: node.type,
|
|
370
|
+
value: stringField(node, "value"),
|
|
371
|
+
origin
|
|
372
|
+
};
|
|
373
|
+
break;
|
|
374
|
+
case "inlineMath":
|
|
375
|
+
result = {
|
|
376
|
+
kind: "inlineMath",
|
|
377
|
+
value: stringField(node, "value"),
|
|
378
|
+
format: mathFormat(node),
|
|
379
|
+
origin
|
|
380
|
+
};
|
|
381
|
+
break;
|
|
382
|
+
case "math": {
|
|
383
|
+
const format = mathFormat(node);
|
|
384
|
+
result = {
|
|
385
|
+
kind: "displayMath",
|
|
386
|
+
value: stringField(node, "value"),
|
|
387
|
+
format,
|
|
388
|
+
meta: format === "dollar" ? nullableString(node.meta) : void 0,
|
|
389
|
+
origin
|
|
390
|
+
};
|
|
391
|
+
break;
|
|
392
|
+
}
|
|
393
|
+
case "paragraph":
|
|
394
|
+
result = {
|
|
395
|
+
kind: "paragraph",
|
|
396
|
+
children: phrasingChildren(),
|
|
397
|
+
origin
|
|
398
|
+
};
|
|
399
|
+
break;
|
|
400
|
+
case "heading":
|
|
401
|
+
result = {
|
|
402
|
+
kind: "heading",
|
|
403
|
+
depth: headingDepth(node.depth),
|
|
404
|
+
children: phrasingChildren(),
|
|
405
|
+
origin
|
|
406
|
+
};
|
|
407
|
+
break;
|
|
408
|
+
case "code":
|
|
409
|
+
result = {
|
|
410
|
+
kind: "codeBlock",
|
|
411
|
+
value: stringField(node, "value"),
|
|
412
|
+
language: nullableString(node.lang),
|
|
413
|
+
meta: nullableString(node.meta),
|
|
414
|
+
origin
|
|
415
|
+
};
|
|
416
|
+
break;
|
|
417
|
+
case "thematicBreak":
|
|
418
|
+
result = {
|
|
419
|
+
kind: "thematicBreak",
|
|
420
|
+
origin
|
|
421
|
+
};
|
|
422
|
+
break;
|
|
423
|
+
case "blockquote":
|
|
424
|
+
result = {
|
|
425
|
+
kind: "blockquote",
|
|
426
|
+
children: flowChildren(),
|
|
427
|
+
origin
|
|
428
|
+
};
|
|
429
|
+
break;
|
|
430
|
+
case "listItem":
|
|
431
|
+
result = {
|
|
432
|
+
kind: "listItem",
|
|
433
|
+
checked: nullableBoolean(node.checked),
|
|
434
|
+
spread: booleanField(node.spread, false),
|
|
435
|
+
children: flowChildren(),
|
|
436
|
+
origin
|
|
437
|
+
};
|
|
438
|
+
break;
|
|
439
|
+
case "list":
|
|
440
|
+
result = {
|
|
441
|
+
kind: "list",
|
|
442
|
+
ordered: booleanField(node.ordered, false),
|
|
443
|
+
start: nullableNumber(node.start) ?? 1,
|
|
444
|
+
spread: booleanField(node.spread, false),
|
|
445
|
+
children: listItems(children(), node.type),
|
|
446
|
+
origin
|
|
447
|
+
};
|
|
448
|
+
break;
|
|
449
|
+
case "tableCell":
|
|
450
|
+
result = {
|
|
451
|
+
kind: "tableCell",
|
|
452
|
+
children: phrasingChildren(),
|
|
453
|
+
origin
|
|
454
|
+
};
|
|
455
|
+
break;
|
|
456
|
+
case "tableRow":
|
|
457
|
+
result = {
|
|
458
|
+
kind: "tableRow",
|
|
459
|
+
children: tableCells(children(), node.type),
|
|
460
|
+
origin
|
|
461
|
+
};
|
|
462
|
+
break;
|
|
463
|
+
case "table":
|
|
464
|
+
result = {
|
|
465
|
+
kind: "table",
|
|
466
|
+
alignments: tableAlignments(node.align),
|
|
467
|
+
children: tableRows(children(), node.type),
|
|
468
|
+
origin
|
|
469
|
+
};
|
|
470
|
+
break;
|
|
471
|
+
case "temeljDirectiveLabel":
|
|
472
|
+
result = {
|
|
473
|
+
kind: "directiveLabel",
|
|
474
|
+
children: phrasingChildren(),
|
|
475
|
+
origin
|
|
476
|
+
};
|
|
477
|
+
break;
|
|
478
|
+
case "textDirective":
|
|
479
|
+
case "leafDirective":
|
|
480
|
+
result = {
|
|
481
|
+
kind: node.type,
|
|
482
|
+
name: stringField(node, "name"),
|
|
483
|
+
attributes: directiveAttributes(node.attributes),
|
|
484
|
+
label: directiveLabel(children("phrasing"), node.type),
|
|
485
|
+
origin
|
|
486
|
+
};
|
|
487
|
+
break;
|
|
488
|
+
case "containerDirective":
|
|
489
|
+
result = {
|
|
490
|
+
kind: "containerDirective",
|
|
491
|
+
name: stringField(node, "name"),
|
|
492
|
+
attributes: directiveAttributes(node.attributes),
|
|
493
|
+
label: directiveDataLabel(node.data, this),
|
|
494
|
+
children: flowChildren(),
|
|
495
|
+
origin
|
|
496
|
+
};
|
|
497
|
+
break;
|
|
498
|
+
case "mdxTextExpression":
|
|
499
|
+
result = {
|
|
500
|
+
kind: "inlineExpression",
|
|
501
|
+
code: stringField(node, "value"),
|
|
502
|
+
origin
|
|
503
|
+
};
|
|
504
|
+
break;
|
|
505
|
+
case "mdxFlowExpression":
|
|
506
|
+
result = {
|
|
507
|
+
kind: "blockExpression",
|
|
508
|
+
code: stringField(node, "value"),
|
|
509
|
+
origin
|
|
510
|
+
};
|
|
511
|
+
break;
|
|
512
|
+
case "mdxjsEsm":
|
|
513
|
+
result = {
|
|
514
|
+
kind: "esm",
|
|
515
|
+
code: stringField(node, "value"),
|
|
516
|
+
origin
|
|
517
|
+
};
|
|
518
|
+
break;
|
|
519
|
+
case "mdxJsxTextElement":
|
|
520
|
+
result = {
|
|
521
|
+
kind: "jsxTextElement",
|
|
522
|
+
name: nullableString(node.name),
|
|
523
|
+
attributes: jsxAttributes(node.attributes, this),
|
|
524
|
+
children: phrasingChildren(),
|
|
525
|
+
origin
|
|
526
|
+
};
|
|
527
|
+
break;
|
|
528
|
+
case "mdxJsxFlowElement":
|
|
529
|
+
result = {
|
|
530
|
+
kind: "jsxFlowElement",
|
|
531
|
+
name: nullableString(node.name),
|
|
532
|
+
attributes: jsxAttributes(node.attributes, this),
|
|
533
|
+
children: flowChildren(),
|
|
534
|
+
origin
|
|
535
|
+
};
|
|
536
|
+
break;
|
|
537
|
+
default: throw new UnifiedBoundaryError(`Unsupported mdast node type: ${node.type}`);
|
|
538
|
+
}
|
|
539
|
+
this.visiting.delete(node);
|
|
540
|
+
return result;
|
|
541
|
+
}
|
|
542
|
+
htmlToUnist(node) {
|
|
543
|
+
const base = this.positionBase(node.origin);
|
|
544
|
+
const children = () => htmlChildren(node).map((child) => this.htmlToUnist(child));
|
|
545
|
+
switch (node.kind) {
|
|
546
|
+
case "root": return {
|
|
547
|
+
type: "root",
|
|
548
|
+
children: children(),
|
|
549
|
+
...base
|
|
550
|
+
};
|
|
551
|
+
case "text": return {
|
|
552
|
+
type: "text",
|
|
553
|
+
value: node.value,
|
|
554
|
+
...base
|
|
555
|
+
};
|
|
556
|
+
case "raw": return {
|
|
557
|
+
type: "raw",
|
|
558
|
+
value: node.value,
|
|
559
|
+
...base
|
|
560
|
+
};
|
|
561
|
+
case "comment": return {
|
|
562
|
+
type: "comment",
|
|
563
|
+
value: node.value,
|
|
564
|
+
...base
|
|
565
|
+
};
|
|
566
|
+
case "doctype": return {
|
|
567
|
+
type: "doctype",
|
|
568
|
+
...base
|
|
569
|
+
};
|
|
570
|
+
case "element": return {
|
|
571
|
+
type: "element",
|
|
572
|
+
tagName: node.tagName,
|
|
573
|
+
properties: propertiesToHast(node.attributes, node.tagName === "svg" ? svg : html),
|
|
574
|
+
children: children(),
|
|
575
|
+
...base
|
|
576
|
+
};
|
|
577
|
+
case "jsx":
|
|
578
|
+
case "expression":
|
|
579
|
+
case "pending":
|
|
580
|
+
case "unresolvedReference": return {
|
|
581
|
+
type: "text",
|
|
582
|
+
value: htmlText(node),
|
|
583
|
+
data: { [unifiedDataKeys.htmlNode]: this.#owned.push(node) - 1 },
|
|
584
|
+
...base
|
|
585
|
+
};
|
|
586
|
+
case "codeBlock":
|
|
587
|
+
case "math":
|
|
588
|
+
case "footnoteReference":
|
|
589
|
+
case "footnoteSection": return {
|
|
590
|
+
...this.htmlToUnist({
|
|
591
|
+
...htmlFallback(node),
|
|
592
|
+
origin: node.origin
|
|
593
|
+
}),
|
|
594
|
+
data: { [unifiedDataKeys.htmlNode]: this.#owned.push(node) - 1 }
|
|
595
|
+
};
|
|
596
|
+
default: return node;
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
unistToHtml(value, inSvg) {
|
|
600
|
+
const node = this.nodeRecord(value);
|
|
601
|
+
const origin = this.origin(node.position);
|
|
602
|
+
let result;
|
|
603
|
+
switch (node.type) {
|
|
604
|
+
case "root":
|
|
605
|
+
result = {
|
|
606
|
+
kind: "root",
|
|
607
|
+
children: this.nodeChildren(node).map((child) => this.unistToHtml(child, inSvg)),
|
|
608
|
+
origin
|
|
609
|
+
};
|
|
610
|
+
break;
|
|
611
|
+
case "text":
|
|
612
|
+
result = {
|
|
613
|
+
kind: "text",
|
|
614
|
+
value: stringField(node, "value"),
|
|
615
|
+
origin
|
|
616
|
+
};
|
|
617
|
+
break;
|
|
618
|
+
case "raw":
|
|
619
|
+
result = {
|
|
620
|
+
kind: "raw",
|
|
621
|
+
value: stringField(node, "value"),
|
|
622
|
+
origin
|
|
623
|
+
};
|
|
624
|
+
break;
|
|
625
|
+
case "comment":
|
|
626
|
+
result = {
|
|
627
|
+
kind: "comment",
|
|
628
|
+
value: stringField(node, "value"),
|
|
629
|
+
origin
|
|
630
|
+
};
|
|
631
|
+
break;
|
|
632
|
+
case "doctype":
|
|
633
|
+
result = {
|
|
634
|
+
kind: "doctype",
|
|
635
|
+
origin
|
|
636
|
+
};
|
|
637
|
+
break;
|
|
638
|
+
case "element": {
|
|
639
|
+
const tagName = stringField(node, "tagName");
|
|
640
|
+
const svgElement = inSvg || tagName === "svg";
|
|
641
|
+
const childSvg = svgElement && tagName !== "foreignObject";
|
|
642
|
+
result = {
|
|
643
|
+
kind: "element",
|
|
644
|
+
tagName,
|
|
645
|
+
attributes: propertiesFromHast(node.properties ?? {}, svgElement ? svg : html),
|
|
646
|
+
children: this.nodeChildren(node).map((child) => this.unistToHtml(child, childSvg)),
|
|
647
|
+
origin
|
|
648
|
+
};
|
|
649
|
+
break;
|
|
650
|
+
}
|
|
651
|
+
default: throw new UnifiedBoundaryError(`Unsupported HAST node type: ${node.type}`);
|
|
652
|
+
}
|
|
653
|
+
this.visiting.delete(node);
|
|
654
|
+
return this.restoredHtmlNode(node.data, result);
|
|
655
|
+
}
|
|
656
|
+
/** Restores a semantic node whose inert fallback came back from rehype unchanged. */
|
|
657
|
+
restoredHtmlNode(data, result) {
|
|
658
|
+
const index = isPlainObject(data) && unifiedDataKeys.htmlNode in data ? data[unifiedDataKeys.htmlNode] : void 0;
|
|
659
|
+
const owned = isNumber(index) ? this.#owned.at(index) : void 0;
|
|
660
|
+
if (owned === void 0) return result;
|
|
661
|
+
return owned.origin?.start === result.origin?.start && owned.origin?.end === result.origin?.end && htmlText(stripSemantic(owned)) === htmlText(result) ? owned : result;
|
|
662
|
+
}
|
|
663
|
+
nodeRecord(value) {
|
|
664
|
+
if (!isUnistNode(value)) throw new UnifiedBoundaryError("Unified returned a malformed node without a string type");
|
|
665
|
+
if (this.visiting.has(value)) throw new UnifiedBoundaryError(`Unified returned a cyclic ${value.type} tree`);
|
|
666
|
+
this.visiting.add(value);
|
|
667
|
+
return value;
|
|
668
|
+
}
|
|
669
|
+
nodeChildren(node) {
|
|
670
|
+
if (!Array.isArray(node.children)) throw new UnifiedBoundaryError(`${node.type} must have a children array`);
|
|
671
|
+
return node.children;
|
|
672
|
+
}
|
|
673
|
+
positionBase(origin) {
|
|
674
|
+
return origin === void 0 ? {} : { position: {
|
|
675
|
+
start: this.file.location(origin.start),
|
|
676
|
+
end: this.file.location(origin.end)
|
|
677
|
+
} };
|
|
678
|
+
}
|
|
679
|
+
origin(position) {
|
|
680
|
+
if (position === void 0) return;
|
|
681
|
+
if (!isPlainObject(position) || !("start" in position) || !("end" in position) || !isPlainObject(position.start) || !isPlainObject(position.end)) throw new UnifiedBoundaryError("A returned Unist position is malformed");
|
|
682
|
+
const start = "offset" in position.start ? position.start.offset : void 0;
|
|
683
|
+
const end = "offset" in position.end ? position.end.offset : void 0;
|
|
684
|
+
if (!Number.isInteger(start) || !Number.isInteger(end) || !isNumber(start) || !isNumber(end) || start < 0 || end < start || end > this.file.length) throw new UnifiedBoundaryError("A returned Unist position has invalid UTF-16 offsets");
|
|
685
|
+
return {
|
|
686
|
+
start,
|
|
687
|
+
end
|
|
688
|
+
};
|
|
689
|
+
}
|
|
690
|
+
};
|
|
691
|
+
function stripSemantic(node) {
|
|
692
|
+
return node.kind === "codeBlock" || node.kind === "math" || node.kind === "footnoteReference" || node.kind === "footnoteSection" ? htmlFallback(node) : node;
|
|
693
|
+
}
|
|
694
|
+
function directiveToUnist(type, node, base, bridge) {
|
|
695
|
+
return {
|
|
696
|
+
type,
|
|
697
|
+
name: node.name,
|
|
698
|
+
attributes: Object.fromEntries(node.attributes.map((entry) => [entry.name, entry.value])),
|
|
699
|
+
children: node.label === void 0 ? [] : node.label.children.map((child) => bridge.mdxToUnist(child)),
|
|
700
|
+
...base
|
|
701
|
+
};
|
|
702
|
+
}
|
|
703
|
+
function jsxElementToUnist(type, node, base, bridge) {
|
|
704
|
+
return {
|
|
705
|
+
type,
|
|
706
|
+
name: node.name ?? null,
|
|
707
|
+
attributes: node.attributes.map((attribute) => jsxAttributeToUnist(attribute, bridge)),
|
|
708
|
+
children: node.children.map((child) => bridge.mdxToUnist(child)),
|
|
709
|
+
...base
|
|
710
|
+
};
|
|
711
|
+
}
|
|
712
|
+
function jsxAttributeToUnist(node, bridge) {
|
|
713
|
+
const base = bridge.positionBase(node.origin);
|
|
714
|
+
if (node.kind === "jsxSpreadAttribute") return {
|
|
715
|
+
type: "mdxJsxExpressionAttribute",
|
|
716
|
+
value: `...${node.code}`,
|
|
717
|
+
...base
|
|
718
|
+
};
|
|
719
|
+
const value = jsxAttributeValueToUnist(node);
|
|
720
|
+
return {
|
|
721
|
+
type: "mdxJsxAttribute",
|
|
722
|
+
name: node.name,
|
|
723
|
+
value,
|
|
724
|
+
...base
|
|
725
|
+
};
|
|
726
|
+
}
|
|
727
|
+
function jsxAttributeValueToUnist(node) {
|
|
728
|
+
if (node.value === void 0) return null;
|
|
729
|
+
return isString(node.value) ? node.value : {
|
|
730
|
+
type: "mdxJsxAttributeValueExpression",
|
|
731
|
+
value: node.value.code
|
|
732
|
+
};
|
|
733
|
+
}
|
|
734
|
+
function propertiesToHast(attributes, schema) {
|
|
735
|
+
const result = Object.create(null);
|
|
736
|
+
for (const [name, value] of Object.entries(attributes)) result[find(schema, name).property] = value;
|
|
737
|
+
return result;
|
|
738
|
+
}
|
|
739
|
+
function propertiesFromHast(value, schema) {
|
|
740
|
+
if (!isPlainObject(value)) throw new UnifiedBoundaryError("HAST properties must be an object");
|
|
741
|
+
const result = Object.create(null);
|
|
742
|
+
for (const [name, item] of Object.entries(value)) {
|
|
743
|
+
if (item === null || item === void 0) continue;
|
|
744
|
+
if (isBoolean(item) || isString(item) || isNumber(item) && Number.isFinite(item)) {
|
|
745
|
+
result[find(schema, name).attribute] = item;
|
|
746
|
+
continue;
|
|
747
|
+
}
|
|
748
|
+
if (Array.isArray(item) && item.every((entry) => isString(entry) || isNumber(entry) && Number.isFinite(entry))) {
|
|
749
|
+
result[find(schema, name).attribute] = [...item];
|
|
750
|
+
continue;
|
|
751
|
+
}
|
|
752
|
+
throw new UnifiedBoundaryError(`Unsupported HAST property value for ${JSON.stringify(name)}`);
|
|
753
|
+
}
|
|
754
|
+
return result;
|
|
755
|
+
}
|
|
756
|
+
function stringField(node, name) {
|
|
757
|
+
const value = node[name];
|
|
758
|
+
if (!isString(value)) throw new UnifiedBoundaryError(`${node.type}.${name} must be a string`);
|
|
759
|
+
return value;
|
|
760
|
+
}
|
|
761
|
+
function nullableString(value) {
|
|
762
|
+
if (value === null || value === void 0) return;
|
|
763
|
+
if (!isString(value)) throw new UnifiedBoundaryError("Expected a string or null");
|
|
764
|
+
return value;
|
|
765
|
+
}
|
|
766
|
+
function nullableBoolean(value) {
|
|
767
|
+
if (value === null || value === void 0) return;
|
|
768
|
+
if (!isBoolean(value)) throw new UnifiedBoundaryError("Expected a boolean or null");
|
|
769
|
+
return value;
|
|
770
|
+
}
|
|
771
|
+
function booleanField(value, fallback) {
|
|
772
|
+
return value === void 0 || value === null ? fallback : nullableBoolean(value) ?? fallback;
|
|
773
|
+
}
|
|
774
|
+
function nullableNumber(value) {
|
|
775
|
+
if (value === null || value === void 0) return;
|
|
776
|
+
if (!isNumber(value) || !Number.isSafeInteger(value)) throw new UnifiedBoundaryError("Expected a safe integer or null");
|
|
777
|
+
return value;
|
|
778
|
+
}
|
|
779
|
+
function headingDepth(value) {
|
|
780
|
+
if (isHeadingDepth(value)) return value;
|
|
781
|
+
throw new UnifiedBoundaryError("heading.depth must be between 1 and 6");
|
|
782
|
+
}
|
|
783
|
+
function referenceKind(value) {
|
|
784
|
+
if (value === "full" || value === "collapsed" || value === "shortcut") return value;
|
|
785
|
+
throw new UnifiedBoundaryError("A reference has an invalid referenceType");
|
|
786
|
+
}
|
|
787
|
+
function mathFormat(node) {
|
|
788
|
+
return isPlainObject(node.data) && unifiedDataKeys.mathFormat in node.data && node.data[unifiedDataKeys.mathFormat] === "tex" ? "tex" : "dollar";
|
|
789
|
+
}
|
|
790
|
+
function tableAlignments(value) {
|
|
791
|
+
if (!Array.isArray(value)) return [];
|
|
792
|
+
return value.map((item) => item === null ? "none" : item === "left" || item === "right" || item === "center" ? item : (() => {
|
|
793
|
+
throw new UnifiedBoundaryError("table.align contains an invalid value");
|
|
794
|
+
})());
|
|
795
|
+
}
|
|
796
|
+
function directiveAttributes(value) {
|
|
797
|
+
if (value === null || value === void 0) return [];
|
|
798
|
+
if (!isPlainObject(value)) throw new UnifiedBoundaryError("Directive attributes must be an object");
|
|
799
|
+
return Object.entries(value).map(([name, item]) => ({
|
|
800
|
+
name,
|
|
801
|
+
value: nullableString(item) ?? ""
|
|
802
|
+
}));
|
|
803
|
+
}
|
|
804
|
+
function directiveLabel(values, type) {
|
|
805
|
+
return values.length === 0 ? void 0 : {
|
|
806
|
+
kind: "directiveLabel",
|
|
807
|
+
children: phrasing(values, type)
|
|
808
|
+
};
|
|
809
|
+
}
|
|
810
|
+
function directiveDataLabel(value, bridge) {
|
|
811
|
+
if (!isPlainObject(value) || !(unifiedDataKeys.directiveLabel in value) || value[unifiedDataKeys.directiveLabel] === void 0) return;
|
|
812
|
+
const label = bridge.unistToMdx(value[unifiedDataKeys.directiveLabel]);
|
|
813
|
+
if (label.kind !== "directiveLabel") throw new UnifiedBoundaryError("Container directive label is malformed");
|
|
814
|
+
return label;
|
|
815
|
+
}
|
|
816
|
+
function jsxAttributes(value, bridge) {
|
|
817
|
+
if (!Array.isArray(value)) throw new UnifiedBoundaryError("MDX JSX attributes must be an array");
|
|
818
|
+
return value.map((item) => {
|
|
819
|
+
const node = bridge.nodeRecord(item);
|
|
820
|
+
const origin = bridge.origin(node.position);
|
|
821
|
+
bridge.visiting.delete(node);
|
|
822
|
+
if (node.type === "mdxJsxExpressionAttribute") return {
|
|
823
|
+
kind: "jsxSpreadAttribute",
|
|
824
|
+
code: stringField(node, "value").replace(/^\s*\.\.\./u, ""),
|
|
825
|
+
origin
|
|
826
|
+
};
|
|
827
|
+
if (node.type !== "mdxJsxAttribute") throw new UnifiedBoundaryError(`Unsupported MDX JSX attribute type: ${node.type}`);
|
|
828
|
+
const name = stringField(node, "name");
|
|
829
|
+
if (node.value === null || node.value === void 0 || isString(node.value)) return {
|
|
830
|
+
kind: "jsxAttribute",
|
|
831
|
+
name,
|
|
832
|
+
value: node.value ?? void 0,
|
|
833
|
+
origin
|
|
834
|
+
};
|
|
835
|
+
const expression = bridge.nodeRecord(node.value);
|
|
836
|
+
if (expression.type !== "mdxJsxAttributeValueExpression") throw new UnifiedBoundaryError("Unsupported MDX JSX attribute value");
|
|
837
|
+
bridge.visiting.delete(expression);
|
|
838
|
+
return {
|
|
839
|
+
kind: "jsxAttribute",
|
|
840
|
+
name,
|
|
841
|
+
value: {
|
|
842
|
+
kind: "jsxAttributeValueExpression",
|
|
843
|
+
code: stringField(expression, "value"),
|
|
844
|
+
origin: bridge.origin(expression.position)
|
|
845
|
+
},
|
|
846
|
+
origin
|
|
847
|
+
};
|
|
848
|
+
});
|
|
849
|
+
}
|
|
850
|
+
function phrasing(values, parent) {
|
|
851
|
+
if (!values.every(isPhrasingContent)) throw new UnifiedBoundaryError(`${parent} contains non-phrasing content`);
|
|
852
|
+
return values;
|
|
853
|
+
}
|
|
854
|
+
function flow(values, parent) {
|
|
855
|
+
if (!values.every(isFlowContent)) throw new UnifiedBoundaryError(`${parent} contains non-flow content`);
|
|
856
|
+
return values;
|
|
857
|
+
}
|
|
858
|
+
function listItems(values, parent) {
|
|
859
|
+
if (!values.every((node) => node.kind === "listItem")) throw new UnifiedBoundaryError(`${parent} contains a non-list-item child`);
|
|
860
|
+
return values;
|
|
861
|
+
}
|
|
862
|
+
function tableCells(values, parent) {
|
|
863
|
+
if (!values.every((node) => node.kind === "tableCell")) throw new UnifiedBoundaryError(`${parent} contains a non-table-cell child`);
|
|
864
|
+
return values;
|
|
865
|
+
}
|
|
866
|
+
function tableRows(values, parent) {
|
|
867
|
+
if (!values.every((node) => node.kind === "tableRow")) throw new UnifiedBoundaryError(`${parent} contains a non-table-row child`);
|
|
868
|
+
return values;
|
|
869
|
+
}
|
|
870
|
+
function isUnistNode(value) {
|
|
871
|
+
return isPlainObject(value) && "type" in value && isString(value.type);
|
|
872
|
+
}
|
|
873
|
+
//#endregion
|
|
874
|
+
export { UnifiedBoundaryError, UnifiedTreeBridge };
|
|
875
|
+
|
|
876
|
+
//# sourceMappingURL=tree-bridge.mjs.map
|