mrkdwn.js 4.0.6 → 6.6.2

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.
Files changed (118) hide show
  1. package/README.md +85 -66
  2. package/dist/ast/ast.d.cts +1 -1
  3. package/dist/ast/ast.d.ts +1 -1
  4. package/dist/{ast-8XCbjRQT.d.cts → ast-CNfTgS0N.d.cts} +35 -35
  5. package/dist/{ast-8XCbjRQT.d.ts → ast-CNfTgS0N.d.ts} +35 -35
  6. package/dist/block/block.cjs +12 -4
  7. package/dist/block/block.d.cts +9 -5
  8. package/dist/block/block.d.ts +9 -5
  9. package/dist/block/block.js +8 -4
  10. package/dist/block/definitions.d.cts +2 -2
  11. package/dist/block/definitions.d.ts +2 -2
  12. package/dist/block/node.d.cts +4 -4
  13. package/dist/block/node.d.ts +4 -4
  14. package/dist/block/table.d.cts +1 -1
  15. package/dist/block/table.d.ts +1 -1
  16. package/dist/codec.cjs +1 -1
  17. package/dist/codec.d.cts +1631 -29
  18. package/dist/codec.d.ts +1631 -29
  19. package/dist/codec.js +2 -2
  20. package/dist/defaults/defaults.d.cts +1 -1
  21. package/dist/defaults/defaults.d.ts +1 -1
  22. package/dist/diagnostics/diagnostics.cjs +17 -7
  23. package/dist/diagnostics/diagnostics.d.cts +2 -2
  24. package/dist/diagnostics/diagnostics.d.ts +2 -2
  25. package/dist/diagnostics/diagnostics.js +17 -8
  26. package/dist/{diagnostics-BWK1iGy7.d.cts → diagnostics-CjfBoW4K.d.cts} +14 -10
  27. package/dist/{diagnostics-BWK1iGy7.d.ts → diagnostics-CjfBoW4K.d.ts} +14 -10
  28. package/dist/emit/emit.cjs +378 -42
  29. package/dist/emit/emit.js +381 -45
  30. package/dist/emit/front-matter.cjs +22 -5
  31. package/dist/emit/front-matter.js +22 -5
  32. package/dist/emit/html-table.cjs +104 -0
  33. package/dist/emit/html-table.d.cts +7 -0
  34. package/dist/emit/html-table.d.ts +7 -0
  35. package/dist/emit/html-table.js +102 -0
  36. package/dist/emit/inline.cjs +69 -26
  37. package/dist/emit/inline.d.cts +2 -12
  38. package/dist/emit/inline.d.ts +2 -12
  39. package/dist/emit/inline.js +68 -27
  40. package/dist/emit/table.cjs +18 -11
  41. package/dist/emit/table.d.cts +3 -2
  42. package/dist/emit/table.d.ts +3 -2
  43. package/dist/emit/table.js +18 -11
  44. package/dist/html/html-table.cjs +243 -0
  45. package/dist/html/html-table.d.cts +5 -0
  46. package/dist/html/html-table.d.ts +5 -0
  47. package/dist/html/html-table.js +242 -0
  48. package/dist/html/render.d.cts +1 -1
  49. package/dist/html/render.d.ts +1 -1
  50. package/dist/image/image.cjs +4 -4
  51. package/dist/image/image.d.cts +1 -1
  52. package/dist/image/image.d.ts +1 -1
  53. package/dist/image/image.js +4 -4
  54. package/dist/{image-C4KYmz_L.d.cts → image-B5L0HpAJ.d.cts} +1 -1
  55. package/dist/{image-DescdRgq.d.cts → image-BtmTG4_a.d.cts} +1 -1
  56. package/dist/{image-DescdRgq.d.ts → image-BtmTG4_a.d.ts} +1 -1
  57. package/dist/{image-Cm3hT5PS.d.ts → image-C390OIB3.d.ts} +1 -1
  58. package/dist/index.cjs +2 -2
  59. package/dist/index.d.cts +4 -4
  60. package/dist/index.d.ts +4 -4
  61. package/dist/index.js +3 -3
  62. package/dist/inline/delimiter.d.cts +2 -2
  63. package/dist/inline/delimiter.d.ts +2 -2
  64. package/dist/inline/inline.d.cts +1 -1
  65. package/dist/inline/inline.d.ts +1 -1
  66. package/dist/inline/link.d.cts +17 -1
  67. package/dist/inline/link.d.ts +17 -1
  68. package/dist/inline/node.d.cts +2 -2
  69. package/dist/inline/node.d.ts +2 -2
  70. package/dist/{inline-CXVQWQnW.d.cts → inline-CnO86zMw.d.cts} +2 -2
  71. package/dist/inline-DCIKQ83j.d.cts +14 -0
  72. package/dist/{inline-B_V7bs5j.d.ts → inline-DItZ-VVw.d.ts} +2 -2
  73. package/dist/inline-INoDAG37.d.ts +14 -0
  74. package/dist/lower/front-matter.cjs +73 -26
  75. package/dist/lower/front-matter.d.cts +2 -1
  76. package/dist/lower/front-matter.d.ts +2 -1
  77. package/dist/lower/front-matter.js +72 -25
  78. package/dist/lower/image.d.cts +1 -1
  79. package/dist/lower/image.d.ts +1 -1
  80. package/dist/lower/inline.cjs +87 -33
  81. package/dist/lower/inline.d.cts +9 -12
  82. package/dist/lower/inline.d.ts +9 -12
  83. package/dist/lower/inline.js +89 -34
  84. package/dist/lower/lower.cjs +143 -113
  85. package/dist/lower/lower.d.cts +8 -1
  86. package/dist/lower/lower.d.ts +8 -1
  87. package/dist/lower/lower.js +144 -115
  88. package/dist/lower/table.cjs +3 -2
  89. package/dist/lower/table.d.cts +1 -1
  90. package/dist/lower/table.d.ts +1 -1
  91. package/dist/lower/table.js +3 -2
  92. package/dist/options/options.d.cts +10 -10
  93. package/dist/options/options.d.ts +10 -10
  94. package/dist/read.cjs +45 -9
  95. package/dist/read.d.cts +3 -3
  96. package/dist/read.d.ts +3 -3
  97. package/dist/read.js +47 -11
  98. package/dist/scan/entity-table.cjs +2125 -2125
  99. package/dist/scan/entity-table.js +2125 -2125
  100. package/dist/shared/line-ending.cjs +7 -0
  101. package/dist/shared/line-ending.d.cts +5 -0
  102. package/dist/shared/line-ending.d.ts +5 -0
  103. package/dist/shared/line-ending.js +5 -0
  104. package/dist/shared/list-id.cjs +11 -3
  105. package/dist/shared/list-id.d.cts +7 -4
  106. package/dist/shared/list-id.d.ts +7 -4
  107. package/dist/shared/list-id.js +11 -4
  108. package/dist/shared/style-constants.cjs +0 -2
  109. package/dist/shared/style-constants.d.cts +1 -2
  110. package/dist/shared/style-constants.d.ts +1 -2
  111. package/dist/shared/style-constants.js +1 -2
  112. package/dist/write.cjs +33 -6
  113. package/dist/write.d.cts +2 -2
  114. package/dist/write.d.ts +2 -2
  115. package/dist/write.js +34 -7
  116. package/package.json +29 -30
  117. package/dist/link-Dv4kxVjk.d.cts +0 -18
  118. package/dist/link-Dv4kxVjk.d.ts +0 -18
