mrkdwn.js 4.0.6 → 6.6.1

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 +1625 -23
  18. package/dist/codec.d.ts +1625 -23
  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
@@ -1,9 +1,10 @@
1
- import { i as MarkdownDiagnosticSink } from "../diagnostics-BWK1iGy7.cjs";
2
- import { InlineEmitContext } from "./inline.cjs";
1
+ import { i as MarkdownDiagnosticSink } from "../diagnostics-CjfBoW4K.cjs";
2
+ import { t as InlineEmitContext } from "../inline-DCIKQ83j.cjs";
3
3
  import { ContentTable } from "document-schema.js";
4
4
  //#region src/emit/table.d.ts
5
5
  interface TableEmitContext extends InlineEmitContext {
6
6
  readonly sink: MarkdownDiagnosticSink;
7
+ readonly embedImages: boolean;
7
8
  }
8
9
  declare function emitTable(table: ContentTable, context: TableEmitContext): string;
9
10
  //#endregion
@@ -1,9 +1,10 @@
1
- import { i as MarkdownDiagnosticSink } from "../diagnostics-BWK1iGy7.js";
2
- import { InlineEmitContext } from "./inline.js";
1
+ import { i as MarkdownDiagnosticSink } from "../diagnostics-CjfBoW4K.js";
2
+ import { t as InlineEmitContext } from "../inline-INoDAG37.js";
3
3
  import { ContentTable } from "document-schema.js";
4
4
  //#region src/emit/table.d.ts
5
5
  interface TableEmitContext extends InlineEmitContext {
6
6
  readonly sink: MarkdownDiagnosticSink;
7
+ readonly embedImages: boolean;
7
8
  }
8
9
  declare function emitTable(table: ContentTable, context: TableEmitContext): string;
9
10
  //#endregion
@@ -1,5 +1,7 @@
1
1
  import { MarkdownDiagnosticCodes } from "../diagnostics/diagnostics.js";
2
2
  import { emitRunsSingleLine } from "./inline.js";
3
+ import { emitImage } from "./image.js";
4
+ import { emitHtmlTable, tableNeedsHtmlFallback } from "./html-table.js";
3
5
  //#region src/emit/table.ts
