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
@@ -2,21 +2,31 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
2
  const require_inline_footnote = require("../inline/footnote.cjs");
3
3
  require("../defaults/defaults.cjs");
4
4
  const require_diagnostics_diagnostics = require("../diagnostics/diagnostics.cjs");
5
- const require_shared_list_id = require("../shared/list-id.cjs");
5
+ const require_html_html = require("../html/html.cjs");
6
+ const require_shared_line_ending = require("../shared/line-ending.cjs");
7
+ require("../scan/scan.cjs");
8
+ const require_block_line = require("../block/line.cjs");
9
+ const require_block_list = require("../block/list.cjs");
10
+ const require_block_table = require("../block/table.cjs");
11
+ const require_block_block = require("../block/block.cjs");
6
12
  const require_shared_style_constants = require("../shared/style-constants.cjs");
13
+ const require_shared_list_id = require("../shared/list-id.cjs");
7
14
  const require_emit_front_matter = require("./front-matter.cjs");
8
15
  const require_emit_inline = require("./inline.cjs");
9
16
  const require_emit_image = require("./image.cjs");
10
17
  const require_emit_table = require("./table.cjs");
11
18
  let document_schema_js = require("document-schema.js");
12
19
  //#region src/emit/emit.ts
20
+ function isDataUri(destination) {
21
+ return destination.startsWith("data:");
22
+ }
13
23
  const MAX_SETEXT_LEVEL = 2;
14
24
  const SETEXT_LEVEL_1_CHAR = "=";
15
25
  const SETEXT_LEVEL_2_CHAR = "-";
16
26
  const MIN_SETEXT_UNDERLINE_LENGTH = 1;
17
27
  function renderSetextHeading(level, text) {
18
28
  const underlineChar = level === 1 ? SETEXT_LEVEL_1_CHAR : SETEXT_LEVEL_2_CHAR;
19
- const firstLine = text.split("\n")[0] ?? "";
29
+ const firstLine = text.split(require_shared_line_ending.LINE_ENDING_PATTERN)[0] ?? "";
20
30
  return `${text}\n${underlineChar.repeat(Math.max(MIN_SETEXT_UNDERLINE_LENGTH, firstLine.length))}`;
21
31
  }
22
32
  const MIN_CODE_FENCE_LENGTH = 3;
@@ -43,6 +53,84 @@ function isQuotableStyle(styleId) {
43
53
  if (styleId === void 0) return false;
44
54
  return QUOTABLE_STYLE_IDS.has(styleId) || require_shared_style_constants.parseHeadingStyleId(styleId) !== void 0;
45
55
  }