@@ -0,0 +1,104 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ const require_diagnostics_diagnostics = require("../diagnostics/diagnostics.cjs");
3
+ require("../shared/style-constants.cjs");
4
+ let document_schema_js = require("document-schema.js");
5
+ //#region src/emit/html-table.ts
6
+ function escapeHtmlText(text) {
7
+ return text.replaceAll("&", "&amp;").replaceAll("<", "&lt;").replaceAll(">", "&gt;");
8
+ }
9
+ function escapeHtmlAttribute(text) {
10
+ return escapeHtmlText(text).replaceAll("\"", "&quot;");
11
+ }
12
+ function blockNeedsHtmlFallback(block) {
13
+ return block.kind !== "paragraph" && block.kind !== "image";
14
+ }
15
+ function tableNeedsHtmlFallback(table) {
16
+ return table.rows.some((row) => row.cells.some((cell) => cell.colSpan !== void 0 || cell.rowSpan !== void 0 || cell.background !== void 0 || cell.blocks.some(blockNeedsHtmlFallback)));
17
+ }
18
+ function emitRunHtml(run) {
19
+ let text = escapeHtmlText(run.text);
20
+ if (run.fontFamily === "Courier New") text = `<code>${text}</code>`;
21
+ if (run.strike === true) text = `<del>${text}</del>`;
22
+ if (run.italic === true) text = `<em>${text}</em>`;
23
+ if (run.bold === true) text = `<strong>${text}</strong>`;
24
+ if (run.hyperlink !== void 0) text = `<a href="${escapeHtmlAttribute(run.hyperlink)}">${text}</a>`;
25
+ return text;
26
+ }
27
+ function emitHtmlImage(block, embedImages) {
28
+ const alt = escapeHtmlAttribute(block.altText ?? "");
29
+ if (!embedImages) return `<img alt="${alt}">`;
30
+ return `<img src="data:image/${block.format};base64,${block.base64}" alt="${alt}">`;
31
+ }
32
+ function backgroundStyleAttr(fill, sink) {
33
+ if (fill.kind === "solid") return `background-color:#${(0, document_schema_js.colorToRgbHex)(fill.color)}`;
34
+ sink({
35
+ code: require_diagnostics_diagnostics.MarkdownDiagnosticCodes.TABLE_CELL_FORMATTING_DROPPED,
36
+ severity: "info",
37
+ message: "a table cell's own pattern background fill has no CSS equivalent this package's bounded HTML-table writer attempts (only a solid fill maps onto a plain background-color); the cell still renders as an ordinary unstyled cell"
38
+ });
39
+ }
40
+ function textAlignStyleAttr(cell) {
41
+ const first = cell.blocks[0];
42
+ if (first?.kind !== "paragraph" || first.alignment === void 0) return;
43
+ return `text-align:${first.alignment}`;
44
+ }
45
+ function emitCellHtml(cell, context) {
46
+ const soleBlock = cell.blocks.length === 1 ? cell.blocks[0] : void 0;
47
+ if (soleBlock?.kind === "table") return emitHtmlTable(soleBlock, context);
48
+ if (cell.blocks.length > 1) context.sink({
49
+ code: require_diagnostics_diagnostics.MarkdownDiagnosticCodes.TABLE_CELL_MULTI_PARAGRAPH_JOINED,
50
+ severity: "info",
51
+ message: `a table cell with ${String(cell.blocks.length)} blocks has no multi-block equivalent in an HTML table cell either; their own rendered content is joined with a literal <br> line break`
52
+ });
53
+ const parts = [];
54
+ for (const block of cell.blocks) {
55
+ if (block.kind === "paragraph") {
56
+ parts.push(block.runs.map(emitRunHtml).join(""));
57
+ continue;
58
+ }
59
+ if (block.kind === "image") {
60
+ parts.push(emitHtmlImage(block, context.embedImages));
61
+ continue;
62
+ }
63
+ if (block.kind === "table") {
64
+ context.sink({
65
+ code: require_diagnostics_diagnostics.MarkdownDiagnosticCodes.TABLE_CELL_FORMATTING_DROPPED,
66
+ severity: "info",
67
+ message: "a nested table mixed with other content in the same cell has no HTML representation this package attempts (a cell's own nested table must be its entire content); it is dropped, the cell's other content still renders"
68
+ });
69
+ continue;
70
+ }
71
+ context.sink({
72
+ code: require_diagnostics_diagnostics.MarkdownDiagnosticCodes.TABLE_CELL_FORMATTING_DROPPED,
73
+ severity: "info",
74
+ message: `a table cell containing a "${block.kind}" block has no HTML-table equivalent this package attempts; it is dropped entirely`
75
+ });
76
+ }
77
+ return parts.join("<br>");
78
+ }
79
+ function emitCellTag(cell, isHeader, context) {
80
+ const tag = isHeader ? "th" : "td";
81
+ const attrs = [];
82
+ if (cell.colSpan !== void 0) attrs.push(`colspan="${String(cell.colSpan)}"`);
83
+ if (cell.rowSpan !== void 0) attrs.push(`rowspan="${String(cell.rowSpan)}"`);
84
+ const styleParts = [];
85
+ if (cell.background !== void 0) {
86
+ const style = backgroundStyleAttr(cell.background, context.sink);
87
+ if (style !== void 0) styleParts.push(style);
88
+ }
89
+ const textAlign = textAlignStyleAttr(cell);
90
+ if (textAlign !== void 0) styleParts.push(textAlign);
91
+ if (styleParts.length > 0) attrs.push(`style="${escapeHtmlAttribute(styleParts.join(";"))}"`);
92
+ return `<${tag}${attrs.length > 0 ? ` ${attrs.join(" ")}` : ""}>${emitCellHtml(cell, context)}</${tag}>`;
93
+ }
94
+ function emitHtmlTable(table, context) {
95
+ const [header, ...body] = table.rows;
96
+ const lines = ["<table>"];
97
+ if (header !== void 0) lines.push(`<tr>${header.cells.map((cell) => emitCellTag(cell, true, context)).join("")}</tr>`);
98
+ for (const row of body) lines.push(`<tr>${row.cells.map((cell) => emitCellTag(cell, false, context)).join("")}</tr>`);
99
+ lines.push("</table>");
100
+ return lines.join("\n");
101
+ }
102
+ //#endregion
103
+ exports.emitHtmlTable = emitHtmlTable;
104
+ exports.tableNeedsHtmlFallback = tableNeedsHtmlFallback;
@@ -0,0 +1,7 @@
1
+ import { TableEmitContext } from "./table.cjs";
2
+ import { ContentTable } from "document-schema.js";
3
+ //#region src/emit/html-table.d.ts
4
+ declare function tableNeedsHtmlFallback(table: ContentTable): boolean;
5
+ declare function emitHtmlTable(table: ContentTable, context: TableEmitContext): string;
6
+ //#endregion
7
+ export { emitHtmlTable, tableNeedsHtmlFallback };
@@ -0,0 +1,7 @@
1
+ import { TableEmitContext } from "./table.js";
2
+ import { ContentTable } from "document-schema.js";
3
+ //#region src/emit/html-table.d.ts
4
+ declare function tableNeedsHtmlFallback(table: ContentTable): boolean;
5
+ declare function emitHtmlTable(table: ContentTable, context: TableEmitContext): string;
6
+ //#endregion
7
+ export { emitHtmlTable, tableNeedsHtmlFallback };
@@ -0,0 +1,102 @@
1
+ import { MarkdownDiagnosticCodes } from "../diagnostics/diagnostics.js";
2
+ import "../shared/style-constants.js";
3
+ import { colorToRgbHex } from "document-schema.js";
4
+ //#region src/emit/html-table.ts
5
+ function escapeHtmlText(text) {
6
+ return text.replaceAll("&", "&amp;").replaceAll("<", "&lt;").replaceAll(">", "&gt;");
7
+ }
8
+ function escapeHtmlAttribute(text) {
9
+ return escapeHtmlText(text).replaceAll("\"", "&quot;");
10
+ }
11
+ function blockNeedsHtmlFallback(block) {
12
+ return block.kind !== "paragraph" && block.kind !== "image";
13
+ }
14
+ function tableNeedsHtmlFallback(table) {
15
+ return table.rows.some((row) => row.cells.some((cell) => cell.colSpan !== void 0 || cell.rowSpan !== void 0 || cell.background !== void 0 || cell.blocks.some(blockNeedsHtmlFallback)));
16
+ }
17
+ function emitRunHtml(run) {
18
+ let text = escapeHtmlText(run.text);
19
+ if (run.fontFamily === "Courier New") text = `<code>${text}</code>`;
20
+ if (run.strike === true) text = `<del>${text}</del>`;
21
+ if (run.italic === true) text = `<em>${text}</em>`;
22
+ if (run.bold === true) text = `<strong>${text}</strong>`;
23
+ if (run.hyperlink !== void 0) text = `<a href="${escapeHtmlAttribute(run.hyperlink)}">${text}</a>`;
24
+ return text;
25
+ }
26
+ function emitHtmlImage(block, embedImages) {
27
+ const alt = escapeHtmlAttribute(block.altText ?? "");
28
+ if (!embedImages) return `<img alt="${alt}">`;
29
+ return `<img src="data:image/${block.format};base64,${block.base64}" alt="${alt}">`;
30
+ }
31
+ function backgroundStyleAttr(fill, sink) {
32
+ if (fill.kind === "solid") return `background-color:#${colorToRgbHex(fill.color)}`;
33
+ sink({
34
+ code: MarkdownDiagnosticCodes.TABLE_CELL_FORMATTING_DROPPED,
35
+ severity: "info",
36
+ message: "a table cell's own pattern background fill has no CSS equivalent this package's bounded HTML-table writer attempts (only a solid fill maps onto a plain background-color); the cell still renders as an ordinary unstyled cell"
37
+ });
38
+ }
39
+ function textAlignStyleAttr(cell) {
40
+ const first = cell.blocks[0];
41
+ if (first?.kind !== "paragraph" || first.alignment === void 0) return;
42
+ return `text-align:${first.alignment}`;
43
+ }
44
+ function emitCellHtml(cell, context) {
45
+ const soleBlock = cell.blocks.length === 1 ? cell.blocks[0] : void 0;
46
+ if (soleBlock?.kind === "table") return emitHtmlTable(soleBlock, context);
47
+ if (cell.blocks.length > 1) context.sink({
48
+ code: MarkdownDiagnosticCodes.TABLE_CELL_MULTI_PARAGRAPH_JOINED,
49
+ severity: "info",
50
+ message: `a table cell with ${String(cell.blocks.length)} blocks has no multi-block equivalent in an HTML table cell either; their own rendered content is joined with a literal <br> line break`
51
+ });
52
+ const parts = [];
53
+ for (const block of cell.blocks) {
54
+ if (block.kind === "paragraph") {
55
+ parts.push(block.runs.map(emitRunHtml).join(""));
56
+ continue;
57
+ }
58
+ if (block.kind === "image") {
59
+ parts.push(emitHtmlImage(block, context.embedImages));
60
+ continue;
61
+ }
62
+ if (block.kind === "table") {
63
+ context.sink({
64
+ code: MarkdownDiagnosticCodes.TABLE_CELL_FORMATTING_DROPPED,
65
+ severity: "info",
66
+ message: "a nested table mixed with other content in the same cell has no HTML representation this package attempts (a cell's own nested table must be its entire content); it is dropped, the cell's other content still renders"
67
+ });
68
+ continue;
69
+ }
70
+ context.sink({
71
+ code: MarkdownDiagnosticCodes.TABLE_CELL_FORMATTING_DROPPED,
72
+ severity: "info",
73
+ message: `a table cell containing a "${block.kind}" block has no HTML-table equivalent this package attempts; it is dropped entirely`
74
+ });
75
+ }
76
+ return parts.join("<br>");
77
+ }
78
+ function emitCellTag(cell, isHeader, context) {
79
+ const tag = isHeader ? "th" : "td";
80
+ const attrs = [];
81
+ if (cell.colSpan !== void 0) attrs.push(`colspan="${String(cell.colSpan)}"`);
82
+ if (cell.rowSpan !== void 0) attrs.push(`rowspan="${String(cell.rowSpan)}"`);
83
+ const styleParts = [];
84
+ if (cell.background !== void 0) {
85
+ const style = backgroundStyleAttr(cell.background, context.sink);
86
+ if (style !== void 0) styleParts.push(style);
87
+ }
88
+ const textAlign = textAlignStyleAttr(cell);
89
+ if (textAlign !== void 0) styleParts.push(textAlign);
90
+ if (styleParts.length > 0) attrs.push(`style="${escapeHtmlAttribute(styleParts.join(";"))}"`);
91
+ return `<${tag}${attrs.length > 0 ? ` ${attrs.join(" ")}` : ""}>${emitCellHtml(cell, context)}</${tag}>`;
92
+ }
93
+ function emitHtmlTable(table, context) {
94
+ const [header, ...body] = table.rows;
95
+ const lines = ["<table>"];
96
+ if (header !== void 0) lines.push(`<tr>${header.cells.map((cell) => emitCellTag(cell, true, context)).join("")}</tr>`);
97
+ for (const row of body) lines.push(`<tr>${row.cells.map((cell) => emitCellTag(cell, false, context)).join("")}</tr>`);
98
+ lines.push("</table>");
99
+ return lines.join("\n");
100
+ }
101
+ //#endregion
102
+ export { emitHtmlTable, tableNeedsHtmlFallback };
@@ -1,6 +1,7 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ const require_inline_footnote = require("../inline/footnote.cjs");
2
3
  const require_diagnostics_diagnostics = require("../diagnostics/diagnostics.cjs");
