@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,47 @@
|
|
|
1
|
+
import { indentLines } from "./format-context.mjs";
|
|
2
|
+
import { isString } from "@temelj/value";
|
|
3
|
+
//#region src/format-jsx.ts
|
|
4
|
+
function formatJsxAttribute(context, node) {
|
|
5
|
+
if (node.value === void 0) return node.name;
|
|
6
|
+
if (!isString(node.value)) return `${node.name}={${indentExpression(node.value.code)}}`;
|
|
7
|
+
const quote = context.options.quote ?? "\"";
|
|
8
|
+
return `${node.name}=${quote}${escapeJsxAttribute(node.value, quote)}${quote}`;
|
|
9
|
+
}
|
|
10
|
+
function formatJsxTextElement(context, node, info) {
|
|
11
|
+
const opening = formatJsxOpening(context, node.name, node.attributes, node.children.length === 0);
|
|
12
|
+
if (node.name !== void 0 && node.children.length === 0) return opening;
|
|
13
|
+
const exit = context.enter("phrasing");
|
|
14
|
+
const body = context.phrasing(node.children, {
|
|
15
|
+
...info,
|
|
16
|
+
after: "<",
|
|
17
|
+
before: ">"
|
|
18
|
+
});
|
|
19
|
+
exit();
|
|
20
|
+
return `${opening}${body}</${node.name ?? ""}>`;
|
|
21
|
+
}
|
|
22
|
+
function formatJsxFlowElement(context, node, info) {
|
|
23
|
+
const opening = formatJsxOpening(context, node.name, node.attributes, node.children.length === 0);
|
|
24
|
+
if (node.name !== void 0 && node.children.length === 0) return opening;
|
|
25
|
+
if (node.children.length === 0) return `${opening}</>`;
|
|
26
|
+
return `${opening}\n${indentLines(context.flow(node.children, {
|
|
27
|
+
...info,
|
|
28
|
+
after: "\n",
|
|
29
|
+
before: "\n"
|
|
30
|
+
}), (line, blank) => blank ? "" : ` ${line}`)}\n</${node.name ?? ""}>`;
|
|
31
|
+
}
|
|
32
|
+
function formatJsxOpening(context, name, attributes, empty) {
|
|
33
|
+
if (name === void 0 && attributes.length > 0) throw new Error("A JSX fragment cannot have attributes");
|
|
34
|
+
const values = attributes.map((attribute) => attribute.kind === "jsxAttribute" ? formatJsxAttribute(context, attribute) : `{...${indentExpression(attribute.code)}}`);
|
|
35
|
+
const suffix = name !== void 0 && empty ? " />" : ">";
|
|
36
|
+
return `<${name ?? ""}${values.length === 0 ? "" : ` ${values.join(" ")}`}${suffix}`;
|
|
37
|
+
}
|
|
38
|
+
function indentExpression(value) {
|
|
39
|
+
return value.replace(/\r\n?|\n/gu, (ending) => `${ending} `);
|
|
40
|
+
}
|
|
41
|
+
function escapeJsxAttribute(value, quote) {
|
|
42
|
+
return value.replaceAll("&", "&").replaceAll("<", "<").replaceAll(quote, quote === "\"" ? """ : "'");
|
|
43
|
+
}
|
|
44
|
+
//#endregion
|
|
45
|
+
export { formatJsxFlowElement, formatJsxTextElement, indentExpression };
|
|
46
|
+
|
|
47
|
+
//# sourceMappingURL=format-jsx.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"format-jsx.mjs","names":[],"sources":["../src/format-jsx.ts"],"sourcesContent":["import { isString } from \"@temelj/value\";\n\nimport type { FormatContext, FormatInfo } from \"./format-context.ts\";\nimport type {\n JsxAttribute,\n JsxAttributeLike,\n JsxFlowElementNode,\n JsxTextElementNode,\n} from \"./model.ts\";\n\nimport { indentLines } from \"./format-context.ts\";\n\n// MDX JSX elements and their attributes.\n\nfunction formatJsxAttribute(context: FormatContext, node: JsxAttribute): string {\n if (node.value === undefined) {\n return node.name;\n }\n\n if (!isString(node.value)) {\n return `${node.name}={${indentExpression(node.value.code)}}`;\n }\n\n const quote = context.options.quote ?? '\"';\n\n return `${node.name}=${quote}${escapeJsxAttribute(node.value, quote)}${quote}`;\n}\n\nexport function formatJsxTextElement(\n context: FormatContext,\n node: JsxTextElementNode,\n info: FormatInfo,\n): string {\n const opening = formatJsxOpening(context, node.name, node.attributes, node.children.length === 0);\n\n if (node.name !== undefined && node.children.length === 0) {\n return opening;\n }\n\n const exit = context.enter(\"phrasing\");\n const body = context.phrasing(node.children, { ...info, after: \"<\", before: \">\" });\n exit();\n\n return `${opening}${body}</${node.name ?? \"\"}>`;\n}\n\nexport function formatJsxFlowElement(\n context: FormatContext,\n node: JsxFlowElementNode,\n info: FormatInfo,\n): string {\n const opening = formatJsxOpening(context, node.name, node.attributes, node.children.length === 0);\n\n if (node.name !== undefined && node.children.length === 0) {\n return opening;\n }\n\n if (node.children.length === 0) {\n return `${opening}</>`;\n }\n\n const body = indentLines(\n context.flow(node.children, { ...info, after: \"\\n\", before: \"\\n\" }),\n (line, blank) => (blank ? \"\" : ` ${line}`),\n );\n\n return `${opening}\\n${body}\\n</${node.name ?? \"\"}>`;\n}\n\nfunction formatJsxOpening(\n context: FormatContext,\n name: string | undefined,\n attributes: readonly JsxAttributeLike[],\n empty: boolean,\n): string {\n if (name === undefined && attributes.length > 0) {\n throw new Error(\"A JSX fragment cannot have attributes\");\n }\n\n const values = attributes.map((attribute) =>\n attribute.kind === \"jsxAttribute\"\n ? formatJsxAttribute(context, attribute)\n : `{...${indentExpression(attribute.code)}}`,\n );\n\n const suffix = name !== undefined && empty ? \" />\" : \">\";\n\n return `<${name ?? \"\"}${values.length === 0 ? \"\" : ` ${values.join(\" \")}`}${suffix}`;\n}\n\nexport function indentExpression(value: string): string {\n return value.replace(/\\r\\n?|\\n/gu, (ending) => `${ending} `);\n}\n\nfunction escapeJsxAttribute(value: string, quote: '\"' | \"'\"): string {\n return value\n .replaceAll(\"&\", \"&\")\n .replaceAll(\"<\", \"<\")\n .replaceAll(quote, quote === '\"' ? \""\" : \"'\");\n}\n"],"mappings":";;;AAcA,SAAS,mBAAmB,SAAwB,MAA4B;CAC9E,IAAI,KAAK,UAAU,KAAA,GACjB,OAAO,KAAK;CAGd,IAAI,CAAC,SAAS,KAAK,KAAK,GACtB,OAAO,GAAG,KAAK,KAAK,IAAI,iBAAiB,KAAK,MAAM,IAAI,EAAE;CAG5D,MAAM,QAAQ,QAAQ,QAAQ,SAAS;CAEvC,OAAO,GAAG,KAAK,KAAK,GAAG,QAAQ,mBAAmB,KAAK,OAAO,KAAK,IAAI;AACzE;AAEA,SAAgB,qBACd,SACA,MACA,MACQ;CACR,MAAM,UAAU,iBAAiB,SAAS,KAAK,MAAM,KAAK,YAAY,KAAK,SAAS,WAAW,CAAC;CAEhG,IAAI,KAAK,SAAS,KAAA,KAAa,KAAK,SAAS,WAAW,GACt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AAO,UAAU,OAAM,WAAW,QAAQ;AAC1D"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { MathFormat } from "./syntax-options.mjs";
|
|
2
|
+
import { DisplayMathNode, InlineMathNode, MdxNode } from "./model.mjs";
|
|
3
|
+
import { FormatContext, FormatInfo } from "./format-context.mjs";
|
|
4
|
+
//#region src/format-math.d.ts
|
|
5
|
+
export declare function formatDisplayMath(context: FormatContext, node: DisplayMathNode, info: FormatInfo): string;
|
|
6
|
+
export declare function formatInlineMath(context: FormatContext, node: InlineMathNode): string;
|
|
7
|
+
export declare function mathFormats(node: MdxNode): ReadonlySet<MathFormat>;
|
|
8
|
+
//#endregion
|
|
9
|
+
//# sourceMappingURL=format-math.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"format-math.d.mts","names":[],"sources":["../src/format-math.ts"],"mappings":";;;;wBAWgB,kBACd,SAAS,eACT,MAAM,iBACN,MAAM;wBAwDQ,iBAAiB,SAAS,eAAe,MAAM;wBAsD/C,YAAY,MAAM,UAAU,YAAY"}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { InputError } from "./errors.mjs";
|
|
2
|
+
import { longestRun } from "./format-context.mjs";
|
|
3
|
+
import { FormatTracker } from "./format-track.mjs";
|
|
4
|
+
import { mdxChildren } from "./model.mjs";
|
|
5
|
+
//#region src/format-math.ts
|
|
6
|
+
function formatDisplayMath(context, node, info) {
|
|
7
|
+
if (node.format === "tex") {
|
|
8
|
+
if (node.meta !== void 0) throw InputError.mathMetadataUnsupported();
|
|
9
|
+
if (node.value.includes("\\]")) throw InputError.mathDelimiterCollision("displayMath", "\\]");
|
|
10
|
+
const exit = context.enter("mathFlow");
|
|
11
|
+
const tracker = new FormatTracker(info);
|
|
12
|
+
let value = tracker.move("\\[");
|
|
13
|
+
value += tracker.move("\n");
|
|
14
|
+
if (node.value.length > 0) value += tracker.move(`${node.value}\n`);
|
|
15
|
+
value += tracker.move("\\]");
|
|
16
|
+
exit();
|
|
17
|
+
return value;
|
|
18
|
+
}
|
|
19
|
+
const sequence = "$".repeat(Math.max(2, longestRun(node.value, "$") + 1));
|
|
20
|
+
const exit = context.enter("mathFlow");
|
|
21
|
+
const tracker = new FormatTracker(info);
|
|
22
|
+
let value = tracker.move(sequence);
|
|
23
|
+
if (node.meta !== void 0 && node.meta.length > 0) {
|
|
24
|
+
const exitMeta = context.enter("mathFlowMeta");
|
|
25
|
+
value += tracker.move(context.safe(node.meta, {
|
|
26
|
+
after: "\n",
|
|
27
|
+
before: value,
|
|
28
|
+
encode: ["$"],
|
|
29
|
+
...tracker.current()
|
|
30
|
+
}));
|
|
31
|
+
exitMeta();
|
|
32
|
+
}
|
|
33
|
+
value += tracker.move("\n");
|
|
34
|
+
if (node.value.length > 0) value += tracker.move(`${node.value}\n`);
|
|
35
|
+
value += tracker.move(sequence);
|
|
36
|
+
exit();
|
|
37
|
+
return value;
|
|
38
|
+
}
|
|
39
|
+
function formatInlineMath(context, node) {
|
|
40
|
+
if (node.format === "tex") {
|
|
41
|
+
if (node.value.includes("\\)")) throw InputError.mathDelimiterCollision("inlineMath", "\\)");
|
|
42
|
+
return `\\(${node.value}\\)`;
|
|
43
|
+
}
|
|
44
|
+
let value = node.value;
|
|
45
|
+
let size = 1;
|
|
46
|
+
while (new RegExp(`(^|[^$])${"\\$".repeat(size)}([^$]|$)`, "u").test(value)) size++;
|
|
47
|
+
const sequence = "$".repeat(size);
|
|
48
|
+
if (/[^ \r\n]/u.test(value) && (/^[ \r\n]/u.test(value) && /[ \r\n]$/u.test(value) || /^\$|\$$/u.test(value))) value = ` ${value} `;
|
|
49
|
+
for (const pattern of context.unsafe) {
|
|
50
|
+
if (pattern.atBreak !== true) continue;
|
|
51
|
+
const expression = context.compilePattern(pattern);
|
|
52
|
+
expression.lastIndex = 0;
|
|
53
|
+
let match = expression.exec(value);
|
|
54
|
+
while (match !== null) {
|
|
55
|
+
let position = match.index;
|
|
56
|
+
if (value.charCodeAt(position) === 10 && value.charCodeAt(position - 1) === 13) position--;
|
|
57
|
+
value = `${value.slice(0, position)} ${value.slice(match.index + 1)}`;
|
|
58
|
+
expression.lastIndex = 0;
|
|
59
|
+
match = expression.exec(value);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
const exit = context.enter("inlineMath");
|
|
63
|
+
const result = `${sequence}${value}${sequence}`;
|
|
64
|
+
exit();
|
|
65
|
+
return result;
|
|
66
|
+
}
|
|
67
|
+
function mathFormats(node) {
|
|
68
|
+
function collectMathFormats(node, formats) {
|
|
69
|
+
if (node.kind === "inlineMath" || node.kind === "displayMath") formats.add(node.format);
|
|
70
|
+
for (const child of mdxChildren(node)) collectMathFormats(child, formats);
|
|
71
|
+
}
|
|
72
|
+
const formats = /* @__PURE__ */ new Set();
|
|
73
|
+
collectMathFormats(node, formats);
|
|
74
|
+
return formats;
|
|
75
|
+
}
|
|
76
|
+
//#endregion
|
|
77
|
+
export { formatDisplayMath, formatInlineMath, mathFormats };
|
|
78
|
+
|
|
79
|
+
//# sourceMappingURL=format-math.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"format-math.mjs","names":[],"sources":["../src/format-math.ts"],"sourcesContent":["import type { FormatContext, FormatInfo } from \"./format-context.ts\";\nimport type { DisplayMathNode, InlineMathNode, MdxNode } from \"./model.ts\";\nimport type { MathFormat } from \"./syntax-options.ts\";\n\nimport { InputError } from \"./errors.ts\";\nimport { longestRun } from \"./format-context.ts\";\nimport { FormatTracker } from \"./format-track.ts\";\nimport { mdxChildren } from \"./model.ts\";\n\n// Inline and display math.\n\nexport function formatDisplayMath(\n context: FormatContext,\n node: DisplayMathNode,\n info: FormatInfo,\n): string {\n if (node.format === \"tex\") {\n if (node.meta !== undefined) {\n throw InputError.mathMetadataUnsupported();\n }\n\n if (node.value.includes(\"\\\\]\")) {\n throw InputError.mathDelimiterCollision(\"displayMath\", \"\\\\]\");\n }\n\n const exit = context.enter(\"mathFlow\");\n const tracker = new FormatTracker(info);\n let value = tracker.move(\"\\\\[\");\n value += tracker.move(\"\\n\");\n\n if (node.value.length > 0) {\n value += tracker.move(`${node.value}\\n`);\n }\n\n value += tracker.move(\"\\\\]\");\n exit();\n\n return value;\n }\n\n const sequence = \"$\".repeat(Math.max(2, longestRun(node.value, \"$\") + 1));\n const exit = context.enter(\"mathFlow\");\n const tracker = new FormatTracker(info);\n let value = tracker.move(sequence);\n\n if (node.meta !== undefined && node.meta.length > 0) {\n const exitMeta = context.enter(\"mathFlowMeta\");\n value += tracker.move(\n context.safe(node.meta, {\n after: \"\\n\",\n before: value,\n encode: [\"$\"],\n ...tracker.current(),\n }),\n );\n exitMeta();\n }\n\n value += tracker.move(\"\\n\");\n\n if (node.value.length > 0) {\n value += tracker.move(`${node.value}\\n`);\n }\n\n value += tracker.move(sequence);\n exit();\n\n return value;\n}\n\nexport function formatInlineMath(context: FormatContext, node: InlineMathNode): string {\n if (node.format === \"tex\") {\n if (node.value.includes(\"\\\\)\")) {\n throw InputError.mathDelimiterCollision(\"inlineMath\", \"\\\\)\");\n }\n\n return `\\\\(${node.value}\\\\)`;\n }\n\n let value = node.value;\n let size = 1;\n\n while (new RegExp(`(^|[^$])${\"\\\\$\".repeat(size)}([^$]|$)`, \"u\").test(value)) {\n size++;\n }\n\n const sequence = \"$\".repeat(size);\n\n if (\n /[^ \\r\\n]/u.test(value) &&\n ((/^[ \\r\\n]/u.test(value) && /[ \\r\\n]$/u.test(value)) || /^\\$|\\$$/u.test(value))\n ) {\n value = ` ${value} `;\n }\n\n for (const pattern of context.unsafe) {\n if (pattern.atBreak !== true) {\n continue;\n }\n\n const expression = context.compilePattern(pattern);\n expression.lastIndex = 0;\n let match = expression.exec(value);\n\n while (match !== null) {\n let position = match.index;\n\n if (value.charCodeAt(position) === 10 && value.charCodeAt(position - 1) === 13) {\n position--;\n }\n\n value = `${value.slice(0, position)} ${value.slice(match.index + 1)}`;\n expression.lastIndex = 0;\n match = expression.exec(value);\n }\n }\n\n const exit = context.enter(\"inlineMath\");\n const result = `${sequence}${value}${sequence}`;\n exit();\n\n return result;\n}\n\nexport function mathFormats(node: MdxNode): ReadonlySet<MathFormat> {\n function collectMathFormats(node: MdxNode, formats: Set<MathFormat>): void {\n if (node.kind === \"inlineMath\" || node.kind === \"displayMath\") {\n formats.add(node.format);\n }\n\n for (const child of mdxChildren(node)) {\n collectMathFormats(child, formats);\n }\n }\n\n const formats = new Set<MathFormat>();\n collectMathFormats(node, formats);\n\n return formats;\n}\n"],"mappings":";;;;;AAWA,SAAgB,kBACd,SACA,MACA,MACQ;CACR,IAAI,KAAK,WAAW,OAAO;EACzB,IAAI,KAAK,SAAS,KAAA,GAChB,MAAM,WAAW,wBAAwB;EAG3C,IAAI,KAAK,MAAM,SAAS,KAAK,GAC3B,MAAM,WAAW,uBAAuB,eAAe,KAAK;EAG9D,MAAM,OAAO,QAAQ,MAAM,UAAU;EACrC,MAAM,UAAU,IAAI,cAAc,IAAI;EACtC,IAAI,QAAQ,QAAQ,KAAK,KAAK;EAC9B,SAAS,QAAQ,KAAK,IAAI;EAE1B,IAAI,KAAK,MAAM,SAAS,GACtB,SAAS,QAAQ,KAAK,GAAG,KAAK,MAAM,GAAG;EAGzC,SAAS,QAAQ,KAAK,KAAK;EAC3B,KAAK;EAEL,OAAO;CACT;CAEA,MAAM,WAAW,IAAI,OAAO,KAAK,IAAI,GAAG,WAAW,KAAK,OAAO,GAAG,IAAI,CAAC,CAAC;CACxE,MAAM,OAAO,QAAQ,MAAM,UAAU;CACrC,MAAM,UAAU,IAAI,cAAc,IAAI;CACtC,IAAI,QAAQ,QAAQ,KAAK,QAAQ;CAEjC,IAAI,KAAK,SAAS,KAAA,KAAa,KAAK,KAAK,SAAS,GAAG;EACnD,MAAM,WAAW,QAAQ,MAAM,cAAc;EAC7C,SAAS,QAAQ,KACf,QAAQ,KAAK,KAAK,MAAM;GACtB,OAAO;GACP,QAAQ;GACR,QAAQ,CAAC,GAAG;GACZ,GAAG,QAAQ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AEL,OAAO;AACT;AAEA,SAAgB,YAAY,MAAwC;CAClE,SAAS,mBAAmB,MAAe,SAAgC;EACzE,IAAI,KAAK,SAAS,gBAAgB,KAAK,SAAS,eAC9C,QAAQ,IAAI,KAAK,MAAM;EAGzB,KAAK,MAAM,SAAS,YAAY,IAAI,GAClC,mBAAmB,OAAO,OAAO;CAErC;CAEA,MAAM,0BAAU,IAAI,IAAgB;CACpC,mBAAmB,MAAM,OAAO;CAEhC,OAAO;AACT"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
//#region src/format-safe.d.ts
|
|
2
|
+
export type FormatConstruct = "autolink" | "blockquote" | "codeFencedLangGraveAccent" | "codeFencedLangTilde" | "codeFencedMetaGraveAccent" | "codeFencedMetaTilde" | "definition" | "containerDirective" | "directiveLabel" | "destinationLiteral" | "destinationRaw" | "mathFlow" | "mathFlowMeta" | "emphasis" | "footnoteDefinition" | "footnoteReference" | "headingAtx" | "headingSetext" | "image" | "imageReference" | "label" | "link" | "linkReference" | "inlineMath" | "list" | "listItem" | "leafDirective" | "paragraph" | "phrasing" | "reference" | "strong" | "strikethrough" | "table" | "tableCell" | "tableRow" | "textDirective" | "titleApostrophe" | "titleQuote";
|
|
3
|
+
export interface FormatUnsafePattern {
|
|
4
|
+
readonly character: string;
|
|
5
|
+
readonly after?: string;
|
|
6
|
+
readonly atBreak?: true;
|
|
7
|
+
readonly before?: string;
|
|
8
|
+
readonly inConstruct?: FormatConstruct | readonly FormatConstruct[];
|
|
9
|
+
readonly notInConstruct?: FormatConstruct | readonly FormatConstruct[];
|
|
10
|
+
}
|
|
11
|
+
export interface FormatSafeInfo {
|
|
12
|
+
readonly after: string;
|
|
13
|
+
readonly before: string;
|
|
14
|
+
readonly encode?: readonly string[];
|
|
15
|
+
}
|
|
16
|
+
export interface FormatSafetyState {
|
|
17
|
+
readonly stack: readonly FormatConstruct[];
|
|
18
|
+
readonly unsafe: readonly FormatUnsafePattern[];
|
|
19
|
+
compilePattern(pattern: FormatUnsafePattern): RegExp;
|
|
20
|
+
}
|
|
21
|
+
export declare const UNSAFE_PATTERNS: readonly FormatUnsafePattern[];
|
|
22
|
+
export declare function safeSource(state: FormatSafetyState, input: string, info: FormatSafeInfo): string;
|
|
23
|
+
export declare function compileUnsafePattern(pattern: FormatUnsafePattern): RegExp;
|
|
24
|
+
//#endregion
|
|
25
|
+
//# sourceMappingURL=format-safe.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"format-safe.d.mts","names":[],"sources":["../src/format-safe.ts"],"mappings":";YAIY;iBAwCK;WACN;WACA;WACA;WACA;WACA,cAAc,2BAA2B;WACzC,iBAAiB,2BAA2B;;iBAGtC;WACN;WACA;WACA;;iBAGM;WACN,gBAAgB;WAChB,iBAAiB;EAC1B,eAAe,SAAS,sBAAsB;;qBAYnC,0BAA0B;wBAgGvB,WAAW,OAAO,mBAAmB,eAAe,MAAM;wBA4F1D,qBAAqB,SAAS,sBAAsB"}
|
|
@@ -0,0 +1,366 @@
|
|
|
1
|
+
import { encodeNumericCharacterReference } from "./character-reference.mjs";
|
|
2
|
+
import { isString } from "@temelj/value";
|
|
3
|
+
//#region src/format-safe.ts
|
|
4
|
+
const fullPhrasingSpans = [
|
|
5
|
+
"autolink",
|
|
6
|
+
"destinationLiteral",
|
|
7
|
+
"destinationRaw",
|
|
8
|
+
"reference",
|
|
9
|
+
"titleQuote",
|
|
10
|
+
"titleApostrophe"
|
|
11
|
+
];
|
|
12
|
+
const UNSAFE_PATTERNS = [
|
|
13
|
+
{
|
|
14
|
+
character: " ",
|
|
15
|
+
after: "[\\r\\n]",
|
|
16
|
+
inConstruct: "phrasing"
|
|
17
|
+
},
|
|
18
|
+
{
|
|
19
|
+
character: "\r",
|
|
20
|
+
inConstruct: "tableCell"
|
|
21
|
+
},
|
|
22
|
+
{
|
|
23
|
+
character: "\n",
|
|
24
|
+
inConstruct: "tableCell"
|
|
25
|
+
},
|
|
26
|
+
{
|
|
27
|
+
character: " ",
|
|
28
|
+
before: "[\\r\\n]",
|
|
29
|
+
inConstruct: "phrasing"
|
|
30
|
+
},
|
|
31
|
+
{
|
|
32
|
+
character: " ",
|
|
33
|
+
inConstruct: ["codeFencedLangGraveAccent", "codeFencedLangTilde"]
|
|
34
|
+
},
|
|
35
|
+
{
|
|
36
|
+
character: "\r",
|
|
37
|
+
inConstruct: [
|
|
38
|
+
"codeFencedLangGraveAccent",
|
|
39
|
+
"codeFencedLangTilde",
|
|
40
|
+
"codeFencedMetaGraveAccent",
|
|
41
|
+
"codeFencedMetaTilde",
|
|
42
|
+
"destinationLiteral",
|
|
43
|
+
"headingAtx"
|
|
44
|
+
]
|
|
45
|
+
},
|
|
46
|
+
{
|
|
47
|
+
character: "\n",
|
|
48
|
+
inConstruct: [
|
|
49
|
+
"codeFencedLangGraveAccent",
|
|
50
|
+
"codeFencedLangTilde",
|
|
51
|
+
"codeFencedMetaGraveAccent",
|
|
52
|
+
"codeFencedMetaTilde",
|
|
53
|
+
"destinationLiteral",
|
|
54
|
+
"headingAtx"
|
|
55
|
+
]
|
|
56
|
+
},
|
|
57
|
+
{
|
|
58
|
+
character: " ",
|
|
59
|
+
after: "[\\r\\n]",
|
|
60
|
+
inConstruct: "phrasing"
|
|
61
|
+
},
|
|
62
|
+
{
|
|
63
|
+
character: " ",
|
|
64
|
+
before: "[\\r\\n]",
|
|
65
|
+
inConstruct: "phrasing"
|
|
66
|
+
},
|
|
67
|
+
{
|
|
68
|
+
character: " ",
|
|
69
|
+
inConstruct: ["codeFencedLangGraveAccent", "codeFencedLangTilde"]
|
|
70
|
+
},
|
|
71
|
+
{
|
|
72
|
+
character: "!",
|
|
73
|
+
after: "\\[",
|
|
74
|
+
inConstruct: "phrasing",
|
|
75
|
+
notInConstruct: fullPhrasingSpans
|
|
76
|
+
},
|
|
77
|
+
{
|
|
78
|
+
character: "\"",
|
|
79
|
+
inConstruct: "titleQuote"
|
|
80
|
+
},
|
|
81
|
+
{
|
|
82
|
+
atBreak: true,
|
|
83
|
+
character: "#"
|
|
84
|
+
},
|
|
85
|
+
{
|
|
86
|
+
character: "#",
|
|
87
|
+
inConstruct: "headingAtx",
|
|
88
|
+
after: "(?:[\\r\\n]|$)"
|
|
89
|
+
},
|
|
90
|
+
{
|
|
91
|
+
character: "&",
|
|
92
|
+
after: "[#A-Za-z]",
|
|
93
|
+
inConstruct: "phrasing"
|
|
94
|
+
},
|
|
95
|
+
{
|
|
96
|
+
character: "@",
|
|
97
|
+
before: "[+\\-.\\w]",
|
|
98
|
+
after: "[\\-.\\w]",
|
|
99
|
+
inConstruct: "phrasing",
|
|
100
|
+
notInConstruct: [
|
|
101
|
+
"autolink",
|
|
102
|
+
"link",
|
|
103
|
+
"image",
|
|
104
|
+
"label"
|
|
105
|
+
]
|
|
106
|
+
},
|
|
107
|
+
{
|
|
108
|
+
character: "'",
|
|
109
|
+
inConstruct: "titleApostrophe"
|
|
110
|
+
},
|
|
111
|
+
{
|
|
112
|
+
character: "(",
|
|
113
|
+
inConstruct: "destinationRaw"
|
|
114
|
+
},
|
|
115
|
+
{
|
|
116
|
+
before: "\\]",
|
|
117
|
+
character: "(",
|
|
118
|
+
inConstruct: "phrasing",
|
|
119
|
+
notInConstruct: fullPhrasingSpans
|
|
120
|
+
},
|
|
121
|
+
{
|
|
122
|
+
atBreak: true,
|
|
123
|
+
before: "\\d+",
|
|
124
|
+
character: ")"
|
|
125
|
+
},
|
|
126
|
+
{
|
|
127
|
+
character: ")",
|
|
128
|
+
inConstruct: "destinationRaw"
|
|
129
|
+
},
|
|
130
|
+
{
|
|
131
|
+
character: ".",
|
|
132
|
+
before: "[Ww]",
|
|
133
|
+
after: "[\\-.\\w]",
|
|
134
|
+
inConstruct: "phrasing",
|
|
135
|
+
notInConstruct: [
|
|
136
|
+
"autolink",
|
|
137
|
+
"link",
|
|
138
|
+
"image",
|
|
139
|
+
"label"
|
|
140
|
+
]
|
|
141
|
+
},
|
|
142
|
+
{
|
|
143
|
+
character: ":",
|
|
144
|
+
before: "[ps]",
|
|
145
|
+
after: "\\/",
|
|
146
|
+
inConstruct: "phrasing",
|
|
147
|
+
notInConstruct: [
|
|
148
|
+
"autolink",
|
|
149
|
+
"link",
|
|
150
|
+
"image",
|
|
151
|
+
"label"
|
|
152
|
+
]
|
|
153
|
+
},
|
|
154
|
+
{
|
|
155
|
+
atBreak: true,
|
|
156
|
+
character: "*",
|
|
157
|
+
after: "(?:[ \\t\\r\\n*])"
|
|
158
|
+
},
|
|
159
|
+
{
|
|
160
|
+
character: "*",
|
|
161
|
+
inConstruct: "phrasing",
|
|
162
|
+
notInConstruct: fullPhrasingSpans
|
|
163
|
+
},
|
|
164
|
+
{
|
|
165
|
+
atBreak: true,
|
|
166
|
+
character: "+",
|
|
167
|
+
after: "(?:[ \\t\\r\\n])"
|
|
168
|
+
},
|
|
169
|
+
{
|
|
170
|
+
atBreak: true,
|
|
171
|
+
character: "-",
|
|
172
|
+
after: "(?:[ \\t\\r\\n-])"
|
|
173
|
+
},
|
|
174
|
+
{
|
|
175
|
+
atBreak: true,
|
|
176
|
+
before: "\\d+",
|
|
177
|
+
character: ".",
|
|
178
|
+
after: "(?:[ \\t\\r\\n]|$)"
|
|
179
|
+
},
|
|
180
|
+
{
|
|
181
|
+
atBreak: true,
|
|
182
|
+
character: "<",
|
|
183
|
+
after: "[!/?A-Za-z]"
|
|
184
|
+
},
|
|
185
|
+
{
|
|
186
|
+
character: "<",
|
|
187
|
+
after: "[!/?A-Za-z]",
|
|
188
|
+
inConstruct: "phrasing",
|
|
189
|
+
notInConstruct: fullPhrasingSpans
|
|
190
|
+
},
|
|
191
|
+
{
|
|
192
|
+
character: "<",
|
|
193
|
+
inConstruct: "destinationLiteral"
|
|
194
|
+
},
|
|
195
|
+
{
|
|
196
|
+
atBreak: true,
|
|
197
|
+
character: "="
|
|
198
|
+
},
|
|
199
|
+
{
|
|
200
|
+
atBreak: true,
|
|
201
|
+
character: ">"
|
|
202
|
+
},
|
|
203
|
+
{
|
|
204
|
+
character: ">",
|
|
205
|
+
inConstruct: "destinationLiteral"
|
|
206
|
+
},
|
|
207
|
+
{
|
|
208
|
+
atBreak: true,
|
|
209
|
+
character: "["
|
|
210
|
+
},
|
|
211
|
+
{
|
|
212
|
+
character: "[",
|
|
213
|
+
inConstruct: "phrasing",
|
|
214
|
+
notInConstruct: fullPhrasingSpans
|
|
215
|
+
},
|
|
216
|
+
{
|
|
217
|
+
character: "[",
|
|
218
|
+
inConstruct: ["label", "reference"]
|
|
219
|
+
},
|
|
220
|
+
{
|
|
221
|
+
character: "\\",
|
|
222
|
+
after: "[\\r\\n]",
|
|
223
|
+
inConstruct: "phrasing"
|
|
224
|
+
},
|
|
225
|
+
{
|
|
226
|
+
character: "]",
|
|
227
|
+
inConstruct: ["label", "reference"]
|
|
228
|
+
},
|
|
229
|
+
{
|
|
230
|
+
atBreak: true,
|
|
231
|
+
character: "_"
|
|
232
|
+
},
|
|
233
|
+
{
|
|
234
|
+
character: "_",
|
|
235
|
+
inConstruct: "phrasing",
|
|
236
|
+
notInConstruct: fullPhrasingSpans
|
|
237
|
+
},
|
|
238
|
+
{
|
|
239
|
+
atBreak: true,
|
|
240
|
+
character: "`"
|
|
241
|
+
},
|
|
242
|
+
{
|
|
243
|
+
character: "`",
|
|
244
|
+
inConstruct: ["codeFencedLangGraveAccent", "codeFencedMetaGraveAccent"]
|
|
245
|
+
},
|
|
246
|
+
{
|
|
247
|
+
character: "`",
|
|
248
|
+
inConstruct: "phrasing",
|
|
249
|
+
notInConstruct: fullPhrasingSpans
|
|
250
|
+
},
|
|
251
|
+
{
|
|
252
|
+
atBreak: true,
|
|
253
|
+
character: "~"
|
|
254
|
+
},
|
|
255
|
+
{
|
|
256
|
+
character: "~",
|
|
257
|
+
inConstruct: "phrasing",
|
|
258
|
+
notInConstruct: fullPhrasingSpans
|
|
259
|
+
},
|
|
260
|
+
{
|
|
261
|
+
atBreak: true,
|
|
262
|
+
character: "|",
|
|
263
|
+
after: "[\\t :-]"
|
|
264
|
+
},
|
|
265
|
+
{
|
|
266
|
+
character: "|",
|
|
267
|
+
inConstruct: "tableCell"
|
|
268
|
+
},
|
|
269
|
+
{
|
|
270
|
+
atBreak: true,
|
|
271
|
+
character: ":",
|
|
272
|
+
after: "-"
|
|
273
|
+
},
|
|
274
|
+
{
|
|
275
|
+
atBreak: true,
|
|
276
|
+
character: "-",
|
|
277
|
+
after: "[:|-]"
|
|
278
|
+
}
|
|
279
|
+
];
|
|
280
|
+
function safeSource(state, input, info) {
|
|
281
|
+
const value = info.before + input + info.after;
|
|
282
|
+
const positions = [];
|
|
283
|
+
const details = /* @__PURE__ */ new Map();
|
|
284
|
+
for (const pattern of state.unsafe) {
|
|
285
|
+
if (!patternInScope(state.stack, pattern)) continue;
|
|
286
|
+
const expression = state.compilePattern(pattern);
|
|
287
|
+
expression.lastIndex = 0;
|
|
288
|
+
let match = expression.exec(value);
|
|
289
|
+
while (match !== null) {
|
|
290
|
+
const before = pattern.before !== void 0 || pattern.atBreak === true;
|
|
291
|
+
const after = pattern.after !== void 0;
|
|
292
|
+
const position = match.index + (before ? match[1]?.length ?? 0 : 0);
|
|
293
|
+
const existing = details.get(position);
|
|
294
|
+
if (existing === void 0) {
|
|
295
|
+
positions.push(position);
|
|
296
|
+
details.set(position, {
|
|
297
|
+
before,
|
|
298
|
+
after
|
|
299
|
+
});
|
|
300
|
+
} else {
|
|
301
|
+
if (existing.before && !before) existing.before = false;
|
|
302
|
+
if (existing.after && !after) existing.after = false;
|
|
303
|
+
}
|
|
304
|
+
match = expression.exec(value);
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
positions.sort((left, right) => left - right);
|
|
308
|
+
let start = info.before.length;
|
|
309
|
+
const end = value.length - info.after.length;
|
|
310
|
+
const result = [];
|
|
311
|
+
for (let index = 0; index < positions.length; index++) {
|
|
312
|
+
const position = positions[index];
|
|
313
|
+
if (position < start || position >= end) continue;
|
|
314
|
+
const detail = details.get(position);
|
|
315
|
+
if (detail === void 0) continue;
|
|
316
|
+
const next = details.get(position + 1);
|
|
317
|
+
const previous = details.get(position - 1);
|
|
318
|
+
if (positions[index + 1] === position + 1 && detail.after && next?.before === false && next.after === false || positions[index - 1] === position - 1 && detail.before && previous?.before === false && previous.after === false) continue;
|
|
319
|
+
if (start !== position) result.push(escapeBackslashes(value.slice(start, position), "\\"));
|
|
320
|
+
start = position;
|
|
321
|
+
const character = value.charAt(position);
|
|
322
|
+
if (/[!-/:-@[-`{-~]/u.test(character) && !info.encode?.includes(character)) result.push("\\");
|
|
323
|
+
else {
|
|
324
|
+
result.push(encodeNumericCharacterReference(value.codePointAt(position) ?? 0));
|
|
325
|
+
start += character.length;
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
result.push(escapeBackslashes(value.slice(start, end), info.after));
|
|
329
|
+
return result.join("");
|
|
330
|
+
}
|
|
331
|
+
function compileUnsafePattern(pattern) {
|
|
332
|
+
const before = `${pattern.atBreak === true ? "[\\r\\n][\\t ]*" : ""}${pattern.before === void 0 ? "" : `(?:${pattern.before})`}`;
|
|
333
|
+
const escaped = /[|\\{}()[\]^$+*?.]/u.test(pattern.character) ? `\\${pattern.character}` : pattern.character;
|
|
334
|
+
return new RegExp(`${before.length === 0 ? "" : `(${before})`}${escaped}${pattern.after === void 0 ? "" : `(?:${pattern.after})`}`, "gu");
|
|
335
|
+
}
|
|
336
|
+
function patternInScope(stack, pattern) {
|
|
337
|
+
return listInScope(stack, pattern.inConstruct, true) && !listInScope(stack, pattern.notInConstruct, false);
|
|
338
|
+
}
|
|
339
|
+
function listInScope(stack, value, whenMissing) {
|
|
340
|
+
if (value === void 0) return whenMissing;
|
|
341
|
+
return isString(value) ? stack.includes(value) : value.some((construct) => stack.includes(construct));
|
|
342
|
+
}
|
|
343
|
+
function escapeBackslashes(value, after) {
|
|
344
|
+
const whole = value + after;
|
|
345
|
+
const positions = [];
|
|
346
|
+
const expression = /\\(?=[!-/:-@[-`{-~])/gu;
|
|
347
|
+
let match = expression.exec(whole);
|
|
348
|
+
while (match !== null) {
|
|
349
|
+
positions.push(match.index);
|
|
350
|
+
match = expression.exec(whole);
|
|
351
|
+
}
|
|
352
|
+
const results = [];
|
|
353
|
+
let start = 0;
|
|
354
|
+
for (const position of positions) {
|
|
355
|
+
if (position >= value.length) break;
|
|
356
|
+
if (start !== position) results.push(value.slice(start, position));
|
|
357
|
+
results.push("\\");
|
|
358
|
+
start = position;
|
|
359
|
+
}
|
|
360
|
+
results.push(value.slice(start));
|
|
361
|
+
return results.join("");
|
|
362
|
+
}
|
|
363
|
+
//#endregion
|
|
364
|
+
export { UNSAFE_PATTERNS, compileUnsafePattern, safeSource };
|
|
365
|
+
|
|
366
|
+
//# sourceMappingURL=format-safe.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"format-safe.mjs","names":[],"sources":["../src/format-safe.ts"],"sourcesContent":["import { isString } from \"@temelj/value\";\n\nimport { encodeNumericCharacterReference } from \"./character-reference.ts\";\n\nexport type FormatConstruct =\n | \"autolink\"\n | \"blockquote\"\n | \"codeFencedLangGraveAccent\"\n | \"codeFencedLangTilde\"\n | \"codeFencedMetaGraveAccent\"\n | \"codeFencedMetaTilde\"\n | \"definition\"\n | \"containerDirective\"\n | \"directiveLabel\"\n | \"destinationLiteral\"\n | \"destinationRaw\"\n | \"mathFlow\"\n | \"mathFlowMeta\"\n | \"emphasis\"\n | \"footnoteDefinition\"\n | \"footnoteReference\"\n | \"headingAtx\"\n | \"headingSetext\"\n | \"image\"\n | \"imageReference\"\n | \"label\"\n | \"link\"\n | \"linkReference\"\n | \"inlineMath\"\n | \"list\"\n | \"listItem\"\n | \"leafDirective\"\n | \"paragraph\"\n | \"phrasing\"\n | \"reference\"\n | \"strong\"\n | \"strikethrough\"\n | \"table\"\n | \"tableCell\"\n | \"tableRow\"\n | \"textDirective\"\n | \"titleApostrophe\"\n | \"titleQuote\";\n\nexport interface FormatUnsafePattern {\n readonly character: string;\n readonly after?: string;\n readonly atBreak?: true;\n readonly before?: string;\n readonly inConstruct?: FormatConstruct | readonly FormatConstruct[];\n readonly notInConstruct?: FormatConstruct | readonly FormatConstruct[];\n}\n\nexport interface FormatSafeInfo {\n readonly after: string;\n readonly before: string;\n readonly encode?: readonly string[];\n}\n\nexport interface FormatSafetyState {\n readonly stack: readonly FormatConstruct[];\n readonly unsafe: readonly FormatUnsafePattern[];\n compilePattern(pattern: FormatUnsafePattern): RegExp;\n}\n\nconst fullPhrasingSpans: readonly FormatConstruct[] = [\n \"autolink\",\n \"destinationLiteral\",\n \"destinationRaw\",\n \"reference\",\n \"titleQuote\",\n \"titleApostrophe\",\n];\n\nexport const UNSAFE_PATTERNS: readonly FormatUnsafePattern[] = [\n { character: \"\\t\", after: \"[\\\\r\\\\n]\", inConstruct: \"phrasing\" },\n { character: \"\\r\", inConstruct: \"tableCell\" },\n { character: \"\\n\", inConstruct: \"tableCell\" },\n { character: \"\\t\", before: \"[\\\\r\\\\n]\", inConstruct: \"phrasing\" },\n { character: \"\\t\", inConstruct: [\"codeFencedLangGraveAccent\", \"codeFencedLangTilde\"] },\n {\n character: \"\\r\",\n inConstruct: [\n \"codeFencedLangGraveAccent\",\n \"codeFencedLangTilde\",\n \"codeFencedMetaGraveAccent\",\n \"codeFencedMetaTilde\",\n \"destinationLiteral\",\n \"headingAtx\",\n ],\n },\n {\n character: \"\\n\",\n inConstruct: [\n \"codeFencedLangGraveAccent\",\n \"codeFencedLangTilde\",\n \"codeFencedMetaGraveAccent\",\n \"codeFencedMetaTilde\",\n \"destinationLiteral\",\n \"headingAtx\",\n ],\n },\n { character: \" \", after: \"[\\\\r\\\\n]\", inConstruct: \"phrasing\" },\n { character: \" \", before: \"[\\\\r\\\\n]\", inConstruct: \"phrasing\" },\n { character: \" \", inConstruct: [\"codeFencedLangGraveAccent\", \"codeFencedLangTilde\"] },\n { character: \"!\", after: \"\\\\[\", inConstruct: \"phrasing\", notInConstruct: fullPhrasingSpans },\n { character: '\"', inConstruct: \"titleQuote\" },\n { atBreak: true, character: \"#\" },\n { character: \"#\", inConstruct: \"headingAtx\", after: \"(?:[\\\\r\\\\n]|$)\" },\n { character: \"&\", after: \"[#A-Za-z]\", inConstruct: \"phrasing\" },\n {\n character: \"@\",\n before: \"[+\\\\-.\\\\w]\",\n after: \"[\\\\-.\\\\w]\",\n inConstruct: \"phrasing\",\n notInConstruct: [\"autolink\", \"link\", \"image\", \"label\"],\n },\n { character: \"'\", inConstruct: \"titleApostrophe\" },\n { character: \"(\", inConstruct: \"destinationRaw\" },\n { before: \"\\\\]\", character: \"(\", inConstruct: \"phrasing\", notInConstruct: fullPhrasingSpans },\n { atBreak: true, before: \"\\\\d+\", character: \")\" },\n { character: \")\", inConstruct: \"destinationRaw\" },\n {\n character: \".\",\n before: \"[Ww]\",\n after: \"[\\\\-.\\\\w]\",\n inConstruct: \"phrasing\",\n notInConstruct: [\"autolink\", \"link\", \"image\", \"label\"],\n },\n {\n character: \":\",\n before: \"[ps]\",\n after: \"\\\\/\",\n inConstruct: \"phrasing\",\n notInConstruct: [\"autolink\", \"link\", \"image\", \"label\"],\n },\n { atBreak: true, character: \"*\", after: \"(?:[ \\\\t\\\\r\\\\n*])\" },\n { character: \"*\", inConstruct: \"phrasing\", notInConstruct: fullPhrasingSpans },\n { atBreak: true, character: \"+\", after: \"(?:[ \\\\t\\\\r\\\\n])\" },\n { atBreak: true, character: \"-\", after: \"(?:[ \\\\t\\\\r\\\\n-])\" },\n { atBreak: true, before: \"\\\\d+\", character: \".\", after: \"(?:[ \\\\t\\\\r\\\\n]|$)\" },\n { atBreak: true, character: \"<\", after: \"[!/?A-Za-z]\" },\n {\n character: \"<\",\n after: \"[!/?A-Za-z]\",\n inConstruct: \"phrasing\",\n notInConstruct: fullPhrasingSpans,\n },\n { character: \"<\", inConstruct: \"destinationLiteral\" },\n { atBreak: true, character: \"=\" },\n { atBreak: true, character: \">\" },\n { character: \">\", inConstruct: \"destinationLiteral\" },\n { atBreak: true, character: \"[\" },\n { character: \"[\", inConstruct: \"phrasing\", notInConstruct: fullPhrasingSpans },\n { character: \"[\", inConstruct: [\"label\", \"reference\"] },\n { character: \"\\\\\", after: \"[\\\\r\\\\n]\", inConstruct: \"phrasing\" },\n { character: \"]\", inConstruct: [\"label\", \"reference\"] },\n { atBreak: true, character: \"_\" },\n { character: \"_\", inConstruct: \"phrasing\", notInConstruct: fullPhrasingSpans },\n { atBreak: true, character: \"`\" },\n { character: \"`\", inConstruct: [\"codeFencedLangGraveAccent\", \"codeFencedMetaGraveAccent\"] },\n { character: \"`\", inConstruct: \"phrasing\", notInConstruct: fullPhrasingSpans },\n { atBreak: true, character: \"~\" },\n { character: \"~\", inConstruct: \"phrasing\", notInConstruct: fullPhrasingSpans },\n { atBreak: true, character: \"|\", after: \"[\\\\t :-]\" },\n { character: \"|\", inConstruct: \"tableCell\" },\n { atBreak: true, character: \":\", after: \"-\" },\n { atBreak: true, character: \"-\", after: \"[:|-]\" },\n];\n\nexport function safeSource(state: FormatSafetyState, input: string, info: FormatSafeInfo): string {\n const value = info.before + input + info.after;\n const positions: number[] = [];\n const details = new Map<number, { before: boolean; after: boolean }>();\n\n for (const pattern of state.unsafe) {\n if (!patternInScope(state.stack, pattern)) {\n continue;\n }\n\n const expression = state.compilePattern(pattern);\n expression.lastIndex = 0;\n let match = expression.exec(value);\n\n while (match !== null) {\n const before = pattern.before !== undefined || pattern.atBreak === true;\n const after = pattern.after !== undefined;\n const position = match.index + (before ? (match[1]?.length ?? 0) : 0);\n const existing = details.get(position);\n\n if (existing === undefined) {\n positions.push(position);\n details.set(position, { before, after });\n } else {\n if (existing.before && !before) {\n existing.before = false;\n }\n\n if (existing.after && !after) {\n existing.after = false;\n }\n }\n\n match = expression.exec(value);\n }\n }\n\n positions.sort((left, right) => left - right);\n\n let start = info.before.length;\n const end = value.length - info.after.length;\n const result: string[] = [];\n\n for (let index = 0; index < positions.length; index++) {\n const position = positions[index];\n\n if (position < start || position >= end) {\n continue;\n }\n\n const detail = details.get(position);\n\n if (detail === undefined) {\n continue;\n }\n\n const next = details.get(position + 1);\n const previous = details.get(position - 1);\n\n if (\n (positions[index + 1] === position + 1 &&\n detail.after &&\n next?.before === false &&\n next.after === false) ||\n (positions[index - 1] === position - 1 &&\n detail.before &&\n previous?.before === false &&\n previous.after === false)\n ) {\n continue;\n }\n\n if (start !== position) {\n result.push(escapeBackslashes(value.slice(start, position), \"\\\\\"));\n }\n\n start = position;\n const character = value.charAt(position);\n\n if (/[!-/:-@[-`{-~]/u.test(character) && !info.encode?.includes(character)) {\n result.push(\"\\\\\");\n } else {\n result.push(encodeNumericCharacterReference(value.codePointAt(position) ?? 0));\n start += character.length;\n }\n }\n\n result.push(escapeBackslashes(value.slice(start, end), info.after));\n\n return result.join(\"\");\n}\n\nexport function compileUnsafePattern(pattern: FormatUnsafePattern): RegExp {\n const before = `${pattern.atBreak === true ? \"[\\\\r\\\\n][\\\\t ]*\" : \"\"}${pattern.before === undefined ? \"\" : `(?:${pattern.before})`}`;\n\n const escaped = /[|\\\\{}()[\\]^$+*?.]/u.test(pattern.character)\n ? `\\\\${pattern.character}`\n : pattern.character;\n\n return new RegExp(\n `${before.length === 0 ? \"\" : `(${before})`}${escaped}${pattern.after === undefined ? \"\" : `(?:${pattern.after})`}`,\n \"gu\",\n );\n}\n\nfunction patternInScope(stack: readonly FormatConstruct[], pattern: FormatUnsafePattern): boolean {\n return (\n listInScope(stack, pattern.inConstruct, true) &&\n !listInScope(stack, pattern.notInConstruct, false)\n );\n}\n\nfunction listInScope(\n stack: readonly FormatConstruct[],\n value: FormatConstruct | readonly FormatConstruct[] | undefined,\n whenMissing: boolean,\n): boolean {\n if (value === undefined) {\n return whenMissing;\n }\n\n return isString(value)\n ? stack.includes(value)\n : value.some((construct) => stack.includes(construct));\n}\n\nfunction escapeBackslashes(value: string, after: string): string {\n const whole = value + after;\n const positions: number[] = [];\n const expression = /\\\\(?=[!-/:-@[-`{-~])/gu;\n let match = expression.exec(whole);\n\n while (match !== null) {\n positions.push(match.index);\n match = expression.exec(whole);\n }\n\n const results: string[] = [];\n let start = 0;\n\n for (const position of positions) {\n if (position >= value.length) {\n break;\n }\n\n if (start !== position) {\n results.push(value.slice(start, position));\n }\n\n results.push(\"\\\\\");\n start = position;\n }\n\n results.push(value.slice(start));\n\n return results.join(\"\");\n}\n"],"mappings":";;;AAiEA,MAAM,oBAAgD;CACpD;CACA;CACA;CACA;CACA;CACA;AACF;AAEA,MAAa,kBAAkD;CAC7D;EAAE,WAAW;EAAM,OAAO;EAAY,aAAa;CAAW;CAC9D;EAAE,WAAW;EAAM,aAAa;CAAY;CAC5C;EAAE,WAAW;EAAM,aAAa;CAAY;CAC5C;EAAE,WAAW;EAAM,QAAQ;EAAY,aAAa;CAAW;CAC/D;EAAE,WAAW;EAAM,aAAa,CAAC,6BAA6B,qBAAqB;CAAE;CACrF;EACE,WAAW;EACX,aAAa;GACX;GACA;GACA;GACA;GACA;GACA;EACF;CACF;CACA;EACE,WAAW;EACX,aAAa;GACX;GACA;GACA;GACA;GACA;GACA;EACF;CACF;CACA;EAAE,WAAW;EAAK,OAAO;EAAY,aAAa;CAAW;CAC7D;EAAE,WAAW;EAAK,QAAQ;EAAY,aAAa;CAAW;CAC9D;EAAE,WAAW;EAAK,aAAa,CAAC,6BAA6B,qBAAqB;CAAE;CACpF;EAAE,WAAW;EAAK,OAAO;EAAO,aAAa;EAAY,gBAAgB;CAAkB;CAC3F;EAAE,WAAW;EAAK,aAAa;CAAa;CAC5C;EAAE,SAAS;EAAM,WAAW;CAAI;CAChC;EAAE,WAAW;EAAK,aAAa;EAAc,OAAO;CAAiB;CACrE;EAAE,WAAW;EAAK,OAAO;EAAa,aAAa;CAAW;CAC9D;EACE,WAAW;EACX,QAAQ;EACR,OAAO;EACP,aAAa;EACb,gBAAgB;GAAC;GAAY;GAAQ;GAAS;EAAO;CACvD;CACA;EAAE,WAAW;EAAK,aAAa;CAAkB;CACjD;EAAE,WAAW;EAAK,aAAa;CAAiB;CAChD;EAAE,QAAQ;E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,GAAG,OAAO,WAAW,IAAI,KAAK,IAAI,OAAO,KAAK,UAAU,QAAQ,UAAU,KAAA,IAAY,KAAK,MAAM,QAAQ,MAAM,MAC/G,IACF;AACF;AAEA,SAAS,eAAe,OAAmC,SAAuC;CAChG,OACE,YAAY,OAAO,QAAQ,aAAa,IAAI,KAC5C,CAAC,YAAY,OAAO,QAAQ,gBAAgB,KAAK;AAErD;AAEA,SAAS,YACP,OACA,OACA,aACS;CACT,IAAI,UAAU,KAAA,GACZ,OAAO;CAGT,OAAO,SAAS,KAAK,IACjB,MAAM,SAAS,KAAK,IACpB,MAAM,MAAM,cAAc,MAAM,SAAS,SAAS,CAAC;AACzD;AAEA,SAAS,kBAAkB,OAAe,OAAuB;CAC/D,MAAM,QAAQ,QAAQ;CACtB,MAAM,YAAsB,CAAC;CAC7B,MAAM,aAAa;CACnB,IAAI,QAAQ,WAAW,KAAK,KAAK;CAEjC,OAAO,UAAU,MAAM;EACrB,UAAU,KAAK,MAAM,KAAK;EAC1B,QAAQ,WAAW,KAAK,KAAK;CAC/B;CAEA,MAAM,UAAoB,CAAC;CAC3B,IAAI,QAAQ;CAEZ,KAAK,MAAM,YAAY,WAAW;EAChC,IAAI,YAAY,MAAM,QACpB;EAGF,IAAI,UAAU,UACZ,QAAQ,KAAK,MAAM,MAAM,OAAO,QAAQ,CAAC;EAG3C,QAAQ,KAAK,IAAI;EACjB,QAAQ;CACV;CAEA,QAAQ,KAAK,MAAM,MAAM,KAAK,CAAC;CAE/B,OAAO,QAAQ,KAAK,EAAE;AACxB"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { TableCellNode, TableNode, TableRowNode } from "./model.mjs";
|
|
2
|
+
import { FormatContext, FormatInfo } from "./format-context.mjs";
|
|
3
|
+
//#region src/format-table.d.ts
|
|
4
|
+
export declare function formatTable(context: FormatContext, node: TableNode, info: FormatInfo): string;
|
|
5
|
+
export declare function formatTableRow(context: FormatContext, node: TableRowNode, info: FormatInfo): string;
|
|
6
|
+
export declare function formatTableCell(context: FormatContext, node: TableCellNode, info: FormatInfo): string;
|
|
7
|
+
//#endregion
|
|
8
|
+
//# sourceMappingURL=format-table.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"format-table.d.mts","names":[],"sources":["../src/format-table.ts"],"mappings":";;;wBAKgB,YAAY,SAAS,eAAe,MAAM,WAAW,MAAM;wBAyC3D,eACd,SAAS,eACT,MAAM,cACN,MAAM;wBAYQ,gBACd,SAAS,eACT,MAAM,eACN,MAAM"}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
//#region src/format-table.ts
|
|
2
|
+
function formatTable(context, node, info) {
|
|
3
|
+
const columns = node.alignments.length;
|
|
4
|
+
if (columns === 0) return "";
|
|
5
|
+
const exit = context.enter("table");
|
|
6
|
+
const matrix = node.children.map((row) => Array.from({ length: columns }, (_, index) => row.children[index] === void 0 ? "" : formatTableCell(context, row.children[index], info)));
|
|
7
|
+
if (matrix.length === 0) matrix.push(Array.from({ length: columns }, () => ""));
|
|
8
|
+
const measure = context.options.tableStringLength ?? ((value) => value.length);
|
|
9
|
+
const alignPipes = context.options.tablePipeAlign !== false;
|
|
10
|
+
const widths = Array.from({ length: columns }, (_, column) => {
|
|
11
|
+
const content = alignPipes ? Math.max(...matrix.map((row) => measure(row[column] ?? ""))) : 0;
|
|
12
|
+
return Math.max(3, content);
|
|
13
|
+
});
|
|
14
|
+
const delimiter = node.alignments.map((alignment, column) => tableDelimiter(alignment, widths[column]));
|
|
15
|
+
const rows = [
|
|
16
|
+
matrix[0],
|
|
17
|
+
delimiter,
|
|
18
|
+
...matrix.slice(1)
|
|
19
|
+
].map((row) => serializeTableRow(row, widths, context.options.tableCellPadding !== false, measure));
|
|
20
|
+
exit();
|
|
21
|
+
return rows.join("\n");
|
|
22
|
+
}
|
|
23
|
+
function formatTableRow(context, node, info) {
|
|
24
|
+
const values = node.children.map((cell) => formatTableCell(context, cell, info));
|
|
25
|
+
return serializeTableRow(values, values.map((value) => Math.max(3, value.length)), context.options.tableCellPadding !== false, (value) => value.length);
|
|
26
|
+
}
|
|
27
|
+
function formatTableCell(context, node, info) {
|
|
28
|
+
const exitCell = context.enter("tableCell");
|
|
29
|
+
const exitPhrasing = context.enter("phrasing");
|
|
30
|
+
const value = context.phrasing(node.children, {
|
|
31
|
+
...info,
|
|
32
|
+
after: "|",
|
|
33
|
+
before: "|"
|
|
34
|
+
});
|
|
35
|
+
exitPhrasing();
|
|
36
|
+
exitCell();
|
|
37
|
+
return value;
|
|
38
|
+
}
|
|
39
|
+
function tableDelimiter(alignment, width) {
|
|
40
|
+
if (alignment === "center") return `:${"-".repeat(Math.max(1, width - 2))}:`;
|
|
41
|
+
if (alignment === "left") return `:${"-".repeat(Math.max(2, width - 1))}`;
|
|
42
|
+
if (alignment === "right") return `${"-".repeat(Math.max(2, width - 1))}:`;
|
|
43
|
+
return "-".repeat(width);
|
|
44
|
+
}
|
|
45
|
+
function serializeTableRow(values, widths, padding, measure) {
|
|
46
|
+
return `|${widths.map((width, index) => {
|
|
47
|
+
const value = values[index] ?? "";
|
|
48
|
+
const tail = " ".repeat(Math.max(0, width - measure(value)));
|
|
49
|
+
return padding ? ` ${value}${tail} ` : `${value}${tail}`;
|
|
50
|
+
}).join("|")}|`;
|
|
51
|
+
}
|
|
52
|
+
//#endregion
|
|
53
|
+
export { formatTable, formatTableCell, formatTableRow };
|
|
54
|
+
|
|
55
|
+
//# sourceMappingURL=format-table.mjs.map
|