@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 @@
|
|
|
1
|
+
{"version":3,"file":"format-table.mjs","names":[],"sources":["../src/format-table.ts"],"sourcesContent":["import type { FormatContext, FormatInfo } from \"./format-context.ts\";\nimport type { TableCellNode, TableNode, TableRowNode } from \"./model.ts\";\n\n// GFM tables.\n\nexport function formatTable(context: FormatContext, node: TableNode, info: FormatInfo): string {\n const columns = node.alignments.length;\n\n if (columns === 0) {\n return \"\";\n }\n\n const exit = context.enter(\"table\");\n\n const matrix = node.children.map((row) =>\n Array.from({ length: columns }, (_, index) =>\n row.children[index] === undefined ? \"\" : formatTableCell(context, row.children[index], info),\n ),\n );\n\n if (matrix.length === 0) {\n matrix.push(Array.from({ length: columns }, () => \"\"));\n }\n\n const measure = context.options.tableStringLength ?? ((value: string): number => value.length);\n const alignPipes = context.options.tablePipeAlign !== false;\n\n const widths = Array.from({ length: columns }, (_, column) => {\n const content = alignPipes ? Math.max(...matrix.map((row) => measure(row[column] ?? \"\"))) : 0;\n\n return Math.max(3, content);\n });\n\n const delimiter = node.alignments.map((alignment, column) =>\n tableDelimiter(alignment, widths[column]),\n );\n\n const rows = [matrix[0], delimiter, ...matrix.slice(1)].map((row) =>\n serializeTableRow(row, widths, context.options.tableCellPadding !== false, measure),\n );\n\n exit();\n\n return rows.join(\"\\n\");\n}\n\nexport function formatTableRow(\n context: FormatContext,\n node: TableRowNode,\n info: FormatInfo,\n): string {\n const values = node.children.map((cell) => formatTableCell(context, cell, info));\n\n return serializeTableRow(\n values,\n values.map((value) => Math.max(3, value.length)),\n context.options.tableCellPadding !== false,\n (value) => value.length,\n );\n}\n\nexport function formatTableCell(\n context: FormatContext,\n node: TableCellNode,\n info: FormatInfo,\n): string {\n const exitCell = context.enter(\"tableCell\");\n const exitPhrasing = context.enter(\"phrasing\");\n const value = context.phrasing(node.children, { ...info, after: \"|\", before: \"|\" });\n exitPhrasing();\n exitCell();\n\n return value;\n}\n\nfunction tableDelimiter(alignment: TableNode[\"alignments\"][number], width: number): string {\n if (alignment === \"center\") {\n return `:${\"-\".repeat(Math.max(1, width - 2))}:`;\n }\n\n if (alignment === \"left\") {\n return `:${\"-\".repeat(Math.max(2, width - 1))}`;\n }\n\n if (alignment === \"right\") {\n return `${\"-\".repeat(Math.max(2, width - 1))}:`;\n }\n\n return \"-\".repeat(width);\n}\n\nfunction serializeTableRow(\n values: readonly string[],\n widths: readonly number[],\n padding: boolean,\n measure: (value: string) => number,\n): string {\n const cells = widths.map((width, index) => {\n const value = values[index] ?? \"\";\n const tail = \" \".repeat(Math.max(0, width - measure(value)));\n\n return padding ? ` ${value}${tail} ` : `${value}${tail}`;\n });\n\n return `|${cells.join(\"|\")}|`;\n}\n"],"mappings":";AAKA,SAAgB,YAAY,SAAwB,MAAiB,MAA0B;CAC7F,MAAM,UAAU,KAAK,WAAW;CAEhC,IAAI,YAAY,GACd,OAAO;CAGT,MAAM,OAAO,QAAQ,MAAM,OAAO;CAElC,MAAM,SAAS,KAAK,SAAS,KAAK,QAChC,MAAM,KAAK,EAAE,QAAQ,QAAQ,IAAI,GAAG,UAClC,IAAI,SAAS,WAAW,KAAA,IAAY,KAAK,gBAAgB,SAAS,IAAI,SAAS,QAAQ,IAAI,CAC7F,CACF;CAEA,IAAI,OAAO,WAAW,GACpB,OAAO,KAAK,MAAM,KAAK,EAAE,QAAQ,QAAQ,SAAS,EAAE,CAAC;CAGvD,MAAM,UAAU,QAAQ,QAAQ,uBAAuB,UAA0B,MAAM;CACvF,MAAM,aAAa,QAAQ,QAAQ,mBAAmB;CAEtD,MAAM,SAAS,MAAM,KAAK,EAAE,QAAQ,QAAQ,IAAI,GAAG,WAAW;EAC5D,MAAM,UAAU,aAAa,KAAK,IAAI,GAAG,OAAO,KAAK,QAAQ,QAAQ,IAAI,WAAW,EAAE,CAAC,CAAC,IAAI;EAE5F,OAAO,KAAK,IAAI,GAAG,OAAO;CAC5B,CAAC;CAED,MAAM,YAAY,KAAK,WAAW,KAAK,WAAW,WAChD,eAAe,WAAW,OAAO,OAAO,CAC1C;CAEA,MAAM,OAAO;EAAC,OAAO;EAAI;EAAW,GAAG,OAAO,MAAM,CAAC;CAAC,CAAC,CAAC,KAAK,QAC3D,kBAAkB,KAAK,QAAQ,QAAQ,QAAQ,qBAAqB,OAAO,OAAO,CACpF;CAEA,KAAK;CAEL,OAAO,KAAK,KAAK,IAAI;AACvB;AAEA,SAAgB,eACd,SACA,MACA,MACQ;CACR,MAAM,SAAS,KAAK,SAAS,KAAK,SAAS,gBAAgB,SAAS,MAAM,IAAI,CAAC;CAE/E,OAAO,kBACL,QACA,OAAO,KAAK,UAAU,KAAK,IAAI,GAAG,MAAM,MAAM,CAAC,GAC/C,QAAQ,QAAQ,qBAAqB,QACpC,UAAU,MAAM,MACnB;AACF;AAEA,SAAgB,gBACd,SACA,MACA,MACQ;CACR,MAAM,WAAW,QAAQ,MAAM,WAAW;CAC1C,MAAM,eAAe,QAAQ,MAAM,UAAU;CAC7C,MAAM,QAAQ,QAAQ,SAAS,KAAK,UAAU;EAAE,GAAG;EAAM,OAAO;EAAK,QAAQ;CAAI,CAAC;CAClF,aAAa;CACb,SAAS;CAET,OAAO;AACT;AAEA,SAAS,eAAe,WAA4C,OAAuB;CACzF,IAAI,cAAc,UAChB,OAAO,IAAI,IAAI,OAAO,KAAK,IAAI,GAAG,QAAQ,CAAC,CAAC,EAAE;CAGhD,IAAI,cAAc,QAChB,OAAO,IAAI,IAAI,OAAO,KAAK,IAAI,GAAG,QAAQ,CAAC,CAAC;CAG9C,IAAI,cAAc,SAChB,OAAO,GAAG,IAAI,OAAO,KAAK,IAAI,GAAG,QAAQ,CAAC,CAAC,EAAE;CAG/C,OAAO,IAAI,OAAO,KAAK;AACzB;AAEA,SAAS,kBACP,QACA,QACA,SACA,SACQ;CAQR,OAAO,IAPO,OAAO,KAAK,OAAO,UAAU;EACzC,MAAM,QAAQ,OAAO,UAAU;EAC/B,MAAM,OAAO,IAAI,OAAO,KAAK,IAAI,GAAG,QAAQ,QAAQ,KAAK,CAAC,CAAC;EAE3D,OAAO,UAAU,IAAI,QAAQ,KAAK,KAAK,GAAG,QAAQ;CACpD,CAEe,CAAC,CAAC,KAAK,GAAG,EAAE;AAC7B"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
//#region src/format-track.d.ts
|
|
2
|
+
export interface FormatPoint {
|
|
3
|
+
readonly line: number;
|
|
4
|
+
readonly column: number;
|
|
5
|
+
}
|
|
6
|
+
export interface FormatTrackInfo {
|
|
7
|
+
readonly now: FormatPoint;
|
|
8
|
+
readonly lineShift: number;
|
|
9
|
+
}
|
|
10
|
+
export declare class FormatTracker {
|
|
11
|
+
#private;
|
|
12
|
+
constructor(info: FormatTrackInfo);
|
|
13
|
+
current(): FormatTrackInfo;
|
|
14
|
+
move(value: string): string;
|
|
15
|
+
shift(value: number): void;
|
|
16
|
+
}
|
|
17
|
+
//#endregion
|
|
18
|
+
//# sourceMappingURL=format-track.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"format-track.d.mts","names":[],"sources":["../src/format-track.ts"],"mappings":";iBAAiB;WACN;WACA;;iBAGM;WACN,KAAK;WACL;;qBAGE;;EAKX,YAAmB,MAAM;EAMlB,WAAW;EAIX,KAAK;EAUL,MAAM"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
//#region src/format-track.ts
|
|
2
|
+
var FormatTracker = class {
|
|
3
|
+
#line;
|
|
4
|
+
#column;
|
|
5
|
+
#lineShift;
|
|
6
|
+
constructor(info) {
|
|
7
|
+
this.#line = info.now.line;
|
|
8
|
+
this.#column = info.now.column;
|
|
9
|
+
this.#lineShift = info.lineShift;
|
|
10
|
+
}
|
|
11
|
+
current() {
|
|
12
|
+
return {
|
|
13
|
+
now: {
|
|
14
|
+
line: this.#line,
|
|
15
|
+
column: this.#column
|
|
16
|
+
},
|
|
17
|
+
lineShift: this.#lineShift
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
move(value) {
|
|
21
|
+
const lines = value.split(/\r\n?|\n/gu);
|
|
22
|
+
const tail = lines.at(-1) ?? "";
|
|
23
|
+
this.#line += lines.length - 1;
|
|
24
|
+
this.#column = lines.length === 1 ? this.#column + tail.length : 1 + tail.length + this.#lineShift;
|
|
25
|
+
return value;
|
|
26
|
+
}
|
|
27
|
+
shift(value) {
|
|
28
|
+
this.#lineShift += value;
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
//#endregion
|
|
32
|
+
export { FormatTracker };
|
|
33
|
+
|
|
34
|
+
//# sourceMappingURL=format-track.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"format-track.mjs","names":[],"sources":["../src/format-track.ts"],"sourcesContent":["export interface FormatPoint {\n readonly line: number;\n readonly column: number;\n}\n\nexport interface FormatTrackInfo {\n readonly now: FormatPoint;\n readonly lineShift: number;\n}\n\nexport class FormatTracker {\n #line: number;\n #column: number;\n #lineShift: number;\n\n public constructor(info: FormatTrackInfo) {\n this.#line = info.now.line;\n this.#column = info.now.column;\n this.#lineShift = info.lineShift;\n }\n\n public current(): FormatTrackInfo {\n return { now: { line: this.#line, column: this.#column }, lineShift: this.#lineShift };\n }\n\n public move(value: string): string {\n const lines = value.split(/\\r\\n?|\\n/gu);\n const tail = lines.at(-1) ?? \"\";\n this.#line += lines.length - 1;\n this.#column =\n lines.length === 1 ? this.#column + tail.length : 1 + tail.length + this.#lineShift;\n\n return value;\n }\n\n public shift(value: number): void {\n this.#lineShift += value;\n }\n}\n"],"mappings":";AAUA,IAAa,gBAAb,MAA2B;CACzB;CACA;CACA;CAEA,YAAmB,MAAuB;EACxC,KAAK,QAAQ,KAAK,IAAI;EACtB,KAAK,UAAU,KAAK,IAAI;EACxB,KAAK,aAAa,KAAK;CACzB;CAEA,UAAkC;EAChC,OAAO;GAAE,KAAK;IAAE,MAAM,KAAK;IAAO,QAAQ,KAAK;GAAQ;GAAG,WAAW,KAAK;EAAW;CACvF;CAEA,KAAY,OAAuB;EACjC,MAAM,QAAQ,MAAM,MAAM,YAAY;EACtC,MAAM,OAAO,MAAM,GAAG,EAAE,KAAK;EAC7B,KAAK,SAAS,MAAM,SAAS;EAC7B,KAAK,UACH,MAAM,WAAW,IAAI,KAAK,UAAU,KAAK,SAAS,IAAI,KAAK,SAAS,KAAK;EAE3E,OAAO;CACT;CAEA,MAAa,OAAqB;EAChC,KAAK,cAAc;CACrB;AACF"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { MdxNode } from "./model.mjs";
|
|
2
|
+
import { FormatOptions } from "./format-context.mjs";
|
|
3
|
+
//#region src/format.d.ts
|
|
4
|
+
/** Formats a node as canonical MDX for the enabled syntax. */
|
|
5
|
+
export declare function formatMdx(node: MdxNode, options?: FormatOptions): string;
|
|
6
|
+
//#endregion
|
|
7
|
+
//# sourceMappingURL=format.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"format.d.mts","names":[],"sources":["../src/format.ts"],"mappings":";;;;wBAuDgB,UAAU,MAAM,SAAS,UAAS"}
|
package/dist/format.mjs
ADDED
|
@@ -0,0 +1,549 @@
|
|
|
1
|
+
import { encodeNumericCharacterReference } from "./character-reference.mjs";
|
|
2
|
+
import { indentLines, longestRun, rootInfo } from "./format-context.mjs";
|
|
3
|
+
import { FormatTracker } from "./format-track.mjs";
|
|
4
|
+
import { normalizeIdentifier } from "./identifier.mjs";
|
|
5
|
+
import { mdxChildren } from "./model.mjs";
|
|
6
|
+
import { containsDirective, formatContainerDirective, formatDirective, formatDirectiveLabel } from "./format-directive.mjs";
|
|
7
|
+
import { formatJsxFlowElement, formatJsxTextElement, indentExpression } from "./format-jsx.mjs";
|
|
8
|
+
import { formatDisplayMath, formatInlineMath, mathFormats } from "./format-math.mjs";
|
|
9
|
+
import { UNSAFE_PATTERNS, compileUnsafePattern, safeSource } from "./format-safe.mjs";
|
|
10
|
+
import { formatTable, formatTableCell, formatTableRow } from "./format-table.mjs";
|
|
11
|
+
import { frontmatterFence, resolveSyntaxOptions } from "./syntax-options.mjs";
|
|
12
|
+
//#region src/format.ts
|
|
13
|
+
/** Formats a node as canonical MDX for the enabled syntax. */
|
|
14
|
+
function formatMdx(node, options = {}) {
|
|
15
|
+
return new Formatter(options, containsDirective(node), mathFormats(node)).format(node, rootInfo);
|
|
16
|
+
}
|
|
17
|
+
var Formatter = class {
|
|
18
|
+
stack = [];
|
|
19
|
+
unsafe;
|
|
20
|
+
compiledPatterns = /* @__PURE__ */ new Map();
|
|
21
|
+
options;
|
|
22
|
+
syntax;
|
|
23
|
+
bulletLastUsed;
|
|
24
|
+
constructor(options, containsDirectives, nodeMathFormats) {
|
|
25
|
+
this.options = { ...options };
|
|
26
|
+
this.syntax = resolveSyntaxOptions(options.syntax);
|
|
27
|
+
this.unsafe = [
|
|
28
|
+
...UNSAFE_PATTERNS,
|
|
29
|
+
{
|
|
30
|
+
character: "{",
|
|
31
|
+
inConstruct: "phrasing"
|
|
32
|
+
},
|
|
33
|
+
{
|
|
34
|
+
atBreak: true,
|
|
35
|
+
character: "{"
|
|
36
|
+
},
|
|
37
|
+
{
|
|
38
|
+
character: "<",
|
|
39
|
+
inConstruct: "phrasing"
|
|
40
|
+
},
|
|
41
|
+
{
|
|
42
|
+
atBreak: true,
|
|
43
|
+
character: "<"
|
|
44
|
+
},
|
|
45
|
+
...containsDirectives || this.syntax.directives ? [{
|
|
46
|
+
before: "[^:]",
|
|
47
|
+
character: ":",
|
|
48
|
+
after: "[A-Za-z]",
|
|
49
|
+
inConstruct: "phrasing"
|
|
50
|
+
}, {
|
|
51
|
+
atBreak: true,
|
|
52
|
+
character: ":",
|
|
53
|
+
after: ":"
|
|
54
|
+
}] : [],
|
|
55
|
+
...nodeMathFormats.has("dollar") || this.syntax.math.includes("dollar") ? [
|
|
56
|
+
{
|
|
57
|
+
character: "$",
|
|
58
|
+
inConstruct: "phrasing"
|
|
59
|
+
},
|
|
60
|
+
{
|
|
61
|
+
character: "$",
|
|
62
|
+
inConstruct: "mathFlowMeta"
|
|
63
|
+
},
|
|
64
|
+
{
|
|
65
|
+
atBreak: true,
|
|
66
|
+
character: "$",
|
|
67
|
+
after: "\\$"
|
|
68
|
+
}
|
|
69
|
+
] : [],
|
|
70
|
+
...this.syntax.frontmatter.map((format) => ({
|
|
71
|
+
atBreak: true,
|
|
72
|
+
character: frontmatterFence(format).charAt(0),
|
|
73
|
+
after: escapeRegularExpression(frontmatterFence(format).slice(1))
|
|
74
|
+
}))
|
|
75
|
+
];
|
|
76
|
+
}
|
|
77
|
+
compilePattern(pattern) {
|
|
78
|
+
const cached = this.compiledPatterns.get(pattern);
|
|
79
|
+
if (cached !== void 0) return cached;
|
|
80
|
+
const expression = compileUnsafePattern(pattern);
|
|
81
|
+
this.compiledPatterns.set(pattern, expression);
|
|
82
|
+
return expression;
|
|
83
|
+
}
|
|
84
|
+
format(node, info) {
|
|
85
|
+
switch (node.kind) {
|
|
86
|
+
case "document": return this.flow(node.children, info);
|
|
87
|
+
case "paragraph": return this.paragraph(node, info);
|
|
88
|
+
case "heading": return this.heading(node, info);
|
|
89
|
+
case "thematicBreak": return this.thematicBreak();
|
|
90
|
+
case "codeBlock": return this.codeBlock(node, info);
|
|
91
|
+
case "blockquote": return this.blockQuote(node, info);
|
|
92
|
+
case "list": return this.list(node, info);
|
|
93
|
+
case "listItem": return this.flow(node.children, info, node.spread);
|
|
94
|
+
case "table": return formatTable(this, node, info);
|
|
95
|
+
case "tableRow": return formatTableRow(this, node, info);
|
|
96
|
+
case "tableCell": return formatTableCell(this, node, info);
|
|
97
|
+
case "definition": return this.definition(node, info);
|
|
98
|
+
case "footnoteDefinition": return this.footnoteDefinition(node, info);
|
|
99
|
+
case "frontmatter": return `${frontmatterFence(node.format)}${node.value.length === 0 ? "" : `\n${node.value}`}\n${frontmatterFence(node.format)}`;
|
|
100
|
+
case "directiveLabel": return formatDirectiveLabel(this, node, info);
|
|
101
|
+
case "textDirective": return formatDirective(this, node, info, ":");
|
|
102
|
+
case "leafDirective": return formatDirective(this, node, info, "::");
|
|
103
|
+
case "containerDirective": return formatContainerDirective(this, node, info);
|
|
104
|
+
case "displayMath": return formatDisplayMath(this, node, info);
|
|
105
|
+
case "rawHtmlBlock":
|
|
106
|
+
case "rawHtmlInline": return node.value;
|
|
107
|
+
case "text": return this.safe(node.value, info);
|
|
108
|
+
case "emphasis": return this.attention(node.children, info, this.attentionMarker(this.options.emphasis ?? "*", false), false);
|
|
109
|
+
case "strong": return this.attention(node.children, info, this.attentionMarker(this.options.strong ?? "*", true), true);
|
|
110
|
+
case "delete": return this.strikethrough(node.children, info);
|
|
111
|
+
case "inlineCode": return this.stack.includes("tableCell") ? formatInlineCode(node.value).replaceAll("|", "\\|") : formatInlineCode(node.value);
|
|
112
|
+
case "inlineMath": return formatInlineMath(this, node);
|
|
113
|
+
case "hardBreak": return "\\\n";
|
|
114
|
+
case "link": return this.link(node, info);
|
|
115
|
+
case "image": return this.image(node, info);
|
|
116
|
+
case "linkReference": return this.linkReference(node, info);
|
|
117
|
+
case "imageReference": return this.imageReference(node, info);
|
|
118
|
+
case "footnoteReference": return this.footnoteReference(node);
|
|
119
|
+
case "inlineExpression":
|
|
120
|
+
case "blockExpression": return `{${indentExpression(node.code)}}`;
|
|
121
|
+
case "esm": return node.code;
|
|
122
|
+
case "jsxTextElement": return formatJsxTextElement(this, node, info);
|
|
123
|
+
case "jsxFlowElement": return formatJsxFlowElement(this, node, info);
|
|
124
|
+
default: return node;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
flow(nodes, info, spread) {
|
|
128
|
+
const tracker = new FormatTracker(info);
|
|
129
|
+
const output = [];
|
|
130
|
+
for (let index = 0; index < nodes.length; index++) {
|
|
131
|
+
const node = nodes[index];
|
|
132
|
+
output.push(tracker.move(this.format(node, {
|
|
133
|
+
after: "\n",
|
|
134
|
+
before: "\n",
|
|
135
|
+
...tracker.current()
|
|
136
|
+
})));
|
|
137
|
+
const next = nodes[index + 1];
|
|
138
|
+
if (next !== void 0) output.push(tracker.move(this.join(node, next, spread)));
|
|
139
|
+
if (node.kind !== "list") this.bulletLastUsed = void 0;
|
|
140
|
+
}
|
|
141
|
+
return output.join("");
|
|
142
|
+
}
|
|
143
|
+
join(left, right, spread) {
|
|
144
|
+
if (this.options.tightDefinitions === true && left.kind === "definition" && right.kind === "definition") return "\n";
|
|
145
|
+
if (spread !== void 0) return spread ? "\n\n" : "\n";
|
|
146
|
+
return "\n\n";
|
|
147
|
+
}
|
|
148
|
+
paragraph(node, info) {
|
|
149
|
+
const exit = this.enter("paragraph");
|
|
150
|
+
const exitPhrasing = this.enter("phrasing");
|
|
151
|
+
const value = this.phrasing(node.children, info);
|
|
152
|
+
exitPhrasing();
|
|
153
|
+
exit();
|
|
154
|
+
return value;
|
|
155
|
+
}
|
|
156
|
+
phrasing(nodes, info) {
|
|
157
|
+
let tracker = new FormatTracker(info);
|
|
158
|
+
const output = [];
|
|
159
|
+
let before = info.before;
|
|
160
|
+
for (let index = 0; index < nodes.length; index++) {
|
|
161
|
+
const node = nodes[index];
|
|
162
|
+
let nextIndex = index + 1;
|
|
163
|
+
let text = node.kind === "text" ? node.value : void 0;
|
|
164
|
+
let nextText = nodes.at(nextIndex);
|
|
165
|
+
while (text !== void 0 && nextText?.kind === "text") {
|
|
166
|
+
text += nextText.value;
|
|
167
|
+
nextIndex++;
|
|
168
|
+
nextText = nodes.at(nextIndex);
|
|
169
|
+
}
|
|
170
|
+
const next = nodes[nextIndex];
|
|
171
|
+
const after = next === void 0 ? info.after : peek(next, this.options);
|
|
172
|
+
if (output.length > 0 && (before === "\r" || before === "\n") && node.kind === "rawHtmlInline") {
|
|
173
|
+
output[output.length - 1] = output[output.length - 1].replace(/(?:\r\n?|\n)$/u, (ending) => encodeLineEnding(ending));
|
|
174
|
+
before = ";";
|
|
175
|
+
tracker = new FormatTracker(info);
|
|
176
|
+
tracker.move(output.join(""));
|
|
177
|
+
}
|
|
178
|
+
let value = text === void 0 ? this.format(node, {
|
|
179
|
+
after,
|
|
180
|
+
before,
|
|
181
|
+
...tracker.current()
|
|
182
|
+
}) : encodeBlankLineEndings(this.safe(text, {
|
|
183
|
+
after,
|
|
184
|
+
before,
|
|
185
|
+
...tracker.current()
|
|
186
|
+
}));
|
|
187
|
+
if (text !== void 0 && after === "[" && value.endsWith("!") && !value.endsWith("\\!")) value = `${value.slice(0, -1)}\\!`;
|
|
188
|
+
tracker.move(value);
|
|
189
|
+
output.push(value);
|
|
190
|
+
before = value.slice(-1);
|
|
191
|
+
index = nextIndex - 1;
|
|
192
|
+
}
|
|
193
|
+
return output.join("");
|
|
194
|
+
}
|
|
195
|
+
heading(node, info) {
|
|
196
|
+
if (node.depth < 3 && node.children.some(containsLineEnding)) {
|
|
197
|
+
const exit = this.enter("headingSetext");
|
|
198
|
+
const exitPhrasing = this.enter("phrasing");
|
|
199
|
+
const content = this.phrasing(node.children, {
|
|
200
|
+
...info,
|
|
201
|
+
after: "\n",
|
|
202
|
+
before: "\n"
|
|
203
|
+
});
|
|
204
|
+
exitPhrasing();
|
|
205
|
+
exit();
|
|
206
|
+
const lastLineStart = Math.max(content.lastIndexOf("\r"), content.lastIndexOf("\n")) + 1;
|
|
207
|
+
return `${content}\n${(node.depth === 1 ? "=" : "-").repeat(Math.max(1, content.length - lastLineStart))}`;
|
|
208
|
+
}
|
|
209
|
+
const sequence = "#".repeat(node.depth);
|
|
210
|
+
const exit = this.enter("headingAtx");
|
|
211
|
+
const exitPhrasing = this.enter("phrasing");
|
|
212
|
+
const tracker = new FormatTracker(info);
|
|
213
|
+
tracker.move(`${sequence} `);
|
|
214
|
+
let content = this.phrasing(node.children, {
|
|
215
|
+
after: "\n",
|
|
216
|
+
before: "# ",
|
|
217
|
+
...tracker.current()
|
|
218
|
+
});
|
|
219
|
+
if (/^[\t ]/u.test(content)) content = encodeNumericCharacterReference(content.codePointAt(0) ?? 32) + content.slice(1);
|
|
220
|
+
if (content.endsWith("#")) content = `${content.slice(0, -1)}${encodeNumericCharacterReference(35)}`;
|
|
221
|
+
exitPhrasing();
|
|
222
|
+
exit();
|
|
223
|
+
if (content.length === 0) return sequence;
|
|
224
|
+
return `${sequence} ${content}${this.options.closeAtx === true ? ` ${sequence}` : ""}`;
|
|
225
|
+
}
|
|
226
|
+
thematicBreak() {
|
|
227
|
+
const marker = this.options.rule ?? "-";
|
|
228
|
+
const repetition = Math.max(3, this.options.ruleRepetition ?? 3);
|
|
229
|
+
const value = `${marker}${this.options.ruleSpaces === true ? " " : ""}`.repeat(repetition);
|
|
230
|
+
const rule = this.options.ruleSpaces === true ? value.slice(0, -1) : value;
|
|
231
|
+
const frontmatterFences = new Set(this.syntax.frontmatter.map(frontmatterFence));
|
|
232
|
+
if (!frontmatterFences.has(rule)) return rule;
|
|
233
|
+
let separator = " ";
|
|
234
|
+
let safeRule = Array.from({ length: repetition }, () => marker).join(separator);
|
|
235
|
+
while (frontmatterFences.has(safeRule)) {
|
|
236
|
+
separator += " ";
|
|
237
|
+
safeRule = Array.from({ length: repetition }, () => marker).join(separator);
|
|
238
|
+
}
|
|
239
|
+
return safeRule;
|
|
240
|
+
}
|
|
241
|
+
codeBlock(node, info) {
|
|
242
|
+
const marker = this.options.fence ?? "`";
|
|
243
|
+
const suffix = marker === "`" ? "GraveAccent" : "Tilde";
|
|
244
|
+
const sequence = marker.repeat(Math.max(3, longestRun(node.value, marker) + 1));
|
|
245
|
+
const tracker = new FormatTracker(info);
|
|
246
|
+
let value = tracker.move(sequence);
|
|
247
|
+
if (node.language !== void 0 && node.language.length > 0) {
|
|
248
|
+
const exitLanguage = this.enter(`codeFencedLang${suffix}`);
|
|
249
|
+
value += tracker.move(this.safe(node.language, {
|
|
250
|
+
after: " ",
|
|
251
|
+
before: value,
|
|
252
|
+
encode: ["`"]
|
|
253
|
+
}));
|
|
254
|
+
exitLanguage();
|
|
255
|
+
}
|
|
256
|
+
if (node.meta !== void 0 && node.meta.length > 0) {
|
|
257
|
+
const exitMeta = this.enter(`codeFencedMeta${suffix}`);
|
|
258
|
+
value += tracker.move(` ${this.safe(node.meta, {
|
|
259
|
+
after: "\n",
|
|
260
|
+
before: value,
|
|
261
|
+
encode: ["`"]
|
|
262
|
+
})}`);
|
|
263
|
+
exitMeta();
|
|
264
|
+
}
|
|
265
|
+
value += tracker.move("\n");
|
|
266
|
+
if (node.value.length > 0) value += tracker.move(`${node.value}\n`);
|
|
267
|
+
return value + sequence;
|
|
268
|
+
}
|
|
269
|
+
blockQuote(node, info) {
|
|
270
|
+
const exit = this.enter("blockquote");
|
|
271
|
+
const tracker = new FormatTracker(info);
|
|
272
|
+
tracker.move("> ");
|
|
273
|
+
tracker.shift(2);
|
|
274
|
+
const value = indentLines(this.flow(node.children, {
|
|
275
|
+
after: "\n",
|
|
276
|
+
before: "\n",
|
|
277
|
+
...tracker.current()
|
|
278
|
+
}), (line, blank) => `>${blank ? "" : " "}${line}`);
|
|
279
|
+
exit();
|
|
280
|
+
return value;
|
|
281
|
+
}
|
|
282
|
+
list(node, info) {
|
|
283
|
+
const exit = this.enter("list");
|
|
284
|
+
let marker = node.ordered ? this.options.bulletOrdered ?? "." : this.options.bullet ?? "-";
|
|
285
|
+
if (this.bulletLastUsed === marker || !node.ordered && marker === (this.options.rule ?? "-") && node.children.some((item) => item.children[0]?.kind === "thematicBreak")) marker = alternateListMarker(marker);
|
|
286
|
+
const values = node.children.map((item, index) => this.listItem(item, node, index, marker, info));
|
|
287
|
+
this.bulletLastUsed = marker;
|
|
288
|
+
exit();
|
|
289
|
+
return values.join(node.spread ? "\n\n" : "\n");
|
|
290
|
+
}
|
|
291
|
+
listItem(item, parent, index, listMarker, info) {
|
|
292
|
+
const marker = parent.ordered ? `${parent.start + (this.options.incrementListMarker === false ? 0 : index)}${listMarker}` : listMarker;
|
|
293
|
+
let size = marker.length + 1;
|
|
294
|
+
const indentation = this.options.listItemIndent ?? "one";
|
|
295
|
+
if (indentation === "tab" || indentation === "mixed" && (parent.spread || item.spread)) size = Math.ceil(size / 4) * 4;
|
|
296
|
+
const prefix = `${marker}${" ".repeat(size - marker.length)}`;
|
|
297
|
+
const tracker = new FormatTracker(info);
|
|
298
|
+
tracker.move(prefix);
|
|
299
|
+
tracker.shift(size);
|
|
300
|
+
const exit = this.enter("listItem");
|
|
301
|
+
let body = this.flow(item.children, {
|
|
302
|
+
after: "\n",
|
|
303
|
+
before: "\n",
|
|
304
|
+
...tracker.current()
|
|
305
|
+
}, parent.spread || item.spread);
|
|
306
|
+
if (item.checked !== void 0 && item.children[0]?.kind === "paragraph") body = `[${item.checked ? "x" : " "}] ${body}`;
|
|
307
|
+
const value = indentLines(body, (line, blank, lineIndex) => lineIndex === 0 ? `${blank ? marker : prefix}${line}` : `${blank ? "" : " ".repeat(size)}${line}`);
|
|
308
|
+
exit();
|
|
309
|
+
return value;
|
|
310
|
+
}
|
|
311
|
+
definition(node, info) {
|
|
312
|
+
const exit = this.enter("definition");
|
|
313
|
+
const tracker = new FormatTracker(info);
|
|
314
|
+
const exitLabel = this.enter("label");
|
|
315
|
+
let value = tracker.move("[");
|
|
316
|
+
value += tracker.move(formatAssociationIdentifier(node.identifier));
|
|
317
|
+
value += tracker.move("]: ");
|
|
318
|
+
exitLabel();
|
|
319
|
+
value += tracker.move(this.destination(node.url, value, node.title === void 0 ? "\n" : " "));
|
|
320
|
+
if (node.title !== void 0) value += tracker.move(this.title(node.title, value));
|
|
321
|
+
exit();
|
|
322
|
+
return value;
|
|
323
|
+
}
|
|
324
|
+
footnoteDefinition(node, info) {
|
|
325
|
+
const exit = this.enter("footnoteDefinition");
|
|
326
|
+
const tracker = new FormatTracker(info);
|
|
327
|
+
const exitLabel = this.enter("label");
|
|
328
|
+
let value = tracker.move("[^");
|
|
329
|
+
value += tracker.move(this.safe(node.label, {
|
|
330
|
+
before: value,
|
|
331
|
+
after: "]"
|
|
332
|
+
}));
|
|
333
|
+
value += tracker.move("]:");
|
|
334
|
+
exitLabel();
|
|
335
|
+
if (node.children.length > 0) {
|
|
336
|
+
tracker.shift(4);
|
|
337
|
+
const content = this.flow(node.children, {
|
|
338
|
+
after: "\n",
|
|
339
|
+
before: "\n",
|
|
340
|
+
...tracker.current()
|
|
341
|
+
});
|
|
342
|
+
const blankFirst = this.options.footnoteFirstLineBlank === true;
|
|
343
|
+
value += (blankFirst ? "\n" : " ") + indentLines(content, (line, blank, lineIndex) => !blankFirst && lineIndex === 0 ? line : `${blank ? "" : " "}${line}`);
|
|
344
|
+
}
|
|
345
|
+
exit();
|
|
346
|
+
return value;
|
|
347
|
+
}
|
|
348
|
+
footnoteReference(node) {
|
|
349
|
+
const exit = this.enter("footnoteReference");
|
|
350
|
+
const exitReference = this.enter("reference");
|
|
351
|
+
const value = `[^${this.safe(node.label, {
|
|
352
|
+
before: "[^",
|
|
353
|
+
after: "]"
|
|
354
|
+
})}]`;
|
|
355
|
+
exitReference();
|
|
356
|
+
exit();
|
|
357
|
+
return value;
|
|
358
|
+
}
|
|
359
|
+
attention(children, info, marker, strong) {
|
|
360
|
+
const sequence = strong ? marker + marker : marker;
|
|
361
|
+
const exit = this.enter(strong ? "strong" : "emphasis");
|
|
362
|
+
const tracker = new FormatTracker(info);
|
|
363
|
+
const before = tracker.move(sequence);
|
|
364
|
+
const content = tracker.move(this.phrasing(children, {
|
|
365
|
+
after: marker,
|
|
366
|
+
before,
|
|
367
|
+
...tracker.current()
|
|
368
|
+
}));
|
|
369
|
+
exit();
|
|
370
|
+
return before + content + sequence;
|
|
371
|
+
}
|
|
372
|
+
attentionMarker(marker, strong) {
|
|
373
|
+
return this.stack.includes(strong ? "strong" : "emphasis") ? marker === "*" ? "_" : "*" : marker;
|
|
374
|
+
}
|
|
375
|
+
strikethrough(children, info) {
|
|
376
|
+
const exit = this.enter("strikethrough");
|
|
377
|
+
const tracker = new FormatTracker(info);
|
|
378
|
+
const before = tracker.move("~~");
|
|
379
|
+
const content = tracker.move(this.phrasing(children, {
|
|
380
|
+
after: "~",
|
|
381
|
+
before,
|
|
382
|
+
...tracker.current()
|
|
383
|
+
}));
|
|
384
|
+
exit();
|
|
385
|
+
return before + content + "~~";
|
|
386
|
+
}
|
|
387
|
+
link(node, info) {
|
|
388
|
+
const exit = this.enter("link");
|
|
389
|
+
const tracker = new FormatTracker(info);
|
|
390
|
+
const exitLabel = this.enter("label");
|
|
391
|
+
let value = tracker.move("[");
|
|
392
|
+
value += tracker.move(this.phrasing(node.children, {
|
|
393
|
+
after: "]",
|
|
394
|
+
before: value,
|
|
395
|
+
...tracker.current()
|
|
396
|
+
}));
|
|
397
|
+
value += tracker.move("](");
|
|
398
|
+
exitLabel();
|
|
399
|
+
value += tracker.move(this.destination(node.url, value, node.title === void 0 ? ")" : " "));
|
|
400
|
+
if (node.title !== void 0) value += tracker.move(this.title(node.title, value));
|
|
401
|
+
exit();
|
|
402
|
+
return value + ")";
|
|
403
|
+
}
|
|
404
|
+
image(node, info) {
|
|
405
|
+
const exit = this.enter("image");
|
|
406
|
+
const tracker = new FormatTracker(info);
|
|
407
|
+
const exitLabel = this.enter("label");
|
|
408
|
+
let value = tracker.move("![");
|
|
409
|
+
value += tracker.move(this.safe(node.alt, {
|
|
410
|
+
before: value,
|
|
411
|
+
after: "]"
|
|
412
|
+
}));
|
|
413
|
+
value += tracker.move("](");
|
|
414
|
+
exitLabel();
|
|
415
|
+
value += tracker.move(this.destination(node.url, value, node.title === void 0 ? ")" : " "));
|
|
416
|
+
if (node.title !== void 0) value += tracker.move(this.title(node.title, value));
|
|
417
|
+
exit();
|
|
418
|
+
return value + ")";
|
|
419
|
+
}
|
|
420
|
+
linkReference(node, info) {
|
|
421
|
+
const exit = this.enter("linkReference");
|
|
422
|
+
const exitLabel = this.enter("label");
|
|
423
|
+
const label = this.phrasing(node.children, {
|
|
424
|
+
...info,
|
|
425
|
+
before: "[",
|
|
426
|
+
after: "]"
|
|
427
|
+
});
|
|
428
|
+
exitLabel();
|
|
429
|
+
const value = `[${label}]${this.referenceSuffix(node.referenceKind, node.identifier, label)}`;
|
|
430
|
+
exit();
|
|
431
|
+
return value;
|
|
432
|
+
}
|
|
433
|
+
imageReference(node, _info) {
|
|
434
|
+
const exit = this.enter("imageReference");
|
|
435
|
+
const exitLabel = this.enter("label");
|
|
436
|
+
const alt = this.safe(node.alt, {
|
|
437
|
+
before: "![",
|
|
438
|
+
after: "]"
|
|
439
|
+
});
|
|
440
|
+
exitLabel();
|
|
441
|
+
const value = `![${alt}]${this.referenceSuffix(node.referenceKind, node.identifier, alt)}`;
|
|
442
|
+
exit();
|
|
443
|
+
return value;
|
|
444
|
+
}
|
|
445
|
+
referenceSuffix(kind, identifier, label) {
|
|
446
|
+
const reference = this.referenceIdentifier(identifier);
|
|
447
|
+
if (label.length > 0 && normalizeIdentifier(label) === normalizeIdentifier(reference)) {
|
|
448
|
+
if (kind === "shortcut") return "";
|
|
449
|
+
if (kind === "collapsed") return "[]";
|
|
450
|
+
}
|
|
451
|
+
return `[${reference}]`;
|
|
452
|
+
}
|
|
453
|
+
referenceIdentifier(identifier) {
|
|
454
|
+
return formatAssociationIdentifier(identifier);
|
|
455
|
+
}
|
|
456
|
+
destination(value, before, after) {
|
|
457
|
+
if (value.length === 0 || /[\0- \u007F]/u.test(value)) {
|
|
458
|
+
const exit = this.enter("destinationLiteral");
|
|
459
|
+
const result = `<${this.safe(value, {
|
|
460
|
+
before: `${before}<`,
|
|
461
|
+
after: ">"
|
|
462
|
+
})}>`;
|
|
463
|
+
exit();
|
|
464
|
+
return result;
|
|
465
|
+
}
|
|
466
|
+
const exit = this.enter("destinationRaw");
|
|
467
|
+
const result = this.safe(value, {
|
|
468
|
+
before,
|
|
469
|
+
after
|
|
470
|
+
});
|
|
471
|
+
exit();
|
|
472
|
+
return result;
|
|
473
|
+
}
|
|
474
|
+
title(value, before) {
|
|
475
|
+
const quote = this.options.quote ?? "\"";
|
|
476
|
+
const exit = this.enter(quote === "\"" ? "titleQuote" : "titleApostrophe");
|
|
477
|
+
const result = ` ${quote}${this.safe(value, {
|
|
478
|
+
before: `${before} ${quote}`,
|
|
479
|
+
after: quote
|
|
480
|
+
})}${quote}`;
|
|
481
|
+
exit();
|
|
482
|
+
return result;
|
|
483
|
+
}
|
|
484
|
+
safe(value, info) {
|
|
485
|
+
return safeSource(this, value, info);
|
|
486
|
+
}
|
|
487
|
+
enter(construct) {
|
|
488
|
+
this.stack.push(construct);
|
|
489
|
+
return () => {
|
|
490
|
+
this.stack.pop();
|
|
491
|
+
};
|
|
492
|
+
}
|
|
493
|
+
};
|
|
494
|
+
function peek(node, options) {
|
|
495
|
+
switch (node.kind) {
|
|
496
|
+
case "textDirective": return ":";
|
|
497
|
+
case "inlineMath": return node.format === "dollar" ? "$" : "\\";
|
|
498
|
+
case "image":
|
|
499
|
+
case "imageReference": return "!";
|
|
500
|
+
case "footnoteReference":
|
|
501
|
+
case "link":
|
|
502
|
+
case "linkReference": return "[";
|
|
503
|
+
case "emphasis": return options.emphasis ?? "*";
|
|
504
|
+
case "strong": return options.strong ?? "*";
|
|
505
|
+
case "delete": return "~";
|
|
506
|
+
case "inlineCode": return "`";
|
|
507
|
+
case "rawHtmlInline":
|
|
508
|
+
case "jsxTextElement": return "<";
|
|
509
|
+
case "hardBreak": return "\\";
|
|
510
|
+
case "inlineExpression": return "{";
|
|
511
|
+
case "text": return node.value.charAt(0);
|
|
512
|
+
default: return "";
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
function formatInlineCode(input) {
|
|
516
|
+
const value = input.replace(/\r\n?|\n/gu, " ");
|
|
517
|
+
let sequence = "`";
|
|
518
|
+
while (new RegExp(`(^|[^\`])${sequence}([^\`]|$)`, "u").test(value)) sequence += "`";
|
|
519
|
+
const padded = /[^ \r\n]/u.test(value) && (/^[ \r\n]/u.test(value) && /[ \r\n]$/u.test(value) || /^`|`$/u.test(value)) ? ` ${value} ` : value;
|
|
520
|
+
return `${sequence}${padded}${sequence}`;
|
|
521
|
+
}
|
|
522
|
+
function alternateListMarker(marker) {
|
|
523
|
+
if (marker === ".") return ")";
|
|
524
|
+
if (marker === ")") return ".";
|
|
525
|
+
return marker === "*" ? "-" : "*";
|
|
526
|
+
}
|
|
527
|
+
function containsLineEnding(node) {
|
|
528
|
+
if (node.kind === "hardBreak") return true;
|
|
529
|
+
if ((node.kind === "text" || node.kind === "rawHtmlInline") && /\r\n?|\n/u.test(node.value)) return true;
|
|
530
|
+
return mdxChildren(node).some(containsLineEnding);
|
|
531
|
+
}
|
|
532
|
+
function encodeBlankLineEndings(value) {
|
|
533
|
+
return value.replace(/(?:(?:\r\n?|\n)){2,}/gu, encodeLineEnding);
|
|
534
|
+
}
|
|
535
|
+
function encodeLineEnding(value) {
|
|
536
|
+
let result = "";
|
|
537
|
+
for (const character of value) result += encodeNumericCharacterReference(character.codePointAt(0) ?? 0);
|
|
538
|
+
return result;
|
|
539
|
+
}
|
|
540
|
+
function formatAssociationIdentifier(value) {
|
|
541
|
+
return value.replace(/[!-/:-@[-`{-~]/gu, "\\$&");
|
|
542
|
+
}
|
|
543
|
+
function escapeRegularExpression(value) {
|
|
544
|
+
return value.replace(/[|\\{}()[\]^$+*?.]/gu, "\\$&");
|
|
545
|
+
}
|
|
546
|
+
//#endregion
|
|
547
|
+
export { formatMdx };
|
|
548
|
+
|
|
549
|
+
//# sourceMappingURL=format.mjs.map
|