4
6
  function toMarkdownAlignment(alignment) {
5
7
  return alignment === void 0 || alignment === "justify" ? "none" : alignment;
@@ -33,32 +35,37 @@ function escapeUnescapedPipes(text) {
33
35
  return out;
34
36
  }
35
37
  function renderCellText(cell, context) {
36
- if (cell.colSpan !== void 0 || cell.rowSpan !== void 0 || cell.background !== void 0) context.sink({
37
- code: MarkdownDiagnosticCodes.TABLE_CELL_FORMATTING_DROPPED,
38
- severity: "info",
39
- message: "a table cell's own colSpan/rowSpan/background has no GFM table equivalent; the cell renders as an ordinary unmerged, unstyled cell"
40
- });
41
38
  if (cell.blocks.length > 1) context.sink({
42
39
  code: MarkdownDiagnosticCodes.TABLE_CELL_MULTI_PARAGRAPH_JOINED,
43
40
  severity: "info",
44
- 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`
41
+ 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`
45
42
  });
46
43
  const parts = [];
47
44
  for (const block of cell.blocks) {
48
- if (block.kind !== "paragraph") {
45
+ if (block.kind === "image") {
49
46
  context.sink({
50
- code: MarkdownDiagnosticCodes.TABLE_CELL_FORMATTING_DROPPED,
47
+ code: MarkdownDiagnosticCodes.TABLE_CELL_IMAGE_DEGRADED,
51
48
  severity: "info",
52
- message: `a table cell containing a "${block.kind}" block has no GFM table equivalent; it is dropped entirely`
49
+ 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"
53
50
  });
51
+ parts.push(emitImage(block, context.embedImages));
54
52
  continue;
55
53
  }
56
- const text = emitRunsSingleLine(block.runs, context);
54
+ if (block.kind !== "paragraph") continue;
55
+ const text = emitRunsSingleLine(block.runs, context, block.constructs);
57
56
  if (text.length > 0) parts.push(text);
58
57
  }
59
- return escapeUnescapedPipes(parts.join(" "));
58
+ return escapeUnescapedPipes(parts.join("<br>"));
60
59
  }
61
60
  function emitTable(table, context) {
61
+ if (tableNeedsHtmlFallback(table)) {
62
+ context.sink({
63
+ code: MarkdownDiagnosticCodes.TABLE_HTML_FALLBACK,
64
+ severity: "info",
65
+ 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"
66
+ });
67
+ return emitHtmlTable(table, context);
68
+ }
62
69
  const [header, ...body] = table.rows;
63
70
  if (header === void 0) return "";
64
71
  const alignments = header.cells.map((cell) => toMarkdownAlignment(cell.blocks[0]?.kind === "paragraph" ? cell.blocks[0].alignment : void 0));
@@ -0,0 +1,243 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ const require_lower_image = require("../lower/image.cjs");
3
+ const require_shared_style_constants = require("../shared/style-constants.cjs");
4
+ let document_schema_js = require("document-schema.js");
5
+ //#region src/html/html-table.ts
6
+ function findBalancedClose(text, openEnd, tagNames) {
7
+ const names = tagNames.join("|");
8
+ const openPattern = new RegExp(`<(?:${names})(?=[\\s/>])`, "gi");
9
+ const closePattern = new RegExp(`</(?:${names})\\s*>`, "gi");
10
+ let depth = 1;
11
+ let pos = openEnd;
12
+ while (depth > 0) {
13
+ openPattern.lastIndex = pos;
14
+ closePattern.lastIndex = pos;
15
+ const openMatch = openPattern.exec(text);
16
+ const closeMatch = closePattern.exec(text);
17
+ if (closeMatch === null) return;
18
+ if (openMatch !== null && openMatch.index < closeMatch.index) {
19
+ depth += 1;
20
+ pos = openMatch.index + openMatch[0].length;
21
+ continue;
22
+ }
23
+ depth -= 1;
24
+ pos = closeMatch.index + closeMatch[0].length;
25
+ if (depth === 0) return {
26
+ start: closeMatch.index,
27
+ end: pos
28
+ };
29
+ }
30
+ }
31
+ function extractTopLevelElements(text, tagNames) {
32
+ const openPattern = new RegExp(`<(?:${tagNames.join("|")})\\b([^>]*)>`, "gi");
33
+ const elements = [];
34
+ let pos = 0;
35
+ for (;;) {
36
+ openPattern.lastIndex = pos;
37
+ const match = openPattern.exec(text);
38
+ if (match === null) break;
39
+ if (text.slice(pos, match.index).trim().length > 0) return;
40
+ const attrs = match[1] ?? "";
41
+ const openEnd = match.index + match[0].length;
42
+ const close = findBalancedClose(text, openEnd, tagNames);
43
+ if (close === void 0) return;
44
+ elements.push({
45
+ attrs,
46
+ inner: text.slice(openEnd, close.start)
47
+ });
48
+ pos = close.end;
49
+ }
50
+ if (text.slice(pos).trim().length > 0) return;
51
+ return elements;
52
+ }
53
+ function readAttr(attrs, name) {
54
+ return new RegExp(`\\b${name}\\s*=\\s*"([^"]*)"`, "i").exec(attrs)?.[1];
55
+ }
56
+ function readPositiveIntAttr(attrs, name) {
57
+ const raw = readAttr(attrs, name);
58
+ if (raw === void 0) return;
59
+ const value = Number.parseInt(raw, 10);
60
+ return Number.isFinite(value) && value > 0 ? value : void 0;
61
+ }
62
+ const BACKGROUND_COLOR_PATTERN = /background-color\s*:\s*#([0-9a-fA-F]{6}|[0-9a-fA-F]{3})\b/i;
63
+ function readBackgroundFill(attrs) {
64
+ const style = readAttr(attrs, "style");
65
+ if (style === void 0) return;
66
+ const hex = BACKGROUND_COLOR_PATTERN.exec(style)?.[1];
67
+ if (hex === void 0) return;
68
+ const normalised = hex.length === 3 ? hex.split("").map((digit) => digit + digit).join("") : hex;
69
+ return {
70
+ kind: "solid",
71
+ color: (0, document_schema_js.rgbHexToColor)(normalised)
72
+ };
73
+ }
74
+ const TEXT_ALIGN_PATTERN = /text-align\s*:\s*(left|right|center|justify)\b/i;
75
+ function readTextAlign(attrs) {
76
+ const style = readAttr(attrs, "style");
77
+ if (style === void 0) return;
78
+ switch (TEXT_ALIGN_PATTERN.exec(style)?.[1]?.toLowerCase()) {
79
+ case "left": return "left";
80
+ case "right": return "right";
81
+ case "center": return "center";
82
+ case "justify": return "justify";
83
+ default: return;
84
+ }
85
+ }
86
+ function unescapeHtml(text) {
87
+ return text.replaceAll("&lt;", "<").replaceAll("&gt;", ">").replaceAll("&quot;", "\"").replaceAll("&#39;", "'").replaceAll("&amp;", "&");
88
+ }
89
+ const INLINE_TAG_PATTERN = /<(a|strong|b|em|i|del|s|strike|code)\b([^>]*)>/i;
90
+ function applyTagStyle(style, tagName, attrs) {
91
+ switch (tagName) {
92
+ case "strong":
93
+ case "b": return {
94
+ ...style,
95
+ bold: true
96
+ };
97
+ case "em":
98
+ case "i": return {
99
+ ...style,
100
+ italic: true
101
+ };
102
+ case "del":
103
+ case "s":
104
+ case "strike": return {
105
+ ...style,
106
+ strike: true
107
+ };
108
+ case "code": return {
109
+ ...style,
110
+ fontFamily: require_shared_style_constants.MONOSPACE_FONT_FAMILY
111
+ };
112
+ case "a": {
113
+ const href = readAttr(attrs, "href");
114
+ return href === void 0 ? style : {
115
+ ...style,
116
+ hyperlink: unescapeHtml(href)
117
+ };
118
+ }
119
+ default: return style;
120
+ }
121
+ }
122
+ function pushPlainRun(runs, text, style) {
123
+ if (text.length === 0) return;
124
+ runs.push({
125
+ text: unescapeHtml(text),
126
+ ...style
127
+ });
128
+ }
129
+ function parseInlineHtml(text, style) {
130
+ const runs = [];
131
+ let pos = 0;
132
+ for (;;) {
133
+ const rest = text.slice(pos);
134
+ const match = INLINE_TAG_PATTERN.exec(rest);
135
+ if (match === null) {
136
+ pushPlainRun(runs, rest, style);
137
+ return runs;
138
+ }
139
+ pushPlainRun(runs, rest.slice(0, match.index), style);
140
+ const tagName = match[1].toLowerCase();
141
+ const attrs = match[2] ?? "";
142
+ const openEnd = pos + match.index + match[0].length;
143
+ const close = findBalancedClose(text, openEnd, [tagName]);
144
+ if (close === void 0) {
145
+ pushPlainRun(runs, rest, style);
146
+ return runs;
147
+ }
148
+ runs.push(...parseInlineHtml(text.slice(openEnd, close.start), applyTagStyle(style, tagName, attrs)));
149
+ pos = close.end;
150
+ }
151
+ }
152
+ const IMG_TAG_PATTERN = /^<img\b([^>]*?)\/?>$/i;
153
+ function parseHtmlImage(piece) {
154
+ const attrs = IMG_TAG_PATTERN.exec(piece)?.[1];
155
+ if (attrs === void 0) return;
156
+ const src = readAttr(attrs, "src");
157
+ if (src === void 0) return;
158
+ const alt = unescapeHtml(readAttr(attrs, "alt") ?? "");
159
+ const resolved = require_lower_image.resolveMarkdownImage(src, { alt }, void 0);
160
+ if (resolved === void 0) return;
161
+ return {
162
+ kind: "image",
163
+ format: resolved.format,
164
+ base64: resolved.base64,
165
+ widthPt: resolved.widthPt,
166
+ heightPt: resolved.heightPt,
167
+ altText: alt
168
+ };
169
+ }
170
+ function parseCellBlocks(inner, contentWidthPt) {
171
+ const trimmed = inner.trim();
172
+ if (trimmed.length === 0) return [];
173
+ const nestedTable = parseWholeTable(trimmed, contentWidthPt);
174
+ if (nestedTable !== void 0) return [nestedTable];
175
+ const blocks = [];
176
+ for (const segment of trimmed.split(/<br\s*\/?>/i)) {
177
+ const piece = segment.trim();
178
+ if (piece.length === 0) continue;
179
+ const image = parseHtmlImage(piece);
180
+ blocks.push(image ?? {
181
+ kind: "paragraph",
182
+ runs: parseInlineHtml(piece, {})
183
+ });
184
+ }
185
+ return blocks;
186
+ }
187
+ function applyTextAlign(blocks, alignment) {
188
+ const first = blocks[0];
189
+ if (alignment === void 0 || first?.kind !== "paragraph") return blocks;
190
+ return [{
191
+ ...first,
192
+ alignment
193
+ }, ...blocks.slice(1)];
194
+ }
195
+ function buildCell(cell, contentWidthPt) {
196
+ const colSpan = readPositiveIntAttr(cell.attrs, "colspan");
197
+ const rowSpan = readPositiveIntAttr(cell.attrs, "rowspan");
198
+ const background = readBackgroundFill(cell.attrs);
199
+ const textAlign = readTextAlign(cell.attrs);
200
+ const parsedBlocks = parseCellBlocks(cell.inner, contentWidthPt);
201
+ return {
202
+ blocks: applyTextAlign(parsedBlocks.length > 0 ? parsedBlocks : [{
203
+ kind: "paragraph",
204
+ runs: []
205
+ }], textAlign),
206
+ ...colSpan === void 0 ? {} : { colSpan },
207
+ ...rowSpan === void 0 ? {} : { rowSpan },
208
+ ...background === void 0 ? {} : { background }
209
+ };
210
+ }
211
+ function parseTableRows(inner, contentWidthPt) {
212
+ const rowElements = extractTopLevelElements(inner, ["tr"]);
213
+ if (rowElements === void 0 || rowElements.length === 0) return;
214
+ const rows = [];
215
+ for (const row of rowElements) {
216
+ const cellElements = extractTopLevelElements(row.inner, ["td", "th"]);
217
+ if (cellElements === void 0 || cellElements.length === 0) return;
218
+ rows.push({ cells: cellElements.map((cell) => buildCell(cell, contentWidthPt)) });
219
+ }
220
+ return rows;
221
+ }
222
+ function buildContentTable(rows, contentWidthPt) {
223
+ const header = rows[0];
224
+ const columnCount = Math.max(1, header.cells.reduce((sum, cell) => sum + (cell.colSpan ?? 1), 0));
225
+ return {
226
+ kind: "table",
227
+ rows,
228
+ columnWidthsPt: Array.from({ length: columnCount }, () => contentWidthPt / columnCount)
229
+ };
230
+ }
231
+ function parseWholeTable(text, contentWidthPt) {
232
+ const tableElements = extractTopLevelElements(text, ["table"]);
233
+ if (tableElements?.length !== 1) return;
234
+ const rows = parseTableRows(tableElements[0].inner, contentWidthPt);
235
+ return rows === void 0 ? void 0 : buildContentTable(rows, contentWidthPt);
236
+ }
237
+ function parseHtmlTable(literal, contentWidthPt) {
238
+ const trimmed = literal.trim();
239
+ if (!/^<table\b/i.test(trimmed)) return;
240
+ return parseWholeTable(trimmed, contentWidthPt);
241
+ }
242
+ //#endregion
243
+ exports.parseHtmlTable = parseHtmlTable;
@@ -0,0 +1,5 @@
1
+ import { ContentTable } from "document-schema.js";
2
+ //#region src/html/html-table.d.ts
3
+ declare function parseHtmlTable(literal: string, contentWidthPt: number): ContentTable | undefined;
4
+ //#endregion
5
+ export { parseHtmlTable };
@@ -0,0 +1,5 @@
1
+ import { ContentTable } from "document-schema.js";
2
+ //#region src/html/html-table.d.ts
3
+ declare function parseHtmlTable(literal: string, contentWidthPt: number): ContentTable | undefined;
4
+ //#endregion
5
+ export { parseHtmlTable };
@@ -0,0 +1,242 @@
1
+ import { resolveMarkdownImage } from "../lower/image.js";
2
+ import { MONOSPACE_FONT_FAMILY } from "../shared/style-constants.js";
3
+ import { rgbHexToColor } from "document-schema.js";
4
+ //#region src/html/html-table.ts
5
+ function findBalancedClose(text, openEnd, tagNames) {
6
+ const names = tagNames.join("|");
7
+ const openPattern = new RegExp(`<(?:${names})(?=[\\s/>])`, "gi");
8
+ const closePattern = new RegExp(`</(?:${names})\\s*>`, "gi");
9
+ let depth = 1;
10
+ let pos = openEnd;
11
+ while (depth > 0) {
12
+ openPattern.lastIndex = pos;
13
+ closePattern.lastIndex = pos;
14
+ const openMatch = openPattern.exec(text);
15
+ const closeMatch = closePattern.exec(text);
16
+ if (closeMatch === null) return;
17
+ if (openMatch !== null && openMatch.index < closeMatch.index) {
18
+ depth += 1;
19
+ pos = openMatch.index + openMatch[0].length;
20
+ continue;
21
+ }
22
+ depth -= 1;
23
+ pos = closeMatch.index + closeMatch[0].length;
24
+ if (depth === 0) return {
25
+ start: closeMatch.index,
26
+ end: pos
27
+ };
28
+ }
29
+ }
30
+ function extractTopLevelElements(text, tagNames) {
31
+ const openPattern = new RegExp(`<(?:${tagNames.join("|")})\\b([^>]*)>`, "gi");
32
+ const elements = [];
33
+ let pos = 0;
34
+ for (;;) {
35
+ openPattern.lastIndex = pos;
36
+ const match = openPattern.exec(text);
37
+ if (match === null) break;
38
+ if (text.slice(pos, match.index).trim().length > 0) return;
39
+ const attrs = match[1] ?? "";
40
+ const openEnd = match.index + match[0].length;
41
+ const close = findBalancedClose(text, openEnd, tagNames);
42
+ if (close === void 0) return;
43
+ elements.push({
44
+ attrs,
45
+ inner: text.slice(openEnd, close.start)
46
+ });
47
+ pos = close.end;
48
+ }
49
+ if (text.slice(pos).trim().length > 0) return;
50
+ return elements;
51
+ }
52
+ function readAttr(attrs, name) {
53
+ return new RegExp(`\\b${name}\\s*=\\s*"([^"]*)"`, "i").exec(attrs)?.[1];
54
+ }
55
+ function readPositiveIntAttr(attrs, name) {
56
+ const raw = readAttr(attrs, name);
57
+ if (raw === void 0) return;
58
+ const value = Number.parseInt(raw, 10);
59
+ return Number.isFinite(value) && value > 0 ? value : void 0;
60
+ }
61
+ const BACKGROUND_COLOR_PATTERN = /background-color\s*:\s*#([0-9a-fA-F]{6}|[0-9a-fA-F]{3})\b/i;
62
+ function readBackgroundFill(attrs) {
63
+ const style = readAttr(attrs, "style");
64
+ if (style === void 0) return;
65
+ const hex = BACKGROUND_COLOR_PATTERN.exec(style)?.[1];
66
+ if (hex === void 0) return;
67
+ const normalised = hex.length === 3 ? hex.split("").map((digit) => digit + digit).join("") : hex;
68
+ return {
69
+ kind: "solid",
70
+ color: rgbHexToColor(normalised)
71
+ };
72
+ }
73
+ const TEXT_ALIGN_PATTERN = /text-align\s*:\s*(left|right|center|justify)\b/i;
74
+ function readTextAlign(attrs) {
75
+ const style = readAttr(attrs, "style");
76
+ if (style === void 0) return;
77
+ switch (TEXT_ALIGN_PATTERN.exec(style)?.[1]?.toLowerCase()) {
78
+ case "left": return "left";
79
+ case "right": return "right";
80
+ case "center": return "center";
81
+ case "justify": return "justify";
82
+ default: return;
83
+ }
84
+ }
85
+ function unescapeHtml(text) {
86
+ return text.replaceAll("&lt;", "<").replaceAll("&gt;", ">").replaceAll("&quot;", "\"").replaceAll("&#39;", "'").replaceAll("&amp;", "&");
87
+ }
88
+ const INLINE_TAG_PATTERN = /<(a|strong|b|em|i|del|s|strike|code)\b([^>]*)>/i;
89
+ function applyTagStyle(style, tagName, attrs) {
90
+ switch (tagName) {
91
+ case "strong":
92
+ case "b": return {
93
+ ...style,
94
+ bold: true
95
+ };
96
+ case "em":
97
+ case "i": return {
98
+ ...style,
99
+ italic: true
100
+ };
101
+ case "del":
102
+ case "s":
103
+ case "strike": return {
104
+ ...style,
105
+ strike: true
106
+ };
107
+ case "code": return {
108
+ ...style,
109
+ fontFamily: MONOSPACE_FONT_FAMILY
110
+ };
111
+ case "a": {
112
+ const href = readAttr(attrs, "href");
113
+ return href === void 0 ? style : {
114
+ ...style,
115
+ hyperlink: unescapeHtml(href)
116
+ };
117
+ }
118
+ default: return style;
119
+ }
120
+ }
121
+ function pushPlainRun(runs, text, style) {
122
+ if (text.length === 0) return;
123
+ runs.push({
124
+ text: unescapeHtml(text),
125
+ ...style
126
+ });
127
+ }
128
+ function parseInlineHtml(text, style) {
129
+ const runs = [];
130
+ let pos = 0;
131
+ for (;;) {
132
+ const rest = text.slice(pos);
133
+ const match = INLINE_TAG_PATTERN.exec(rest);
134
+ if (match === null) {
135
+ pushPlainRun(runs, rest, style);
136
+ return runs;
137
+ }
138
+ pushPlainRun(runs, rest.slice(0, match.index), style);
139
+ const tagName = match[1].toLowerCase();
140
+ const attrs = match[2] ?? "";
141
+ const openEnd = pos + match.index + match[0].length;
142
+ const close = findBalancedClose(text, openEnd, [tagName]);
143
+ if (close === void 0) {
144
+ pushPlainRun(runs, rest, style);
145
+ return runs;
146
+ }
147
+ runs.push(...parseInlineHtml(text.slice(openEnd, close.start), applyTagStyle(style, tagName, attrs)));
148
+ pos = close.end;
149
+ }
150
+ }
151
+ const IMG_TAG_PATTERN = /^<img\b([^>]*?)\/?>$/i;
152
+ function parseHtmlImage(piece) {
153
+ const attrs = IMG_TAG_PATTERN.exec(piece)?.[1];
154
+ if (attrs === void 0) return;
155
+ const src = readAttr(attrs, "src");
156
+ if (src === void 0) return;
157
+ const alt = unescapeHtml(readAttr(attrs, "alt") ?? "");
158
+ const resolved = resolveMarkdownImage(src, { alt }, void 0);
159
+ if (resolved === void 0) return;
160
+ return {
161
+ kind: "image",
162
+ format: resolved.format,
163
+ base64: resolved.base64,
164
+ widthPt: resolved.widthPt,
165
+ heightPt: resolved.heightPt,
166
+ altText: alt
167
+ };
168
+ }
169
+ function parseCellBlocks(inner, contentWidthPt) {
170
+ const trimmed = inner.trim();
171
+ if (trimmed.length === 0) return [];
172
+ const nestedTable = parseWholeTable(trimmed, contentWidthPt);
173
+ if (nestedTable !== void 0) return [nestedTable];
174
+ const blocks = [];
175
+ for (const segment of trimmed.split(/<br\s*\/?>/i)) {
176
+ const piece = segment.trim();
177
+ if (piece.length === 0) continue;
178
+ const image = parseHtmlImage(piece);
179
+ blocks.push(image ?? {
180
+ kind: "paragraph",
181
+ runs: parseInlineHtml(piece, {})
182
+ });
183
+ }
184
+ return blocks;
185
+ }
186
+ function applyTextAlign(blocks, alignment) {
187
+ const first = blocks[0];
188
+ if (alignment === void 0 || first?.kind !== "paragraph") return blocks;
189
+ return [{
190
+ ...first,
191
+ alignment
192
+ }, ...blocks.slice(1)];
193
+ }
194
+ function buildCell(cell, contentWidthPt) {
195
+ const colSpan = readPositiveIntAttr(cell.attrs, "colspan");
196
+ const rowSpan = readPositiveIntAttr(cell.attrs, "rowspan");
197
+ const background = readBackgroundFill(cell.attrs);
198
+ const textAlign = readTextAlign(cell.attrs);
199
+ const parsedBlocks = parseCellBlocks(cell.inner, contentWidthPt);
200
+ return {
201
+ blocks: applyTextAlign(parsedBlocks.length > 0 ? parsedBlocks : [{
202
+ kind: "paragraph",
203
+ runs: []
204
+ }], textAlign),
205
+ ...colSpan === void 0 ? {} : { colSpan },
206
+ ...rowSpan === void 0 ? {} : { rowSpan },
207
+ ...background === void 0 ? {} : { background }
208
+ };
209
+ }
210
+ function parseTableRows(inner, contentWidthPt) {
211
+ const rowElements = extractTopLevelElements(inner, ["tr"]);
212
+ if (rowElements === void 0 || rowElements.length === 0) return;
213
+ const rows = [];
214
+ for (const row of rowElements) {
215
+ const cellElements = extractTopLevelElements(row.inner, ["td", "th"]);
216
+ if (cellElements === void 0 || cellElements.length === 0) return;
217
+ rows.push({ cells: cellElements.map((cell) => buildCell(cell, contentWidthPt)) });
218
+ }
219
+ return rows;
220
+ }
221
+ function buildContentTable(rows, contentWidthPt) {
222
+ const header = rows[0];
223
+ const columnCount = Math.max(1, header.cells.reduce((sum, cell) => sum + (cell.colSpan ?? 1), 0));
224
+ return {
225
+ kind: "table",
226
+ rows,
227
+ columnWidthsPt: Array.from({ length: columnCount }, () => contentWidthPt / columnCount)
228
+ };
229
+ }
230
+ function parseWholeTable(text, contentWidthPt) {
231
+ const tableElements = extractTopLevelElements(text, ["table"]);
232
+ if (tableElements?.length !== 1) return;
233
+ const rows = parseTableRows(tableElements[0].inner, contentWidthPt);
234
+ return rows === void 0 ? void 0 : buildContentTable(rows, contentWidthPt);
235
+ }
236
+ function parseHtmlTable(literal, contentWidthPt) {
237
+ const trimmed = literal.trim();
238
+ if (!/^<table\b/i.test(trimmed)) return;
239
+ return parseWholeTable(trimmed, contentWidthPt);
240
+ }
241
+ //#endregion
242
+ export { parseHtmlTable };
@@ -1,4 +1,4 @@
1
- import { s as MarkdownDocumentNode, v as MarkdownInlineNode } from "../ast-8XCbjRQT.cjs";
1
+ import { s as MarkdownDocumentNode, v as MarkdownInlineNode } from "../ast-CNfTgS0N.cjs";
2
2
  //#region src/html/render.d.ts
3
3
  declare function escapeHtml(text: string): string;
4
4
  declare function escapeHref(href: string): string;
@@ -1,4 +1,4 @@
1
- import { s as MarkdownDocumentNode, v as MarkdownInlineNode } from "../ast-8XCbjRQT.js";
1
+ import { s as MarkdownDocumentNode, v as MarkdownInlineNode } from "../ast-CNfTgS0N.js";
2
2
  //#region src/html/render.d.ts
3
3
  declare function escapeHtml(text: string): string;
4
4
  declare function escapeHref(href: string): string;
@@ -14,10 +14,10 @@ function bytesToBase64(bytes) {
14
14
  const b0 = bytes[index];
15
15
  const b1 = index + 1 < length ? bytes[index + 1] : 0;
16
16
  const b2 = index + 2 < length ? bytes[index + 2] : 0;
17
- out += BASE64_TABLE[b0 >> 2];
18
- out += BASE64_TABLE[(b0 & 3) << 4 | b1 >> 4];
19
- out += index + 1 < length ? BASE64_TABLE[(b1 & 15) << 2 | b2 >> 6] : "=";
20
- out += index + 2 < length ? BASE64_TABLE[b2 & 63] : "=";
17
+ out += BASE64_TABLE.charAt(b0 >> 2);
18
+ out += BASE64_TABLE.charAt((b0 & 3) << 4 | b1 >> 4);
19
+ out += index + 1 < length ? BASE64_TABLE.charAt((b1 & 15) << 2 | b2 >> 6) : "=";
20
+ out += index + 2 < length ? BASE64_TABLE.charAt(b2 & 63) : "=";
21
21
  }
22
22
  return out;
23
23
  }
@@ -1,2 +1,2 @@
1
- import { a as detectImageFormat, i as bytesToBase64, n as ImageFormat, o as readImageDimensions, r as base64ToBytes, t as ImageDimensions } from "../image-DescdRgq.cjs";
1
+ import { a as detectImageFormat, i as bytesToBase64, n as ImageFormat, o as readImageDimensions, r as base64ToBytes, t as ImageDimensions } from "../image-BtmTG4_a.cjs";
2
2
  export { ImageDimensions, ImageFormat, base64ToBytes, bytesToBase64, detectImageFormat, readImageDimensions };
@@ -1,2 +1,2 @@
1
- import { a as detectImageFormat, i as bytesToBase64, n as ImageFormat, o as readImageDimensions, r as base64ToBytes, t as ImageDimensions } from "../image-DescdRgq.js";
1
+ import { a as detectImageFormat, i as bytesToBase64, n as ImageFormat, o as readImageDimensions, r as base64ToBytes, t as ImageDimensions } from "../image-BtmTG4_a.js";
2
2
  export { ImageDimensions, ImageFormat, base64ToBytes, bytesToBase64, detectImageFormat, readImageDimensions };
@@ -13,10 +13,10 @@ function bytesToBase64(bytes) {
13
13
  const b0 = bytes[index];
14
14
  const b1 = index + 1 < length ? bytes[index + 1] : 0;
15
15
  const b2 = index + 2 < length ? bytes[index + 2] : 0;
16
- out += BASE64_TABLE[b0 >> 2];
17
- out += BASE64_TABLE[(b0 & 3) << 4 | b1 >> 4];
18
- out += index + 1 < length ? BASE64_TABLE[(b1 & 15) << 2 | b2 >> 6] : "=";
19
- out += index + 2 < length ? BASE64_TABLE[b2 & 63] : "=";
16
+ out += BASE64_TABLE.charAt(b0 >> 2);
17
+ out += BASE64_TABLE.charAt((b0 & 3) << 4 | b1 >> 4);
18
+ out += index + 1 < length ? BASE64_TABLE.charAt((b1 & 15) << 2 | b2 >> 6) : "=";
19
+ out += index + 2 < length ? BASE64_TABLE.charAt(b2 & 63) : "=";
20
20
  }
21
21
  return out;
22
22
  }
@@ -1,4 +1,4 @@
1
- import { n as ImageFormat } from "./image-DescdRgq.cjs";
1
+ import { n as ImageFormat } from "./image-BtmTG4_a.cjs";
2
2
  //#region src/lower/image.d.ts
3
3
  interface MarkdownImageResolveContext {
4
4
  readonly alt: string;
@@ -3,7 +3,7 @@ interface ImageDimensions {
3
3
  readonly widthPx: number;
4
4
  readonly heightPx: number;
5
5
  }
6
- type ImageFormat = 'png' | 'jpeg';
6
+ type ImageFormat = "png" | "jpeg";
7
7
  declare function bytesToBase64(bytes: Uint8Array): string;
8
8
  declare function base64ToBytes(base64: string): Uint8Array;
9
9
  declare function detectImageFormat(bytes: Uint8Array): ImageFormat | undefined;
@@ -3,7 +3,7 @@ interface ImageDimensions {
3
3
  readonly widthPx: number;
4
4
  readonly heightPx: number;
5
5
  }
6
- type ImageFormat = 'png' | 'jpeg';
6
+ type ImageFormat = "png" | "jpeg";
7
7
  declare function bytesToBase64(bytes: Uint8Array): string;
8
8
  declare function base64ToBytes(base64: string): Uint8Array;
9
9
  declare function detectImageFormat(bytes: Uint8Array): ImageFormat | undefined;
@@ -1,4 +1,4 @@
1
- import { n as ImageFormat } from "./image-DescdRgq.js";
1
+ import { n as ImageFormat } from "./image-BtmTG4_a.js";
2
2
  //#region src/lower/image.d.ts
3
3
  interface MarkdownImageResolveContext {
4
4
  readonly alt: string;