56
+ function terminatesCleanly(styleId) {
57
+ if (styleId === void 0 || styleId === "Quote" || styleId === "HTMLPreformatted") return false;
58
+ return styleId === "CodeBlock" || styleId === "MathBlock" || styleId === "HorizontalRule" || require_shared_style_constants.parseHeadingStyleId(styleId) !== void 0;
59
+ }
60
+ const BLANK_OR_WHITESPACE_ONLY_LINE = /^[ \t]*$/;
61
+ function firstContentLineIndex(text) {
62
+ return text.split(require_shared_line_ending.LINE_ENDING_PATTERN).findIndex((line) => !BLANK_OR_WHITESPACE_ONLY_LINE.test(line));
63
+ }
64
+ function leadingIndentColumns(line) {
65
+ let column = 0;
66
+ for (const char of line) if (char === " ") column += 1;
67
+ else if (char === " ") column += 4 - column % 4;
68
+ else break;
69
+ return column;
70
+ }
71
+ const MAX_LEADING_BLANK_LINES_FOR_SETEXT = 1;
72
+ function interruptsSetextParagraph(line, atBlockStart) {
73
+ const cursor = new require_block_line.LineCursor(line);
74
+ if (cursor.indented) return false;
75
+ if (cursor.peekNextNonspace() === ">") return true;
76
+ const rest = cursor.restFromNextNonspace();
77
+ if (require_block_block.ATX_MARKER_PATTERN.test(rest) || require_block_block.CODE_FENCE_PATTERN.test(rest) || require_block_block.THEMATIC_BREAK_PATTERN.test(rest) || require_block_block.MATH_BLOCK_MARKER_PATTERN.test(rest) || require_html_html.matchHtmlBlockStart(rest, !atBlockStart) !== void 0) return true;
78
+ return require_block_list.parseListMarker(cursor, !atBlockStart) !== void 0;
79
+ }
80
+ function tableDelimiterRowPromotesPrecedingLine(line, precedingLine) {
81
+ const cursor = new require_block_line.LineCursor(line);
82
+ if (cursor.indented) return false;
83
+ return require_block_table.parseTableDelimiterRow(cursor.restFromNextNonspace())?.length === require_block_table.splitTableRow(precedingLine).length;
84
+ }
85
+ function unsafeSetextBreakReason(text) {
86
+ let sawContentLine = false;
87
+ let leadingBlankLines = 0;
88
+ let precedingLine = "";
89
+ for (const line of text.split(require_shared_line_ending.LINE_ENDING_PATTERN)) {
90
+ const isBlank = BLANK_OR_WHITESPACE_ONLY_LINE.test(line);
91
+ if (!sawContentLine) {
92
+ if (isBlank) {
93
+ leadingBlankLines += 1;
94
+ if (leadingBlankLines > MAX_LEADING_BLANK_LINES_FOR_SETEXT) return "blank-line";
95
+ continue;
96
+ }
97
+ sawContentLine = true;
98
+ if (leadingIndentColumns(line) >= 4) return "leading-indentation";
99
+ if (interruptsSetextParagraph(line, true)) return "interrupting-line";
100
+ precedingLine = line;
101
+ continue;
102
+ }
103
+ if (isBlank) return "blank-line";
104
+ if (interruptsSetextParagraph(line, false) || tableDelimiterRowPromotesPrecedingLine(line, precedingLine)) return "interrupting-line";
105
+ precedingLine = line;
106
+ }
107
+ return sawContentLine ? void 0 : "blank-line";
108
+ }
109
+ function embedsUnsafeBreakForSetext(text) {
110
+ return unsafeSetextBreakReason(text) !== void 0;
111
+ }
112
+ function unsafeSetextDiagnosticMessage(level, reason, embedsLineBreak) {
113
+ const hazard = reason === "leading-indentation" ? `the line that would become the setext heading's own first line of text opens with 4 or more columns of space/tab indentation -- setext's own grammar (spec 0.31.2) requires the first line to have "not more than 3 spaces of indentation", so promoting would read that line back as an indented code block instead of heading text` : reason === "interrupting-line" ? `a line of the heading's own text -- including possibly its first -- would itself be interpretable as a code fence, ATX heading, block quote, thematic break, list item, HTML block, math block, or GFM table delimiter row -- setext's own grammar (spec 0.31.2) requires every one of the heading's lines to instead read as ordinary paragraph text, so promoting would read that line back as the start of a fresh, unrelated block (or, for a table delimiter row, a conversion of the line before it) rather than more of the heading` : `promoting would leave the setext underline with no heading text of its own to attach to -- either a blank line immediately before it (a trailing break), a blank line in the middle of the heading's own text (a genuinely interior break), or the heading's own rendered text being entirely blank to begin with -- setext's own grammar requires "one or more lines of text, not interrupted by a blank line"`;
114
+ return embedsLineBreak ? `a level ${String(level)} heading's own content contains a line break, but ${hazard}; ATX collapses the break to a single space instead of promoting into a corrupt reparse` : `the effective WriteMarkdownOptions.headingStyle is 'setext' (an explicit caller choice -- a break-free heading only reaches this path when requested outright), but ${hazard}; rendered as ATX instead of promoting into a corrupt reparse`;
115
+ }
116
+ function willRenderAsSetext(paragraph, level, context) {
117
+ if (level > MAX_SETEXT_LEVEL) return false;
118
+ const text = require_emit_inline.emitRuns(paragraph.runs, {
119
+ sink: require_diagnostics_diagnostics.NOOP_MARKDOWN_DIAGNOSTIC_SINK,
120
+ emphasisMarker: context.emphasisMarker
121
+ }, paragraph.constructs);
122
+ if (embedsUnsafeBreakForSetext(text)) return false;
123
+ return context.headingStyle === "setext" || require_shared_line_ending.LINE_ENDING_PATTERN.test(text);
124
+ }
125
+ function canInterruptOpenParagraph(paragraph, context) {
126
+ const styleId = paragraph.styleId;
127
+ if (styleId === void 0 || styleId === "Quote" || styleId === "HTMLPreformatted") return false;
128
+ if (styleId === "CodeBlock" || styleId === "MathBlock") return true;
129
+ if (styleId === "HorizontalRule") return context.thematicBreakChar !== SETEXT_LEVEL_2_CHAR;
130
+ const headingLevel = require_shared_style_constants.parseHeadingStyleId(styleId);
131
+ if (headingLevel !== void 0) return !willRenderAsSetext(paragraph, (0, document_schema_js.clampHeadingLevel)(headingLevel), context);
132
+ return false;
133
+ }
46
134
  function quoteDepthOf(paragraph) {
47
135
  if (paragraph.indentLeftPt === void 0 || paragraph.indentLeftPt <= 0) return 0;
48
136
  return Math.max(1, Math.round(paragraph.indentLeftPt / 36));
@@ -52,9 +140,12 @@ function renderParagraphBody(paragraph, context) {
52
140
  if (paragraph.styleId === "CodeBlock") {
53
141
  const literal = paragraph.runs.map((run) => run.text).join("");
54
142
  const fence = codeFenceFor(literal, context.codeFenceChar);
55
- return literal.length === 0 ? `${fence}\n${fence}` : `${fence}\n${literal}\n${fence}`;
143
+ const remainder = paragraph.source?.format === "markdown" ? paragraph.source.xml : void 0;
144
+ const info = [paragraph.codeLanguage, remainder].filter((part) => part !== void 0 && part.length > 0).join(" ");
145
+ const opening = info.length > 0 ? `${fence} ${info}` : fence;
146
+ return literal.length === 0 ? `${opening}\n${fence}` : `${opening}\n${literal}\n${fence}`;
56
147
  }
57
- if (paragraph.styleId === "HTMLPreformatted") return paragraph.runs.map((run) => run.text).join("");
148
+ if (paragraph.styleId === "HTMLPreformatted") return paragraph.source?.format === "markdown" ? paragraph.source.xml : paragraph.runs.map((run) => run.text).join("");
58
149
  if (paragraph.styleId === "MathBlock") return `$$\n${paragraph.runs.map((run) => run.text).join("")}\n$$`;
59
150
  const headingLevel = paragraph.styleId === void 0 ? void 0 : require_shared_style_constants.parseHeadingStyleId(paragraph.styleId);
60
151
  if (headingLevel !== void 0) {
@@ -64,15 +155,46 @@ function renderParagraphBody(paragraph, context) {
64
155
  severity: "info",
65
156
  message: `heading level ${String(headingLevel)} exceeds ATX's own six-"#" ceiling and is clamped to ${String(level)}`
66
157
  });
67
- const text = require_emit_inline.emitRuns(paragraph.runs, context);
68
- if (context.headingStyle === "setext" && level <= MAX_SETEXT_LEVEL) return renderSetextHeading(level, text);
158
+ const text = require_emit_inline.emitRuns(paragraph.runs, context, paragraph.constructs);
159
+ const embedsLineBreak = require_shared_line_ending.LINE_ENDING_PATTERN.test(text);
160
+ const unsafeSetextReason = unsafeSetextBreakReason(text);
161
+ const unsafeForSetext = unsafeSetextReason !== void 0;
162
+ const setextRequested = context.headingStyle === "setext" || embedsLineBreak;
163
+ if (setextRequested && level <= MAX_SETEXT_LEVEL && !unsafeForSetext) {
164
+ if (context.headingStyle !== "setext") {
165
+ const leadingBreakAbsorbed = firstContentLineIndex(text) > 0;
166
+ context.sink({
167
+ code: require_diagnostics_diagnostics.MarkdownDiagnosticCodes.HEADING_STYLE_OVERRIDDEN_FOR_LINE_BREAK,
168
+ severity: "info",
169
+ message: leadingBreakAbsorbed ? `the effective WriteMarkdownOptions.headingStyle is 'atx' (its own default, or an explicit caller choice), but this level ${String(level)} heading's own content contains a line break ATX has no way to hold -- rendered as setext instead, though the heading's own leading blank line is absorbed as ordinary space ahead of it on read-back rather than reproduced inside it` : `the effective WriteMarkdownOptions.headingStyle is 'atx' (its own default, or an explicit caller choice), but this level ${String(level)} heading's own content contains a line break ATX has no way to hold -- rendered as setext instead so the break survives`
170
+ });
171
+ }
172
+ return renderSetextHeading(level, text);
173
+ }
174
+ if (setextRequested && level <= MAX_SETEXT_LEVEL && unsafeForSetext) {
175
+ context.sink({
176
+ code: require_diagnostics_diagnostics.MarkdownDiagnosticCodes.HEADING_LINE_BREAK_UNSAFE_FOR_SETEXT,
177
+ severity: "info",
178
+ message: unsafeSetextDiagnosticMessage(level, unsafeSetextReason, embedsLineBreak)
179
+ });
180
+ if (embedsLineBreak) return `${"#".repeat(level)} ${text.replace(require_shared_line_ending.ESCAPED_HARD_BREAK_PATTERN, " ").split(require_shared_line_ending.LINE_ENDING_PATTERN).join(" ")}`;
181
+ return `${"#".repeat(level)} ${text}`;
182
+ }
183
+ if (embedsLineBreak) {
184
+ context.sink({
185
+ code: require_diagnostics_diagnostics.MarkdownDiagnosticCodes.HEADING_LINE_BREAK_COLLAPSED,
186
+ severity: "info",
187
+ message: `a level ${String(level)} heading's own content contains a line break; only setext's own level-1/2 grammar can hold one, so ATX collapses it to a single space`
188
+ });
189
+ return `${"#".repeat(level)} ${text.replace(require_shared_line_ending.ESCAPED_HARD_BREAK_PATTERN, " ").split(require_shared_line_ending.LINE_ENDING_PATTERN).join(" ")}`;
190
+ }
69
191
  return `${"#".repeat(level)} ${text}`;
70
192
  }
71
- return require_emit_inline.emitRuns(paragraph.runs, context);
193
+ return require_emit_inline.emitRuns(paragraph.runs, context, paragraph.constructs);
72
194
  }