3
- const require_html_html = require("../html/html.cjs");
4
+ const require_shared_line_ending = require("../shared/line-ending.cjs");
4
5
  require("../shared/style-constants.cjs");
5
6
  //#region src/emit/inline.ts
6
7
  const ESCAPE_CHARS = /* @__PURE__ */ new Set([
@@ -40,17 +41,9 @@ function escapeMarkdownText(text) {
40
41
  let index = 0;
41
42
  while (index < text.length) {
42
43
  const char = text.charAt(index);
43
- if (char === "<") {
44
- const tag = require_html_html.matchHtmlTag(text, index);
45
- if (tag !== void 0) {
46
- out += tag;
47
- index += tag.length;
48
- continue;
49
- }
50
- }
51
- if (char === "\n") {
44
+ if (char === "\n" || char === "\r") {
52
45
  out += "\\\n";
53
- index += 1;
46
+ index += char === "\r" && text.charAt(index + 1) === "\n" ? 2 : 1;
54
47
  continue;
55
48
  }
56
49
  if (ESCAPE_CHARS.has(char)) {
@@ -76,7 +69,11 @@ function renderCodeSpan(text) {
76
69
  const wouldBeStrippedOnReparse = !isAllSpaces && text.startsWith(" ") && text.endsWith(" ");
77
70
  return risksFenceCollision || wouldBeStrippedOnReparse ? `${fence} ${text} ${fence}` : `${fence}${text}${fence}`;
78
71
  }
79
- function renderLeaf(run, context) {
72
+ function footnoteReferenceAt(constructs, index) {
73
+ for (const extent of constructs ?? []) if (extent.descriptor.kind === "anchor" && extent.descriptor.anchorType === "footnote" && extent.startRun === index && extent.endRun === index) return extent.descriptor;
74
+ }
75
+ function renderLeaf(run, context, index, constructs) {
76
+ if (run.source?.format === "markdown") return run.source.xml;
80
77
  if (run.fontFamily === "Courier New") {
81
78
  context.sink({
82
79
  code: require_diagnostics_diagnostics.MarkdownDiagnosticCodes.CODE_SPAN_AS_MONOSPACE_RUN,
@@ -85,7 +82,13 @@ function renderLeaf(run, context) {
85
82
  });
86
83
  return renderCodeSpan(run.text);
87
84
  }
88
- if (run.fontFamily === "Footnote Reference") return run.text;
85
+ const footnote = footnoteReferenceAt(constructs, index);
86
+ if (footnote !== void 0 && require_inline_footnote.isValidFootnoteLabel(footnote.name)) return `[^${footnote.name}]`;
87
+ if (footnote !== void 0) context.sink({
88
+ code: require_diagnostics_diagnostics.MarkdownDiagnosticCodes.CONSTRUCT_UNREPRESENTED,
89
+ severity: "info",
90
+ message: `a footnote reference anchor's own name "${footnote.name}" cannot be spelled as a "[^label]" marker (whitespace or "]" would reparse as something else); the run's own text renders escaped in its place, but the reference itself is not represented`
91
+ });
89
92
  if (run.fontFamily === "Cambria Math") return `\\(${run.text}\\)`;
90
93
  return escapeMarkdownText(run.text);
91
94
  }
@@ -118,9 +121,33 @@ function wrapForStyle(body, key, context, precedingText) {
118
121
  const delimiter = key === "bold" ? marker.repeat(2) : marker;
119
122
  return `${delimiter}${body}${delimiter}`;
120
123
  }
121
- function renderNestedStyles(runs, depth, context) {
122
- if (depth >= STYLE_KEYS.length) return runs.map((run) => renderLeaf(run, context)).join("");
123
- const key = STYLE_KEYS[depth];
124
+ function groupCount(runs, key) {
125
+ let count = 0;
126
+ let index = 0;
127
+ while (index < runs.length) {
128
+ const active = styleActive(runs[index], key);
129
+ count += 1;
130
+ index += 1;
131
+ while (index < runs.length && styleActive(runs[index], key) === active) index += 1;
132
+ }
133
+ return count;
134
+ }
135
+ function pickSplitKey(runs, remaining) {
136
+ let best = remaining[0];
137
+ let bestCount = groupCount(runs, best);
138
+ for (const key of remaining.slice(1)) {
139
+ const count = groupCount(runs, key);
140
+ if (count < bestCount) {
141
+ best = key;
142
+ bestCount = count;
143
+ }
144
+ }
145
+ return best;
146
+ }
147
+ function renderNestedStyles(runs, remainingKeys, context, base, constructs) {
148
+ if (remainingKeys.length === 0) return runs.map((run, local) => renderLeaf(run, context, base + local, constructs)).join("");
149
+ const key = pickSplitKey(runs, remainingKeys);
150
+ const rest = remainingKeys.filter((candidate) => candidate !== key);
124
151
  let out = "";
125
152
  let index = 0;
126
153
  while (index < runs.length) {
@@ -129,21 +156,34 @@ function renderNestedStyles(runs, depth, context) {
129
156
  const active = styleActive(current, key);
130
157
  let end = index + 1;
131
158
  while (end < runs.length && styleActive(runs[end], key) === active) end += 1;
132
- const inner = renderNestedStyles(runs.slice(index, end), depth + 1, context);
159
+ const inner = renderNestedStyles(runs.slice(index, end), rest, context, base + index, constructs);
133
160
  out += active ? wrapForStyle(inner, key, context, out) : inner;
134
161
  index = end;
135
162
  }
136
163
  return out;
137
164
  }
138
- function isPlainAutolink(run) {
139
- if (run.hyperlink === void 0 || run.hyperlink.length === 0 || run.bold === true || run.italic === true || run.strike === true || run.fontFamily === "Courier New" || run.fontFamily === "Cambria Math" || run.fontFamily === "Footnote Reference") return false;
165
+ function isPlainAutolink(run, footnoteReference) {
166
+ if (run.hyperlink === void 0 || run.hyperlink.length === 0 || run.bold === true || run.italic === true || run.strike === true || run.fontFamily === "Courier New" || run.fontFamily === "Cambria Math" || footnoteReference !== void 0) return false;
140
167
  return run.text === run.hyperlink || run.hyperlink === `mailto:${run.text}`;
141
168
  }
142
169
  function escapeLinkDestination(destination) {
143
170
  if (!/[\s()]/.test(destination)) return destination;
144
171
  return `<${destination.replace(/[<>]/g, (char) => `\\${char}`)}>`;
145
172
  }
146
- function emitRuns(runs, context) {
173
+ function renderLinkTitle(title) {
174
+ return title.replace(/[\n\r]+/g, " ").replace(/["\\]/g, (char) => `\\${char}`);
175
+ }
176
+ function linkTitleCovering(constructs, start, end) {
177
+ let tightest;
178
+ for (const extent of constructs ?? []) {
179
+ if (extent.descriptor.kind !== "link" || extent.descriptor.title === void 0) continue;
180
+ if (extent.startRun > start || extent.endRun < end) continue;
181
+ const current = tightest;
182
+ if (current === void 0 || extent.startRun > current.startRun || extent.startRun === current.startRun && extent.endRun < current.endRun) tightest = extent;
183
+ }
184
+ return tightest?.descriptor.kind === "link" ? tightest.descriptor.title : void 0;
185
+ }
186
+ function emitRuns(runs, context, constructs) {
147
187
  let out = "";
148
188
  let index = 0;
149
189
  while (index < runs.length) {
@@ -152,7 +192,7 @@ function emitRuns(runs, context) {
152
192
  if (run.hyperlink === void 0) {
153
193
  let end = index + 1;
154
194
  while (end < runs.length && runs[end]?.hyperlink === void 0) end += 1;
155
- out += renderNestedStyles(runs.slice(index, end), 0, context);
195
+ out += renderNestedStyles(runs.slice(index, end), STYLE_KEYS, context, index, constructs);
156
196
  index = end;
157
197
  continue;
158
198
  }
@@ -165,19 +205,22 @@ function emitRuns(runs, context) {
165
205
  severity: "info",
166
206
  message: `${String(group.length)} adjacent runs share the hyperlink "${hyperlink}"; markdown has no way to place two link boundaries back to back, so they render as one link spanning their combined text`
167
207
  });
168
- if (group.length === 1 && isPlainAutolink(group[0])) out += `<${group[0].text}>`;
208
+ const title = linkTitleCovering(constructs, index, groupEnd);
209
+ if (title === void 0 && group.length === 1 && isPlainAutolink(group[0], footnoteReferenceAt(constructs, index))) out += `<${group[0].text}>`;
169
210
  else {
170
- const linkText = renderNestedStyles(group, 0, context);
171
- out += `[${linkText}](${escapeLinkDestination(hyperlink)})`;
211
+ const linkText = renderNestedStyles(group, STYLE_KEYS, context, index, constructs);
212
+ out += `[${linkText}](${escapeLinkDestination(hyperlink)}${title === void 0 ? "" : ` "${renderLinkTitle(title)}"`})`;
172
213
  }
173
214
  index = groupEnd;
174
215
  }
175
216
  return out;
176
217
  }
177
- function emitRunsSingleLine(runs, context) {
178
- return emitRuns(runs, context).replace(/\\\n/g, " ");
218
+ function emitRunsSingleLine(runs, context, constructs) {
219
+ return emitRuns(runs, context, constructs).replace(require_shared_line_ending.ESCAPED_HARD_BREAK_PATTERN, " ").split(require_shared_line_ending.LINE_ENDING_PATTERN).join(" ");
179
220
  }
180
221
  //#endregion
181
222
  exports.emitRuns = emitRuns;
182
223
  exports.emitRunsSingleLine = emitRunsSingleLine;
224
+ exports.escapeLinkDestination = escapeLinkDestination;
183
225
  exports.escapeMarkdownText = escapeMarkdownText;
226
+ exports.renderLinkTitle = renderLinkTitle;
@@ -1,12 +1,2 @@
1
- import { i as MarkdownDiagnosticSink } from "../diagnostics-BWK1iGy7.cjs";
2
- import { ContentRun } from "document-schema.js";
3
- //#region src/emit/inline.d.ts
4
- interface InlineEmitContext {
5
- readonly sink: MarkdownDiagnosticSink;
6
- readonly emphasisMarker: string;
7
- }
8
- declare function escapeMarkdownText(text: string): string;
9
- declare function emitRuns(runs: readonly ContentRun[], context: InlineEmitContext): string;
10
- declare function emitRunsSingleLine(runs: readonly ContentRun[], context: InlineEmitContext): string;
11
- //#endregion
12
- export { InlineEmitContext, emitRuns, emitRunsSingleLine, escapeMarkdownText };
1
+ import { a as escapeMarkdownText, i as escapeLinkDestination, n as emitRuns, o as renderLinkTitle, r as emitRunsSingleLine, t as InlineEmitContext } from "../inline-DCIKQ83j.cjs";
2
+ export { InlineEmitContext, emitRuns, emitRunsSingleLine, escapeLinkDestination, escapeMarkdownText, renderLinkTitle };
@@ -1,12 +1,2 @@
1
- import { i as MarkdownDiagnosticSink } from "../diagnostics-BWK1iGy7.js";
2
- import { ContentRun } from "document-schema.js";
3
- //#region src/emit/inline.d.ts
4
- interface InlineEmitContext {
5
- readonly sink: MarkdownDiagnosticSink;
6
- readonly emphasisMarker: string;
7
- }
8
- declare function escapeMarkdownText(text: string): string;
9
- declare function emitRuns(runs: readonly ContentRun[], context: InlineEmitContext): string;
10
- declare function emitRunsSingleLine(runs: readonly ContentRun[], context: InlineEmitContext): string;
11
- //#endregion
12
- export { InlineEmitContext, emitRuns, emitRunsSingleLine, escapeMarkdownText };
1
+ import { a as escapeMarkdownText, i as escapeLinkDestination, n as emitRuns, o as renderLinkTitle, r as emitRunsSingleLine, t as InlineEmitContext } from "../inline-INoDAG37.js";
2
+ export { InlineEmitContext, emitRuns, emitRunsSingleLine, escapeLinkDestination, escapeMarkdownText, renderLinkTitle };
@@ -1,5 +1,6 @@
1
+ import { isValidFootnoteLabel } from "../inline/footnote.js";
1
2
  import { MarkdownDiagnosticCodes } from "../diagnostics/diagnostics.js";
2
- import { matchHtmlTag } from "../html/html.js";
3
+ import { ESCAPED_HARD_BREAK_PATTERN, LINE_ENDING_PATTERN } from "../shared/line-ending.js";
3
4
  import "../shared/style-constants.js";
4
5
  //#region src/emit/inline.ts
5
6
  const ESCAPE_CHARS = /* @__PURE__ */ new Set([
@@ -39,17 +40,9 @@ function escapeMarkdownText(text) {
39
40
  let index = 0;
40
41
  while (index < text.length) {
41
42
  const char = text.charAt(index);
42
- if (char === "<") {
43
- const tag = matchHtmlTag(text, index);
44
- if (tag !== void 0) {
45
- out += tag;
46
- index += tag.length;
47
- continue;
48
- }
49
- }
50
- if (char === "\n") {
43
+ if (char === "\n" || char === "\r") {
51
44
  out += "\\\n";
52
- index += 1;
45
+ index += char === "\r" && text.charAt(index + 1) === "\n" ? 2 : 1;
53
46
  continue;
54
47
  }
55
48
  if (ESCAPE_CHARS.has(char)) {
@@ -75,7 +68,11 @@ function renderCodeSpan(text) {
75
68
  const wouldBeStrippedOnReparse = !isAllSpaces && text.startsWith(" ") && text.endsWith(" ");
76
69
  return risksFenceCollision || wouldBeStrippedOnReparse ? `${fence} ${text} ${fence}` : `${fence}${text}${fence}`;
77
70
  }
78
- function renderLeaf(run, context) {
71
+ function footnoteReferenceAt(constructs, index) {
72
+ for (const extent of constructs ?? []) if (extent.descriptor.kind === "anchor" && extent.descriptor.anchorType === "footnote" && extent.startRun === index && extent.endRun === index) return extent.descriptor;
73
+ }
74
+ function renderLeaf(run, context, index, constructs) {
75
+ if (run.source?.format === "markdown") return run.source.xml;
79
76
  if (run.fontFamily === "Courier New") {
80
77
  context.sink({
81
78
  code: MarkdownDiagnosticCodes.CODE_SPAN_AS_MONOSPACE_RUN,
@@ -84,7 +81,13 @@ function renderLeaf(run, context) {
84
81
  });
85
82
  return renderCodeSpan(run.text);
86
83
  }
87
- if (run.fontFamily === "Footnote Reference") return run.text;
84
+ const footnote = footnoteReferenceAt(constructs, index);
85
+ if (footnote !== void 0 && isValidFootnoteLabel(footnote.name)) return `[^${footnote.name}]`;
86
+ if (footnote !== void 0) context.sink({
87
+ code: MarkdownDiagnosticCodes.CONSTRUCT_UNREPRESENTED,
88
+ severity: "info",
89
+ message: `a footnote reference anchor's own name "${footnote.name}" cannot be spelled as a "[^label]" marker (whitespace or "]" would reparse as something else); the run's own text renders escaped in its place, but the reference itself is not represented`
90
+ });
88
91
  if (run.fontFamily === "Cambria Math") return `\\(${run.text}\\)`;
89
92
  return escapeMarkdownText(run.text);
90
93
  }
@@ -117,9 +120,33 @@ function wrapForStyle(body, key, context, precedingText) {
117
120
  const delimiter = key === "bold" ? marker.repeat(2) : marker;
118
121
  return `${delimiter}${body}${delimiter}`;
119
122
  }
120
- function renderNestedStyles(runs, depth, context) {
121
- if (depth >= STYLE_KEYS.length) return runs.map((run) => renderLeaf(run, context)).join("");
122
- const key = STYLE_KEYS[depth];
123
+ function groupCount(runs, key) {
124
+ let count = 0;
125
+ let index = 0;
126
+ while (index < runs.length) {
127
+ const active = styleActive(runs[index], key);
128
+ count += 1;
129
+ index += 1;
130
+ while (index < runs.length && styleActive(runs[index], key) === active) index += 1;
131
+ }
132
+ return count;
133
+ }
134
+ function pickSplitKey(runs, remaining) {
135
+ let best = remaining[0];
136
+ let bestCount = groupCount(runs, best);
137
+ for (const key of remaining.slice(1)) {
138
+ const count = groupCount(runs, key);
139
+ if (count < bestCount) {
140
+ best = key;
141
+ bestCount = count;
142
+ }
143
+ }
144
+ return best;
145
+ }
146
+ function renderNestedStyles(runs, remainingKeys, context, base, constructs) {
147
+ if (remainingKeys.length === 0) return runs.map((run, local) => renderLeaf(run, context, base + local, constructs)).join("");
148
+ const key = pickSplitKey(runs, remainingKeys);
149
+ const rest = remainingKeys.filter((candidate) => candidate !== key);
123
150
  let out = "";
124
151
  let index = 0;
125
152
  while (index < runs.length) {
@@ -128,21 +155,34 @@ function renderNestedStyles(runs, depth, context) {
128
155
  const active = styleActive(current, key);
129
156
  let end = index + 1;
130
157
  while (end < runs.length && styleActive(runs[end], key) === active) end += 1;
131
- const inner = renderNestedStyles(runs.slice(index, end), depth + 1, context);
158
+ const inner = renderNestedStyles(runs.slice(index, end), rest, context, base + index, constructs);
132
159
  out += active ? wrapForStyle(inner, key, context, out) : inner;
133
160
  index = end;
134
161
  }
135
162
  return out;
136
163
  }
137
- function isPlainAutolink(run) {
138
- if (run.hyperlink === void 0 || run.hyperlink.length === 0 || run.bold === true || run.italic === true || run.strike === true || run.fontFamily === "Courier New" || run.fontFamily === "Cambria Math" || run.fontFamily === "Footnote Reference") return false;
164
+ function isPlainAutolink(run, footnoteReference) {
165
+ if (run.hyperlink === void 0 || run.hyperlink.length === 0 || run.bold === true || run.italic === true || run.strike === true || run.fontFamily === "Courier New" || run.fontFamily === "Cambria Math" || footnoteReference !== void 0) return false;
139
166
  return run.text === run.hyperlink || run.hyperlink === `mailto:${run.text}`;
140
167
  }
141
168
  function escapeLinkDestination(destination) {
142
169
  if (!/[\s()]/.test(destination)) return destination;
143
170
  return `<${destination.replace(/[<>]/g, (char) => `\\${char}`)}>`;
144
171
  }
145
- function emitRuns(runs, context) {
172
+ function renderLinkTitle(title) {
173
+ return title.replace(/[\n\r]+/g, " ").replace(/["\\]/g, (char) => `\\${char}`);
174
+ }
175
+ function linkTitleCovering(constructs, start, end) {
176
+ let tightest;
177
+ for (const extent of constructs ?? []) {
178
+ if (extent.descriptor.kind !== "link" || extent.descriptor.title === void 0) continue;
179
+ if (extent.startRun > start || extent.endRun < end) continue;
180
+ const current = tightest;
181
+ if (current === void 0 || extent.startRun > current.startRun || extent.startRun === current.startRun && extent.endRun < current.endRun) tightest = extent;
182
+ }
183
+ return tightest?.descriptor.kind === "link" ? tightest.descriptor.title : void 0;
184
+ }
185
+ function emitRuns(runs, context, constructs) {
146
186
  let out = "";
147
187
  let index = 0;
148
188
  while (index < runs.length) {
@@ -151,7 +191,7 @@ function emitRuns(runs, context) {
151
191
  if (run.hyperlink === void 0) {
152
192
  let end = index + 1;
153
193
  while (end < runs.length && runs[end]?.hyperlink === void 0) end += 1;
154
- out += renderNestedStyles(runs.slice(index, end), 0, context);
194
+ out += renderNestedStyles(runs.slice(index, end), STYLE_KEYS, context, index, constructs);
155
195
  index = end;
156
196
  continue;
157
197
  }
@@ -164,17 +204,18 @@ function emitRuns(runs, context) {
164
204
  severity: "info",
165
205
  message: `${String(group.length)} adjacent runs share the hyperlink "${hyperlink}"; markdown has no way to place two link boundaries back to back, so they render as one link spanning their combined text`
166
206
  });
167
- if (group.length === 1 && isPlainAutolink(group[0])) out += `<${group[0].text}>`;
207
+ const title = linkTitleCovering(constructs, index, groupEnd);
208
+ if (title === void 0 && group.length === 1 && isPlainAutolink(group[0], footnoteReferenceAt(constructs, index))) out += `<${group[0].text}>`;
168
209
  else {
169
- const linkText = renderNestedStyles(group, 0, context);
170
- out += `[${linkText}](${escapeLinkDestination(hyperlink)})`;
210
+ const linkText = renderNestedStyles(group, STYLE_KEYS, context, index, constructs);
211
+ out += `[${linkText}](${escapeLinkDestination(hyperlink)}${title === void 0 ? "" : ` "${renderLinkTitle(title)}"`})`;
171
212
  }
172
213
  index = groupEnd;
173
214
  }
174
215
  return out;
175
216
  }
176
- function emitRunsSingleLine(runs, context) {
177
- return emitRuns(runs, context).replace(/\\\n/g, " ");
217
+ function emitRunsSingleLine(runs, context, constructs) {
218
+ return emitRuns(runs, context, constructs).replace(ESCAPED_HARD_BREAK_PATTERN, " ").split(LINE_ENDING_PATTERN).join(" ");
178
219
  }
179
220
  //#endregion
180
- export { emitRuns, emitRunsSingleLine, escapeMarkdownText };
221
+ export { emitRuns, emitRunsSingleLine, escapeLinkDestination, escapeMarkdownText, renderLinkTitle };
@@ -1,6 +1,8 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
2
  const require_diagnostics_diagnostics = require("../diagnostics/diagnostics.cjs");
3
3
  const require_emit_inline = require("./inline.cjs");
4
+ const require_emit_image = require("./image.cjs");
5
+ const require_emit_html_table = require("./html-table.cjs");
4
6
  //#region src/emit/table.ts
5
7
  function toMarkdownAlignment(alignment) {
6
8
  return alignment === void 0 || alignment === "justify" ? "none" : alignment;
@@ -34,32 +36,37 @@ function escapeUnescapedPipes(text) {
34
36
  return out;
35
37
  }
36
38
  function renderCellText(cell, context) {
37
- if (cell.colSpan !== void 0 || cell.rowSpan !== void 0 || cell.background !== void 0) context.sink({
38
- code: require_diagnostics_diagnostics.MarkdownDiagnosticCodes.TABLE_CELL_FORMATTING_DROPPED,
39
- severity: "info",
40
- message: "a table cell's own colSpan/rowSpan/background has no GFM table equivalent; the cell renders as an ordinary unmerged, unstyled cell"
41
- });
42
39
  if (cell.blocks.length > 1) context.sink({
43
40
  code: require_diagnostics_diagnostics.MarkdownDiagnosticCodes.TABLE_CELL_MULTI_PARAGRAPH_JOINED,
44
41
  severity: "info",
45
- message: `a table cell with ${String(cell.blocks.length)} blocks has no multi-paragraph equivalent in a GFM table cell; their own rendered text is space-joined into the one line a cell allows`
42
+ message: `a table cell with ${String(cell.blocks.length)} blocks has no multi-paragraph equivalent in a GFM table cell; their own rendered text is joined with a literal <br> line break`
46
43
  });
47
44
  const parts = [];
48
45
  for (const block of cell.blocks) {
49
- if (block.kind !== "paragraph") {
46
+ if (block.kind === "image") {
50
47
  context.sink({
51
- code: require_diagnostics_diagnostics.MarkdownDiagnosticCodes.TABLE_CELL_FORMATTING_DROPPED,
48
+ code: require_diagnostics_diagnostics.MarkdownDiagnosticCodes.TABLE_CELL_IMAGE_DEGRADED,
52
49
  severity: "info",
53
- message: `a table cell containing a "${block.kind}" block has no GFM table equivalent; it is dropped entirely`
50
+ message: "a table cell's own image block has no GFM table equivalent; it emits inline instead, degrading on read-back to a run carrying the alt text with the image's data as that run's hyperlink"
54
51
  });
52
+ parts.push(require_emit_image.emitImage(block, context.embedImages));
55
53
  continue;
56
54
  }
57
- const text = require_emit_inline.emitRunsSingleLine(block.runs, context);
55
+ if (block.kind !== "paragraph") continue;
56
+ const text = require_emit_inline.emitRunsSingleLine(block.runs, context, block.constructs);
58
57
  if (text.length > 0) parts.push(text);
59
58
  }
60
- return escapeUnescapedPipes(parts.join(" "));
59
+ return escapeUnescapedPipes(parts.join("<br>"));
61
60
  }
62
61
  function emitTable(table, context) {
62
+ if (require_emit_html_table.tableNeedsHtmlFallback(table)) {
63
+ context.sink({
64
+ code: require_diagnostics_diagnostics.MarkdownDiagnosticCodes.TABLE_HTML_FALLBACK,
65
+ severity: "info",
66
+ message: "a cell in this table needs colSpan/rowSpan/background, or holds a block a GFM table cell cannot represent at all (most commonly a nested table); GFM's own table extension holds inline content only (github.github.com/gfm, \"Tables (extension)\"), so no single cell can carry an HTML sub-block inside an otherwise pipe-syntax table -- the whole table is rendered as a raw HTML <table> block instead (CommonMark spec 0.31.2, HTML blocks condition 6, https://spec.commonmark.org/0.31.2/#html-blocks), which src/html/html-table.ts's own reader recognises back into an equal ContentTable"
67
+ });
68
+ return require_emit_html_table.emitHtmlTable(table, context);
69
+ }
63
70
  const [header, ...body] = table.rows;
64
71
  if (header === void 0) return "";
65
72
  const alignments = header.cells.map((cell) => toMarkdownAlignment(cell.blocks[0]?.kind === "paragraph" ? cell.blocks[0].alignment : void 0));