73
195
  function renderParagraph(paragraph, context) {
74
196
  const body = renderParagraphBody(paragraph, context);
75
- const depth = quoteDepthOf(paragraph);
197
+ const depth = context.divisionDepth > 0 ? 0 : quoteDepthOf(paragraph);
76
198
  if (depth === 0) return body;
77
199
  if (!isQuotableStyle(paragraph.styleId)) {
78
200
  context.sink({
@@ -90,8 +212,13 @@ function renderTopLevelBlock(block, context) {
90
212
  case "paragraph": return renderParagraph(block, context);
91
213
  case "table": return require_emit_table.emitTable(block, context);
92
214
  case "image": return require_emit_image.emitImage(block, context.embedImages);
93
- case "pageBreak":
94
- case "embeddedObject": return "";
215
+ case "embeddedObject":
216
+ if (block.objectKind === "formula" && block.document.kind === "formula" && block.document.formula.presentation !== void 0) {
217
+ const latex = block.document.formula.presentation.latex;
218
+ return latex.length === 0 ? "$$\n$$" : `$$\n${latex}\n$$`;
219
+ }
220
+ return "";
221
+ case "pageBreak": return "";
95
222
  }
96
223
  }
97
224
  function listInfoFor(numId, context) {
@@ -117,16 +244,9 @@ function listInfoFor(numId, context) {
117
244
  }
118
245
  return info;
119
246
  }
120
- function checkboxPrefixFor(item) {
121
- const first = item.runs[0];
122
- if (first === void 0) return;
123
- if (first.text.startsWith(`☒ `)) return "[x] ";
124
- if (first.text.startsWith(`☐ `)) return "[ ] ";
125
- }
126
- function stripCheckboxRun(item, checkboxPrefix) {
127
- if (checkboxPrefix === void 0) return item;
247
+ function stripCheckboxRun(item) {
128
248
  const first = item.runs[0];
129
- const glyphPrefix = checkboxPrefix === "[x] " ? `☒ ` : `☐ `;
249
+ const glyphPrefix = first?.text.startsWith(`☒ `) ? `☒ ` : `☐ `;
130
250
  if (!first?.text.startsWith(glyphPrefix)) return item;
131
251
  const strippedText = first.text.slice(glyphPrefix.length);
132
252
  const runs = strippedText.length === 0 ? item.runs.slice(1) : [{
@@ -138,49 +258,191 @@ function stripCheckboxRun(item, checkboxPrefix) {
138
258
  runs
139
259
  };
140
260
  }
141
- function renderListItemMarker(numId, info, item, context) {
142
- const checkboxText = (info?.task === true ? checkboxPrefixFor(item) : void 0) ?? "";
261
+ function toEmitItem(item) {
262
+ return item.kind === "paragraph" ? { block: item.block } : item.item;
263
+ }
264
+ function firstBlockCheckbox(first, taskNumId) {
265
+ if (first.list.checked !== void 0) return {
266
+ checkboxText: first.list.checked ? "[x] " : "[ ] ",
267
+ stripGlyph: false
268
+ };
269
+ if (!taskNumId || first.kind !== "paragraph") return {
270
+ checkboxText: "",
271
+ stripGlyph: false
272
+ };
273
+ const leading = first.block.runs[0]?.text ?? "";
274
+ if (leading.startsWith(`☒ `)) return {
275
+ checkboxText: "[x] ",
276
+ stripGlyph: true
277
+ };
278
+ if (leading.startsWith(`☐ `)) return {
279
+ checkboxText: "[ ] ",
280
+ stripGlyph: true
281
+ };
282
+ return {
283
+ checkboxText: "",
284
+ stripGlyph: false
285
+ };
286
+ }
287
+ function renderListItemMarker(numId, info, glyph, checkboxText, context) {
143
288
  if (info?.type === "ordered" && numId !== void 0) {
144
289
  const next = context.orderedCounters.get(numId) ?? info.start ?? 1;
145
290
  context.orderedCounters.set(numId, next + 1);
146
- const bare = `${String(next)}${context.orderedDelimiter} `;
291
+ const bare = `${String(next)}${glyph} `;
147
292
  return {
148
293
  full: `${bare}${checkboxText}`,
149
294
  bareLength: bare.length
150
295
  };
151
296
  }
152
- const bare = `${context.bulletMarker} `;
297
+ const bare = `${glyph} `;
153
298
  return {
154
299
  full: `${bare}${checkboxText}`,
155
300
  bareLength: bare.length
156
301
  };
157
302
  }
303
+ const BULLET_GLYPH_CANDIDATES = [
304
+ "-",
305
+ "+",
306
+ "*"
307
+ ];
308
+ const ORDERED_GLYPH_CANDIDATES = [".", ")"];
309
+ function resolveListGlyph(numId, type, previousSibling, context) {
310
+ const cached = context.resolvedListGlyphs.get(numId);
311
+ if (cached !== void 0) return cached;
312
+ const defaultGlyph = type === "ordered" ? context.orderedDelimiter : context.bulletMarker;
313
+ const glyph = previousSibling?.type === type && previousSibling.glyph === defaultGlyph ? (type === "ordered" ? ORDERED_GLYPH_CANDIDATES : BULLET_GLYPH_CANDIDATES).find((candidate) => candidate !== defaultGlyph) ?? defaultGlyph : defaultGlyph;
314
+ context.resolvedListGlyphs.set(numId, glyph);
315
+ return glyph;
316
+ }
317
+ function consumeSameItemRun(items, from, level, itemId) {
318
+ let end = from;
319
+ while (end < items.length) {
320
+ const candidate = items[end];
321
+ if (candidate?.list.level !== level || candidate.list.itemId !== itemId) break;
322
+ end += 1;
323
+ }
324
+ return end;
325
+ }
326
+ function collectListItem(items, start, level, itemId) {
327
+ const segments = [];
328
+ let index = start;
329
+ const ownEnd = itemId === void 0 ? index + 1 : consumeSameItemRun(items, index, level, itemId);
330
+ segments.push({
331
+ kind: "own",
332
+ blocks: items.slice(index, ownEnd)
333
+ });
334
+ index = ownEnd;
335
+ for (;;) {
336
+ let nestedEnd = index;
337
+ while (nestedEnd < items.length) {
338
+ const candidateLevel = items[nestedEnd]?.list.level;
339
+ if (candidateLevel === void 0 || candidateLevel <= level) break;
340
+ nestedEnd += 1;
341
+ }
342
+ if (nestedEnd === index) break;
343
+ segments.push({
344
+ kind: "nested",
345
+ blocks: items.slice(index, nestedEnd)
346
+ });
347
+ index = nestedEnd;
348
+ if (itemId === void 0) break;
349
+ const resumedEnd = consumeSameItemRun(items, index, level, itemId);
350
+ if (resumedEnd === index) break;
351
+ segments.push({
352
+ kind: "own",
353
+ blocks: items.slice(index, resumedEnd)
354
+ });
355
+ index = resumedEnd;
356
+ }
357
+ return {
358
+ segments,
359
+ next: index
360
+ };
361
+ }
362
+ function emitItemCanInterrupt(item, context) {
363
+ if (!isConstructItem(item)) return item.block.kind === "paragraph" ? canInterruptOpenParagraph(item.block, context) : true;
364
+ if (isMaterialisedDivision(item)) return true;
365
+ const first = item.children[0];
366
+ return first === void 0 ? true : emitItemCanInterrupt(first, context);
367
+ }
368
+ function lastStyleIdOf(item) {
369
+ if (isConstructItem(item)) {
370
+ const last = item.children[item.children.length - 1];
371
+ return last === void 0 ? void 0 : lastStyleIdOf(last);
372
+ }
373
+ return item.block.kind === "paragraph" ? item.block.styleId : void 0;
374
+ }
375
+ function regionItemCanInterrupt(item, context) {
376
+ return emitItemCanInterrupt(toEmitItem(item), context);
377
+ }
378
+ function lastStyleIdOfRegionItem(item) {
379
+ return lastStyleIdOf(toEmitItem(item));
380
+ }
381
+ function listRegionItemBody(item, context, stripGlyph) {
382
+ if (item.kind === "paragraph") return renderParagraphBody(stripGlyph ? stripCheckboxRun(item.block) : item.block, context);
383
+ const previousEnclosingItemId = context.enclosingItemId;
384
+ context.enclosingItemId = item.list.itemId;
385
+ const body = renderConstruct(item.item, context);
386
+ context.enclosingItemId = previousEnclosingItemId;
387
+ return body;
388
+ }
389
+ function requiresBlankLineBefore(next, previousStyleId, context) {
390
+ if (previousStyleId === "HTMLPreformatted") return true;
391
+ return !terminatesCleanly(previousStyleId) && !regionItemCanInterrupt(next, context);
392
+ }
158
393
  function renderListRegion(items, context) {
159
394
  const parts = [];
160
395
  let index = 0;
396
+ let previousSibling;
161
397
  while (index < items.length) {
162
398
  const item = items[index];
163
- if (item?.list === void 0) break;
164
- const { numId, level } = item.list;
399
+ if (item === void 0) break;
400
+ const { numId, level, itemId } = item.list;
165
401
  const info = listInfoFor(numId, context);
166
- let lookahead = index + 1;
167
- while (lookahead < items.length && (items[lookahead]?.list?.level ?? -1) > level) lookahead += 1;
168
- const nestedItems = items.slice(index + 1, lookahead);
169
- const checkboxPrefix = info?.task === true ? checkboxPrefixFor(item) : void 0;
170
- const marker = renderListItemMarker(numId, info, item, context);
171
- const bodyLines = renderParagraphBody(stripCheckboxRun(item, checkboxPrefix), context).split("\n");
402
+ const loose = info?.loose === true;
403
+ const type = info?.type ?? "bullet";
404
+ const glyph = numId === void 0 ? type === "ordered" ? context.orderedDelimiter : context.bulletMarker : resolveListGlyph(numId, type, previousSibling, context);
405
+ if (numId !== void 0) previousSibling = {
406
+ numId,
407
+ type,
408
+ glyph
409
+ };
410
+ const { segments, next } = collectListItem(items, index, level, itemId);
411
+ const first = segments[0]?.blocks[0];
412
+ if (first === void 0) break;
413
+ const { checkboxText, stripGlyph } = firstBlockCheckbox(first, info?.task === true);
414
+ const marker = renderListItemMarker(numId, info, glyph, checkboxText, context);
172
415
  const indent = " ".repeat(marker.bareLength);
173
- const [firstLine = "", ...restLines] = bodyLines;
174
- let text = [`${marker.full}${firstLine}`, ...restLines.map((line) => `${indent}${line}`)].join("\n");
175
- if (nestedItems.length > 0) {
176
- const nested = renderListRegion(nestedItems, context).split("\n").map((line) => line.length === 0 ? line : `${indent}${line}`).join("\n");
177
- text += `\n${nested}`;
416
+ let text = "";
417
+ let renderedFirstLine = false;
418
+ let previousStyleId;
419
+ for (const segment of segments) {
420
+ if (segment.kind === "nested") {
421
+ const nested = renderListRegion(segment.blocks, context).split("\n").map((line) => line.length === 0 ? line : `${indent}${line}`).join("\n");
422
+ text += `\n${nested}`;
423
+ const lastNested = segment.blocks[segment.blocks.length - 1];
424
+ previousStyleId = lastNested === void 0 ? void 0 : lastStyleIdOfRegionItem(lastNested);
425
+ continue;
426
+ }
427
+ for (const block of segment.blocks) {
428
+ if (!renderedFirstLine) {
429
+ const [firstLine = "", ...restLines] = listRegionItemBody(block, context, stripGlyph).split("\n");
430
+ text = [`${marker.full}${firstLine}`, ...restLines.map((line) => `${indent}${line}`)].join("\n");
431
+ renderedFirstLine = true;
432
+ previousStyleId = lastStyleIdOfRegionItem(block);
433
+ continue;
434
+ }
435
+ const rendered = listRegionItemBody(block, context, false).split("\n").map((line) => line.length === 0 ? line : `${indent}${line}`).join("\n");
436
+ const blank = loose || requiresBlankLineBefore(block, previousStyleId, context);
437
+ text += blank ? `\n\n${rendered}` : `\n${rendered}`;
438
+ previousStyleId = lastStyleIdOfRegionItem(block);
439
+ }
178
440
  }
179
441
  parts.push({
180
442
  numId,
181
443
  text
182
444
  });
183
- index = lookahead;
445
+ index = next;
184
446
  }
185
447
  let out = "";
186
448
  for (const [partIndex, part] of parts.entries()) {
@@ -197,6 +459,12 @@ function renderListRegion(items, context) {
197
459
  function isConstructItem(item) {
198
460
  return "descriptor" in item;
199
461
  }
462
+ function isMaterialisedDivision(item) {
463
+ return item.descriptor.kind === "division" && item.children.every((child) => {
464
+ if (isConstructItem(child)) return true;
465
+ return child.block.kind !== "paragraph" || child.block.indentLeftPt !== void 0 && child.block.indentLeftPt >= 36;
466
+ });
467
+ }
200
468
  function groupConstructItems(blocks, start) {
201
469
  const items = [];
202
470
  let index = start;
@@ -233,9 +501,9 @@ function renderFootnoteDefinition(name, body) {
233
501
  return [`${marker} ${firstLine}`, ...restLines.map((line) => line.length === 0 ? line : `${indent}${line}`)].join("\n");
234
502
  }
235
503
  function renderConstruct(item, context) {
236
- const body = renderItems(item.children, context);
237
504
  const { descriptor } = item;
238
505
  if (descriptor.kind === "anchor" && descriptor.anchorType === "footnote") {
506
+ const body = renderItems(item.children, context);
239
507
  if (require_inline_footnote.isValidFootnoteLabel(descriptor.name)) return renderFootnoteDefinition(descriptor.name, body);
240
508
  context.sink({
241
509
  code: require_diagnostics_diagnostics.MarkdownDiagnosticCodes.CONSTRUCT_UNREPRESENTED,
@@ -244,6 +512,20 @@ function renderConstruct(item, context) {
244
512
  });
245
513
  return body;
246
514
  }
515
+ if (descriptor.kind === "division") {
516
+ if (isMaterialisedDivision(item)) {
517
+ context.divisionDepth += 1;
518
+ const body = renderItems(item.children, context);
519
+ context.divisionDepth -= 1;
520
+ return body.split("\n").map((line) => line.length === 0 ? ">" : `> ${line}`).join("\n");
521
+ }
522
+ }
523
+ if (descriptor.kind === "link" && descriptor.target.kind === "external") {
524
+ const onlyChild = item.children.length === 1 && !isConstructItem(item.children[0]) ? item.children[0].block : void 0;
525
+ if (onlyChild?.kind === "image" && !isDataUri(descriptor.target.uri)) return `![${require_emit_inline.escapeMarkdownText(onlyChild.altText ?? "")}](${require_emit_inline.escapeLinkDestination(descriptor.target.uri)}${descriptor.title === void 0 ? "" : ` "${require_emit_inline.renderLinkTitle(descriptor.title)}"`})`;
526
+ if (onlyChild?.kind === "image" && isDataUri(descriptor.target.uri) && !context.embedImages) return require_emit_image.emitImage(onlyChild, false);
527
+ }
528
+ const body = renderItems(item.children, context);
247
529
  const detail = descriptor.kind === "anchor" ? `${descriptor.kind} (${descriptor.anchorType})` : descriptor.kind;
248
530
  context.sink({
249
531
  code: require_diagnostics_diagnostics.MarkdownDiagnosticCodes.CONSTRUCT_UNREPRESENTED,
@@ -252,6 +534,18 @@ function renderConstruct(item, context) {
252
534
  });
253
535
  return body;
254
536
  }
537
+ function constructCarriesListItemId(item, itemId) {
538
+ return item.children.some((child) => {
539
+ if (isConstructItem(child)) return constructCarriesListItemId(child, itemId);
540
+ if (child.block.kind !== "paragraph") return false;
541
+ const list = child.block.list;
542
+ if (list?.itemId === itemId) return true;
543
+ return list?.numId !== void 0 && require_shared_list_id.parseListNumId(list.numId)?.ownerItemId === itemId;
544
+ });
545
+ }
546
+ function isInheritedListMembership(list, context) {
547
+ return list.itemId !== void 0 && list.itemId === context.enclosingItemId;
548
+ }
255
549
  function renderItems(items, context) {
256
550
  const parts = [];
257
551
  let index = 0;
@@ -264,11 +558,40 @@ function renderItems(items, context) {
264
558
  index += 1;
265
559
  continue;
266
560
  }
267
- if (item.block.kind === "paragraph" && item.block.list !== void 0) {
561
+ if (item.block.kind === "paragraph" && item.block.list !== void 0 && !isInheritedListMembership(item.block.list, context)) {
268
562
  const region = [];
269
563
  let end = index;
270
- for (let candidate = items[end]; candidate !== void 0 && !isConstructItem(candidate) && candidate.block.kind === "paragraph" && candidate.block.list !== void 0; candidate = items[end]) {
271
- region.push(candidate.block);
564
+ const openMemberships = [];
565
+ for (;;) {
566
+ const candidate = items[end];
567
+ if (candidate === void 0) break;
568
+ if (isConstructItem(candidate)) {
569
+ let matched;
570
+ for (let i = openMemberships.length - 1; i >= 0; i -= 1) {
571
+ const membership = openMemberships[i];
572
+ if (membership.itemId !== void 0 && constructCarriesListItemId(candidate, membership.itemId)) {
573
+ matched = membership;
574
+ break;
575
+ }
576
+ }
577
+ if (matched === void 0) break;
578
+ region.push({
579
+ kind: "construct",
580
+ item: candidate,
581
+ list: matched
582
+ });
583
+ end += 1;
584
+ continue;
585
+ }
586
+ if (candidate.block.kind !== "paragraph" || candidate.block.list === void 0 || isInheritedListMembership(candidate.block.list, context)) break;
587
+ const list = candidate.block.list;
588
+ while (openMemberships.length > 0 && openMemberships[openMemberships.length - 1].level >= list.level) openMemberships.pop();
589
+ openMemberships.push(list);
590
+ region.push({
591
+ kind: "paragraph",
592
+ block: candidate.block,
593
+ list
594
+ });
272
595
  end += 1;
273
596
  }
274
597
  parts.push(renderListRegion(region, context));
@@ -284,8 +607,18 @@ function renderItems(items, context) {
284
607
  function emitBlocks(blocks, context) {
285
608
  const imbalance = (0, document_schema_js.findConstructMarkerImbalance)(blocks);
286
609
  if (imbalance !== void 0) throw new require_diagnostics_diagnostics.MarkdownUnbalancedConstructMarkersError(imbalance.kind, imbalance.index);
610
+ validateRunConstructExtents(blocks);
287
611
  return renderItems(groupConstructItems(blocks, 0).items, context);
288
612
  }
613
+ function validateRunConstructExtents(blocks) {
614
+ for (const block of blocks) {
615
+ if (block.kind === "paragraph" && block.constructs !== void 0) {
616
+ const fault = (0, document_schema_js.findRunConstructFault)(block);
617
+ if (fault !== void 0) throw new require_diagnostics_diagnostics.MarkdownInvalidRunConstructExtentError(fault.kind, fault.index);
618
+ }
619
+ if (block.kind === "table") for (const row of block.rows) for (const cell of row.cells) validateRunConstructExtents(cell.blocks);
620
+ }
621
+ }
289
622
  function emitMarkdown(document, options = {}) {
290
623
  if (document.kind !== "wordprocessing") throw new require_diagnostics_diagnostics.MarkdownUnsupportedDocumentKindError(document.kind);
291
624
  const context = {
@@ -298,8 +631,11 @@ function emitMarkdown(document, options = {}) {
298
631
  headingStyle: options.headingStyle ?? "atx",
299
632
  embedImages: options.images ?? true,
300
633
  orderedCounters: /* @__PURE__ */ new Map(),
634
+ resolvedListGlyphs: /* @__PURE__ */ new Map(),
301
635
  reportedFallbackNumIds: /* @__PURE__ */ new Set(),
302
- reportedAbsentNumIdFallback: false
636
+ reportedAbsentNumIdFallback: false,
637
+ divisionDepth: 0,
638
+ enclosingItemId: void 0
303
639
  };
304
640
  const body = document.sections.map((section) => emitBlocks(section.blocks, context)).join("\n\n");
305
641
  const frontMatter = options.frontMatter === true ? require_emit_front_matter.emitFrontMatter(document.metadata) : void 0;