markdown-codec 0.0.0 → 1.0.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 (171) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +215 -0
  3. package/dist/ast/ast.cjs +25 -0
  4. package/dist/ast/ast.d.cts +2 -0
  5. package/dist/ast/ast.d.ts +2 -0
  6. package/dist/ast/ast.js +23 -0
  7. package/dist/ast-BXYwy08e.d.cts +160 -0
  8. package/dist/ast-BXYwy08e.d.ts +160 -0
  9. package/dist/block/block.cjs +530 -0
  10. package/dist/block/block.d.cts +18 -0
  11. package/dist/block/block.d.ts +18 -0
  12. package/dist/block/block.js +529 -0
  13. package/dist/block/definitions.cjs +59 -0
  14. package/dist/block/definitions.d.cts +6 -0
  15. package/dist/block/definitions.d.ts +6 -0
  16. package/dist/block/definitions.js +58 -0
  17. package/dist/block/line.cjs +76 -0
  18. package/dist/block/line.d.cts +28 -0
  19. package/dist/block/line.d.ts +28 -0
  20. package/dist/block/line.js +74 -0
  21. package/dist/block/list.cjs +110 -0
  22. package/dist/block/list.d.cts +8 -0
  23. package/dist/block/list.d.ts +8 -0
  24. package/dist/block/list.js +107 -0
  25. package/dist/block/node.cjs +68 -0
  26. package/dist/block/node.d.cts +45 -0
  27. package/dist/block/node.d.ts +45 -0
  28. package/dist/block/node.js +65 -0
  29. package/dist/block/table.cjs +64 -0
  30. package/dist/block/table.d.cts +7 -0
  31. package/dist/block/table.d.ts +7 -0
  32. package/dist/block/table.js +61 -0
  33. package/dist/codec.cjs +22 -0
  34. package/dist/codec.d.cts +492 -0
  35. package/dist/codec.d.ts +492 -0
  36. package/dist/codec.js +20 -0
  37. package/dist/defaults/defaults.cjs +31 -0
  38. package/dist/defaults/defaults.d.cts +16 -0
  39. package/dist/defaults/defaults.d.ts +16 -0
  40. package/dist/defaults/defaults.js +20 -0
  41. package/dist/diagnostics/diagnostics.cjs +85 -0
  42. package/dist/diagnostics/diagnostics.d.cts +2 -0
  43. package/dist/diagnostics/diagnostics.d.ts +2 -0
  44. package/dist/diagnostics/diagnostics.js +77 -0
  45. package/dist/diagnostics-DmuWc7d8.d.cts +61 -0
  46. package/dist/diagnostics-DmuWc7d8.d.ts +61 -0
  47. package/dist/emit/emit.cjs +228 -0
  48. package/dist/emit/emit.d.cts +6 -0
  49. package/dist/emit/emit.d.ts +6 -0
  50. package/dist/emit/emit.js +227 -0
  51. package/dist/emit/front-matter.cjs +24 -0
  52. package/dist/emit/front-matter.d.cts +5 -0
  53. package/dist/emit/front-matter.d.ts +5 -0
  54. package/dist/emit/front-matter.js +23 -0
  55. package/dist/emit/image.cjs +10 -0
  56. package/dist/emit/image.d.cts +5 -0
  57. package/dist/emit/image.d.ts +5 -0
  58. package/dist/emit/image.js +9 -0
  59. package/dist/emit/inline.cjs +183 -0
  60. package/dist/emit/inline.d.cts +12 -0
  61. package/dist/emit/inline.d.ts +12 -0
  62. package/dist/emit/inline.js +180 -0
  63. package/dist/emit/table.cjs +73 -0
  64. package/dist/emit/table.d.cts +10 -0
  65. package/dist/emit/table.d.ts +10 -0
  66. package/dist/emit/table.js +72 -0
  67. package/dist/html/html.cjs +116 -0
  68. package/dist/html/html.d.cts +2 -0
  69. package/dist/html/html.d.ts +2 -0
  70. package/dist/html/html.js +113 -0
  71. package/dist/html/render.cjs +193 -0
  72. package/dist/html/render.d.cts +8 -0
  73. package/dist/html/render.d.ts +8 -0
  74. package/dist/html/render.js +189 -0
  75. package/dist/html-bkz2QTuq.d.cts +7 -0
  76. package/dist/html-bkz2QTuq.d.ts +7 -0
  77. package/dist/image/image.cjs +129 -0
  78. package/dist/image/image.d.cts +2 -0
  79. package/dist/image/image.d.ts +2 -0
  80. package/dist/image/image.js +125 -0
  81. package/dist/image-C4KYmz_L.d.cts +19 -0
  82. package/dist/image-Cm3hT5PS.d.ts +19 -0
  83. package/dist/image-DescdRgq.d.cts +12 -0
  84. package/dist/image-DescdRgq.d.ts +12 -0
  85. package/dist/index.cjs +17 -0
  86. package/dist/index.d.cts +7 -0
  87. package/dist/index.d.ts +7 -0
  88. package/dist/index.js +5 -0
  89. package/dist/inline/chars.cjs +50 -0
  90. package/dist/inline/chars.d.cts +11 -0
  91. package/dist/inline/chars.d.ts +11 -0
  92. package/dist/inline/chars.js +42 -0
  93. package/dist/inline/delimiter.cjs +136 -0
  94. package/dist/inline/delimiter.d.cts +29 -0
  95. package/dist/inline/delimiter.d.ts +29 -0
  96. package/dist/inline/delimiter.js +132 -0
  97. package/dist/inline/entity.cjs +67 -0
  98. package/dist/inline/entity.d.cts +9 -0
  99. package/dist/inline/entity.d.ts +9 -0
  100. package/dist/inline/entity.js +65 -0
  101. package/dist/inline/gfm-autolink.cjs +156 -0
  102. package/dist/inline/gfm-autolink.d.cts +5 -0
  103. package/dist/inline/gfm-autolink.d.ts +5 -0
  104. package/dist/inline/gfm-autolink.js +155 -0
  105. package/dist/inline/inline.cjs +457 -0
  106. package/dist/inline/inline.d.cts +2 -0
  107. package/dist/inline/inline.d.ts +2 -0
  108. package/dist/inline/inline.js +456 -0
  109. package/dist/inline/link.cjs +130 -0
  110. package/dist/inline/link.d.cts +2 -0
  111. package/dist/inline/link.d.ts +2 -0
  112. package/dist/inline/link.js +124 -0
  113. package/dist/inline/node.cjs +57 -0
  114. package/dist/inline/node.d.cts +23 -0
  115. package/dist/inline/node.d.ts +23 -0
  116. package/dist/inline/node.js +55 -0
  117. package/dist/inline-B50cM6cn.d.ts +10 -0
  118. package/dist/inline-C9YEGDV5.d.cts +10 -0
  119. package/dist/link-Dv4kxVjk.d.cts +18 -0
  120. package/dist/link-Dv4kxVjk.d.ts +18 -0
  121. package/dist/lower/front-matter.cjs +75 -0
  122. package/dist/lower/front-matter.d.cts +10 -0
  123. package/dist/lower/front-matter.d.ts +10 -0
  124. package/dist/lower/front-matter.js +74 -0
  125. package/dist/lower/image.cjs +29 -0
  126. package/dist/lower/image.d.cts +2 -0
  127. package/dist/lower/image.d.ts +2 -0
  128. package/dist/lower/image.js +28 -0
  129. package/dist/lower/inline.cjs +96 -0
  130. package/dist/lower/inline.d.cts +19 -0
  131. package/dist/lower/inline.d.ts +19 -0
  132. package/dist/lower/inline.js +93 -0
  133. package/dist/lower/lower.cjs +293 -0
  134. package/dist/lower/lower.d.cts +8 -0
  135. package/dist/lower/lower.d.ts +8 -0
  136. package/dist/lower/lower.js +291 -0
  137. package/dist/lower/table.cjs +27 -0
  138. package/dist/lower/table.d.cts +7 -0
  139. package/dist/lower/table.d.ts +7 -0
  140. package/dist/lower/table.js +26 -0
  141. package/dist/options/options.cjs +0 -0
  142. package/dist/options/options.d.cts +43 -0
  143. package/dist/options/options.d.ts +43 -0
  144. package/dist/options/options.js +0 -0
  145. package/dist/read.cjs +20 -0
  146. package/dist/read.d.cts +11 -0
  147. package/dist/read.d.ts +11 -0
  148. package/dist/read.js +19 -0
  149. package/dist/scan/entity-table.cjs +2131 -0
  150. package/dist/scan/entity-table.d.cts +4 -0
  151. package/dist/scan/entity-table.d.ts +4 -0
  152. package/dist/scan/entity-table.js +2130 -0
  153. package/dist/scan/scan.cjs +80 -0
  154. package/dist/scan/scan.d.cts +2 -0
  155. package/dist/scan/scan.d.ts +2 -0
  156. package/dist/scan/scan.js +78 -0
  157. package/dist/scan-DeBxgG-r.d.cts +30 -0
  158. package/dist/scan-DeBxgG-r.d.ts +30 -0
  159. package/dist/shared/list-id.cjs +36 -0
  160. package/dist/shared/list-id.d.cts +22 -0
  161. package/dist/shared/list-id.d.ts +22 -0
  162. package/dist/shared/list-id.js +32 -0
  163. package/dist/shared/style-constants.cjs +34 -0
  164. package/dist/shared/style-constants.d.cts +14 -0
  165. package/dist/shared/style-constants.d.ts +14 -0
  166. package/dist/shared/style-constants.js +23 -0
  167. package/dist/write.cjs +9 -0
  168. package/dist/write.d.cts +6 -0
  169. package/dist/write.d.ts +6 -0
  170. package/dist/write.js +8 -0
  171. package/package.json +92 -2
@@ -0,0 +1,530 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ require("../defaults/defaults.cjs");
3
+ const require_diagnostics_diagnostics = require("../diagnostics/diagnostics.cjs");
4
+ const require_html_html = require("../html/html.cjs");
5
+ const require_inline_entity = require("../inline/entity.cjs");
6
+ const require_inline_inline = require("../inline/inline.cjs");
7
+ const require_block_definitions = require("./definitions.cjs");
8
+ const require_block_line = require("./line.cjs");
9
+ const require_block_list = require("./list.cjs");
10
+ const require_block_node = require("./node.cjs");
11
+ const require_block_table = require("./table.cjs");
12
+ //#region src/block/block.ts
13
+ const TASK_LIST_MARKER_PATTERN = /^\[([ xX])\][ \t]/;
14
+ const NUL_REPLACEMENT = "�";
15
+ const NUL_PATTERN = /\0/g;
16
+ const LINE_ENDING_PATTERN = /\r\n|\n|\r/;
17
+ const MAYBE_SPECIAL_PATTERN = /^[#`~*+_=<>0-9|:-]/;
18
+ const ATX_MARKER_PATTERN = /^#{1,6}(?:[ \t]+|$)/;
19
+ const ATX_ONLY_CLOSING_SEQUENCE_PATTERN = /^[ \t]*#+[ \t]*$/;
20
+ const ATX_TRAILING_CLOSING_SEQUENCE_PATTERN = /[ \t]+#+[ \t]*$/;
21
+ const CODE_FENCE_PATTERN = /^`{3,}(?!.*`)|^~{3,}/;
22
+ const CLOSING_CODE_FENCE_PATTERN = /^(?:`{3,}|~{3,})(?=[ \t]*$)/;
23
+ const SETEXT_UNDERLINE_PATTERN = /^(?:=+|-+)[ \t]*$/;
24
+ const THEMATIC_BREAK_PATTERN = /^(?:\*[ \t]*){3,}$|^(?:_[ \t]*){3,}$|^(?:-[ \t]*){3,}$/;
25
+ const BLANK_CONTENT_PATTERN = /^[ \t\n]*$/;
26
+ const TRAILING_BLANK_LINES_PATTERN = /(?:\n[ \t]*)+$/;
27
+ const TRAILING_HTML_BLANK_LINES_PATTERN = /(?:\n *)+$/;
28
+ const MAX_HEADING_LEVEL = 6;
29
+ const SETEXT_LEVEL_1 = 1;
30
+ const SETEXT_LEVEL_2 = 2;
31
+ const HTML_BLOCK_BLANK_LINE_END_TYPES = [6, 7];
32
+ function isBlankContent(content) {
33
+ return BLANK_CONTENT_PATTERN.test(content);
34
+ }
35
+ function headingLevelOf(hashes) {
36
+ switch (hashes) {
37
+ case 1: return 1;
38
+ case 2: return 2;
39
+ case 3: return 3;
40
+ case 4: return 4;
41
+ case 5: return 5;
42
+ default: return MAX_HEADING_LEVEL;
43
+ }
44
+ }
45
+ var BlockParser = class {
46
+ references = /* @__PURE__ */ new Map();
47
+ document = new require_block_node.BlockNode("document", 1);
48
+ tables;
49
+ sink;
50
+ maxNesting;
51
+ tip = this.document;
52
+ oldTip = this.document;
53
+ lastMatchedContainer = this.document;
54
+ allClosed = true;
55
+ line = new require_block_line.LineCursor("");
56
+ lineNumber = 0;
57
+ nestingDepth = 0;
58
+ constructor(options) {
59
+ this.tables = options.gfmTables ?? true;
60
+ this.sink = options.sink ?? require_diagnostics_diagnostics.NOOP_MARKDOWN_DIAGNOSTIC_SINK;
61
+ this.maxNesting = options.maxNesting ?? 250;
62
+ }
63
+ parse(source) {
64
+ const lines = source.split(LINE_ENDING_PATTERN);
65
+ const count = source.endsWith("\n") || source.endsWith("\r") ? lines.length - 1 : lines.length;
66
+ for (let index = 0; index < count; index += 1) {
67
+ const text = lines[index];
68
+ if (text !== void 0) this.incorporateLine(text);
69
+ }
70
+ while (this.tip.open) {
71
+ this.reportUnterminatedAtEof(this.tip);
72
+ this.finalize(this.tip);
73
+ }
74
+ return this.document;
75
+ }
76
+ reportUnterminatedAtEof(node) {
77
+ if (node.kind === "codeBlock" && node.fenced) {
78
+ this.sink({
79
+ code: require_diagnostics_diagnostics.MarkdownDiagnosticCodes.UNCLOSED_FENCE,
80
+ severity: "warning",
81
+ message: `fenced code block starting at line ${String(node.startLine)} was never closed by a matching closing fence before the end of the document`,
82
+ line: node.startLine
83
+ });
84
+ return;
85
+ }
86
+ if (node.kind === "htmlBlock" && !HTML_BLOCK_BLANK_LINE_END_TYPES.includes(node.htmlBlockType)) this.sink({
87
+ code: require_diagnostics_diagnostics.MarkdownDiagnosticCodes.UNTERMINATED_HTML_BLOCK,
88
+ severity: "warning",
89
+ message: `HTML block (type ${String(node.htmlBlockType)}) starting at line ${String(node.startLine)} never met its own end condition before the end of the document`,
90
+ line: node.startLine
91
+ });
92
+ }
93
+ incorporateLine(rawText) {
94
+ this.lineNumber += 1;
95
+ this.line = new require_block_line.LineCursor(rawText.replace(NUL_PATTERN, NUL_REPLACEMENT));
96
+ this.oldTip = this.tip;
97
+ const matched = this.walkOpenBlocks();
98
+ if (matched === void 0) return;
99
+ this.allClosed = matched === this.oldTip;
100
+ this.lastMatchedContainer = matched;
101
+ this.addTextToContainer(this.openNewBlocks(matched));
102
+ }
103
+ walkOpenBlocks() {
104
+ let container = this.document;
105
+ for (;;) {
106
+ const lastChild = container.lastChild;
107
+ if (lastChild?.open !== true) return container;
108
+ this.line.findNextNonspace();
109
+ const result = this.continueBlock(lastChild);
110
+ if (result === "finished") return;
111
+ if (result === "not-matched") return container;
112
+ container = lastChild;
113
+ }
114
+ }
115
+ continueBlock(node) {
116
+ switch (node.kind) {
117
+ case "document":
118
+ case "list": return "matched";
119
+ case "blockquote": return this.continueBlockquote();
120
+ case "listItem": return this.continueListItem(node);
121
+ case "codeBlock": return this.continueCodeBlock(node);
122
+ case "htmlBlock": return this.line.blank && HTML_BLOCK_BLANK_LINE_END_TYPES.includes(node.htmlBlockType) ? "not-matched" : "matched";
123
+ case "paragraph":
124
+ case "table": return this.line.blank ? "not-matched" : "matched";
125
+ case "heading":
126
+ case "thematicBreak": return "not-matched";
127
+ }
128
+ }
129
+ continueBlockquote() {
130
+ if (!this.consumeBlockquoteMarker()) return "not-matched";
131
+ return "matched";
132
+ }
133
+ consumeBlockquoteMarker() {
134
+ if (this.line.indented || this.line.peekNextNonspace() !== ">") return false;
135
+ this.line.advanceToNextNonspace();
136
+ this.line.advance(1);
137
+ if (this.line.peek() === " ") this.line.advance(1);
138
+ return true;
139
+ }
140
+ continueListItem(node) {
141
+ const listData = node.listData;
142
+ if (listData === void 0) return "not-matched";
143
+ if (this.line.blank) {
144
+ if (node.children.length === 0) return "not-matched";
145
+ this.line.advanceToNextNonspace();
146
+ return "matched";
147
+ }
148
+ if (this.line.indent >= listData.markerOffset + listData.padding) {
149
+ this.line.advance(listData.markerOffset + listData.padding);
150
+ return "matched";
151
+ }
152
+ return "not-matched";
153
+ }
154
+ continueCodeBlock(node) {
155
+ if (!node.fenced) {
156
+ if (this.line.indent >= 4) {
157
+ this.line.advance(4);
158
+ return "matched";
159
+ }
160
+ if (this.line.blank) {
161
+ this.line.advanceToNextNonspace();
162
+ return "matched";
163
+ }
164
+ return "not-matched";
165
+ }
166
+ const rest = this.line.restFromNextNonspace();
167
+ const closing = this.line.indented || !rest.startsWith(node.fenceChar) ? null : CLOSING_CODE_FENCE_PATTERN.exec(rest);
168
+ if (closing !== null && closing[0].length >= node.fenceLength) {
169
+ this.finalize(node);
170
+ return "finished";
171
+ }
172
+ for (let remaining = node.fenceOffset; remaining > 0 && this.line.peek() === " "; remaining -= 1) this.line.advance(1);
173
+ return "matched";
174
+ }
175
+ openNewBlocks(matchedContainer) {
176
+ let container = matchedContainer;
177
+ let matchedLeaf = container.kind !== "paragraph" && container.kind !== "table" && require_block_node.acceptsLines(container.kind);
178
+ while (!matchedLeaf) {
179
+ this.line.findNextNonspace();
180
+ if (!this.line.indented && !MAYBE_SPECIAL_PATTERN.test(this.line.restFromNextNonspace())) {
181
+ this.line.advanceToNextNonspace();
182
+ break;
183
+ }
184
+ const result = this.tryBlockStart(container);
185
+ if (result === "none") {
186
+ this.line.advanceToNextNonspace();
187
+ break;
188
+ }
189
+ container = this.tip;
190
+ matchedLeaf = result === "leaf";
191
+ }
192
+ return container;
193
+ }
194
+ tryBlockStart(container) {
195
+ const starts = [
196
+ () => this.tryBlockquoteStart(),
197
+ () => this.tryAtxHeadingStart(),
198
+ () => this.tryCodeFenceStart(),
199
+ () => this.tryHtmlBlockStart(container),
200
+ () => this.tryPromoteParagraph(container),
201
+ () => this.tryThematicBreakStart(),
202
+ () => this.tryListItemStart(container),
203
+ () => this.tryIndentedCodeStart()
204
+ ];
205
+ for (const start of starts) {
206
+ const result = start();
207
+ if (result !== "none") return result;
208
+ }
209
+ return "none";
210
+ }
211
+ tryBlockquoteStart() {
212
+ if (!this.consumeBlockquoteMarker()) return "none";
213
+ this.closeUnmatchedBlocks();
214
+ this.addChild("blockquote");
215
+ return "container";
216
+ }
217
+ tryAtxHeadingStart() {
218
+ if (this.line.indented) return "none";
219
+ const rest = this.line.restFromNextNonspace();
220
+ const match = ATX_MARKER_PATTERN.exec(rest);
221
+ if (match === null) return "none";
222
+ this.line.advanceToNextNonspace();
223
+ this.closeUnmatchedBlocks();
224
+ const heading = this.addChild("heading");
225
+ heading.level = headingLevelOf(match[0].trim().length);
226
+ heading.content = rest.slice(match[0].length).replace(ATX_ONLY_CLOSING_SEQUENCE_PATTERN, "").replace(ATX_TRAILING_CLOSING_SEQUENCE_PATTERN, "");
227
+ this.line.advanceToEndOfLine();
228
+ return "leaf";
229
+ }
230
+ tryCodeFenceStart() {
231
+ if (this.line.indented) return "none";
232
+ const match = CODE_FENCE_PATTERN.exec(this.line.restFromNextNonspace());
233
+ if (match === null) return "none";
234
+ const fence = match[0];
235
+ const fenceChar = fence.charAt(0);
236
+ if (fenceChar !== "`" && fenceChar !== "~") return "none";
237
+ this.closeUnmatchedBlocks();
238
+ const block = this.addChild("codeBlock");
239
+ block.fenced = true;
240
+ block.fenceChar = fenceChar;
241
+ block.fenceLength = fence.length;
242
+ block.fenceOffset = this.line.indent;
243
+ this.line.advanceToNextNonspace();
244
+ this.line.advance(fence.length);
245
+ return "leaf";
246
+ }
247
+ tryHtmlBlockStart(container) {
248
+ if (this.line.indented || this.line.peekNextNonspace() !== "<") return "none";
249
+ const interruptsParagraph = container.kind === "paragraph" || !this.allClosed && !this.line.blank && this.tip.kind === "paragraph";
250
+ const type = require_html_html.matchHtmlBlockStart(this.line.restFromNextNonspace(), interruptsParagraph);
251
+ if (type === void 0) return "none";
252
+ this.closeUnmatchedBlocks();
253
+ this.addChild("htmlBlock").htmlBlockType = type;
254
+ return "leaf";
255
+ }
256
+ tryPromoteParagraph(container) {
257
+ if (this.line.indented || container.kind !== "paragraph") return "none";
258
+ const setext = this.trySetextHeading(container);
259
+ return setext === "none" ? this.tryTableHeader(container) : setext;
260
+ }
261
+ trySetextHeading(paragraph) {
262
+ const match = SETEXT_UNDERLINE_PATTERN.exec(this.line.restFromNextNonspace());
263
+ if (match === null) return "none";
264
+ this.closeUnmatchedBlocks();
265
+ paragraph.content = require_block_definitions.extractDefinitions(paragraph.content, this.references, this.sink, paragraph.startLine);
266
+ if (isBlankContent(paragraph.content)) return "none";
267
+ const heading = new require_block_node.BlockNode("heading", paragraph.startLine);
268
+ heading.level = match[0].startsWith("=") ? SETEXT_LEVEL_1 : SETEXT_LEVEL_2;
269
+ heading.setext = true;
270
+ heading.content = paragraph.content;
271
+ paragraph.replaceWith(heading);
272
+ this.tip = heading;
273
+ this.line.advanceToEndOfLine();
274
+ return "leaf";
275
+ }
276
+ tryTableHeader(paragraph) {
277
+ if (!this.tables) return "none";
278
+ const alignments = require_block_table.parseTableDelimiterRow(this.line.restFromNextNonspace());
279
+ if (alignments === void 0) return "none";
280
+ const lines = paragraph.content.split("\n");
281
+ const headerLine = lines.at(-2);
282
+ if (headerLine === void 0 || require_block_table.splitTableRow(headerLine).length !== alignments.length) return "none";
283
+ this.closeUnmatchedBlocks();
284
+ paragraph.content = lines.slice(0, -2).map((text) => `${text}\n`).join("");
285
+ this.finalize(paragraph);
286
+ const table = this.addChild("table");
287
+ table.alignments = alignments;
288
+ table.headerLine = headerLine;
289
+ this.line.advanceToEndOfLine();
290
+ return "leaf";
291
+ }
292
+ tryThematicBreakStart() {
293
+ if (this.line.indented || !THEMATIC_BREAK_PATTERN.test(this.line.restFromNextNonspace())) return "none";
294
+ this.closeUnmatchedBlocks();
295
+ this.addChild("thematicBreak");
296
+ this.line.advanceToEndOfLine();
297
+ return "leaf";
298
+ }
299
+ tryListItemStart(container) {
300
+ const data = require_block_list.parseListMarker(this.line, container.kind === "paragraph");
301
+ if (data === void 0) return "none";
302
+ this.closeUnmatchedBlocks();
303
+ const openList = this.tip.listData;
304
+ if (this.tip.kind !== "list" || openList === void 0 || !require_block_list.listsMatch(openList, data)) this.addChild("list").listData = data;
305
+ this.addChild("listItem").listData = data;
306
+ return "container";
307
+ }
308
+ tryIndentedCodeStart() {
309
+ if (!this.line.indented || this.tip.kind === "paragraph" || this.line.blank) return "none";
310
+ this.line.advance(4);
311
+ this.closeUnmatchedBlocks();
312
+ this.addChild("codeBlock");
313
+ return "leaf";
314
+ }
315
+ addTextToContainer(container) {
316
+ if (!this.allClosed && !this.line.blank && this.tip.kind === "paragraph") {
317
+ this.addLine();
318
+ return;
319
+ }
320
+ this.closeUnmatchedBlocks();
321
+ const lastChild = container.lastChild;
322
+ if (this.line.blank && lastChild !== void 0) lastChild.lastLineBlank = true;
323
+ this.recordBlankLineForTightness(container);
324
+ if (require_block_node.acceptsLines(container.kind)) {
325
+ this.addLine();
326
+ if (container.kind === "htmlBlock" && require_html_html.matchesHtmlBlockEnd(this.line.rest(), container.htmlBlockType)) this.finalize(container);
327
+ return;
328
+ }
329
+ if (!this.line.atEnd && !this.line.blank) {
330
+ this.addChild("paragraph");
331
+ this.line.advanceToNextNonspace();
332
+ this.addLine();
333
+ }
334
+ }
335
+ recordBlankLineForTightness(container) {
336
+ const blank = this.line.blank && !(container.kind === "blockquote" || container.kind === "codeBlock" && container.fenced || container.kind === "listItem" && container.children.length === 0 && container.startLine === this.lineNumber);
337
+ for (let node = container; node !== void 0; node = node.parent) node.lastLineBlank = blank;
338
+ }
339
+ addLine() {
340
+ this.tip.content += `${this.line.rest()}\n`;
341
+ }
342
+ addChild(kind) {
343
+ while (!require_block_node.canContain(this.tip.kind, kind)) this.finalize(this.tip);
344
+ if (this.nestingDepth >= this.maxNesting) throw new require_diagnostics_diagnostics.MarkdownNestingLimitExceededError(this.maxNesting);
345
+ const node = new require_block_node.BlockNode(kind, this.lineNumber);
346
+ this.tip.appendChild(node);
347
+ this.tip = node;
348
+ this.nestingDepth += 1;
349
+ return node;
350
+ }
351
+ closeUnmatchedBlocks() {
352
+ if (this.allClosed) return;
353
+ while (this.oldTip !== this.lastMatchedContainer) {
354
+ const parent = this.oldTip.parent;
355
+ this.finalize(this.oldTip);
356
+ if (parent === void 0) break;
357
+ this.oldTip = parent;
358
+ }
359
+ this.allClosed = true;
360
+ }
361
+ finalize(node) {
362
+ const above = node.parent;
363
+ node.open = false;
364
+ this.finalizeContent(node);
365
+ if (node !== this.document) this.nestingDepth -= 1;
366
+ this.tip = above ?? this.document;
367
+ }
368
+ finalizeContent(node) {
369
+ switch (node.kind) {
370
+ case "paragraph":
371
+ node.content = require_block_definitions.extractDefinitions(node.content, this.references, this.sink, node.startLine);
372
+ if (isBlankContent(node.content)) node.unlink();
373
+ return;
374
+ case "codeBlock":
375
+ this.finalizeCodeBlock(node);
376
+ return;
377
+ case "htmlBlock":
378
+ node.literal = node.content.replace(TRAILING_HTML_BLANK_LINES_PATTERN, "");
379
+ return;
380
+ case "list":
381
+ require_block_list.finalizeListTightness(node);
382
+ return;
383
+ default: return;
384
+ }
385
+ }
386
+ finalizeCodeBlock(node) {
387
+ if (!node.fenced) {
388
+ node.literal = node.content.replace(TRAILING_BLANK_LINES_PATTERN, "\n");
389
+ return;
390
+ }
391
+ const breakIndex = node.content.indexOf("\n");
392
+ node.infoString = require_inline_entity.unescapeString(node.content.slice(0, breakIndex).trim());
393
+ node.literal = node.content.slice(breakIndex + 1);
394
+ }
395
+ };
396
+ function toInlineChildren(content, references, options) {
397
+ return require_inline_inline.parseInlines(content.trim(), references, options);
398
+ }
399
+ function toHeadingNode(node, references, options) {
400
+ return {
401
+ type: "heading",
402
+ level: node.level,
403
+ style: node.setext ? "setext" : "atx",
404
+ children: toInlineChildren(node.content, references, options)
405
+ };
406
+ }
407
+ function extractTaskListMarker(itemChildren) {
408
+ const first = itemChildren[0];
409
+ if (first?.kind !== "paragraph") return;
410
+ const match = TASK_LIST_MARKER_PATTERN.exec(first.content);
411
+ if (match === null) return;
412
+ first.content = first.content.slice(match[0].length);
413
+ return match[1] !== " ";
414
+ }
415
+ function toListItemNode(item, references, options) {
416
+ const checked = options.gfmTaskLists ?? true ? extractTaskListMarker(item.children) : void 0;
417
+ return checked === void 0 ? {
418
+ type: "listItem",
419
+ children: toAstBlocks(item.children, references, options)
420
+ } : {
421
+ type: "listItem",
422
+ checked,
423
+ children: toAstBlocks(item.children, references, options)
424
+ };
425
+ }
426
+ function toListNode(node, references, options) {
427
+ const children = node.children.map((item) => toListItemNode(item, references, options));
428
+ const data = node.listData;
429
+ if (data?.type === "ordered") return {
430
+ type: "list",
431
+ markerType: "ordered",
432
+ orderedDelimiter: data.delimiter,
433
+ start: data.start,
434
+ tight: node.tight,
435
+ children
436
+ };
437
+ return {
438
+ type: "list",
439
+ markerType: "bullet",
440
+ bulletMarker: data?.bulletChar,
441
+ tight: node.tight,
442
+ children
443
+ };
444
+ }
445
+ function toTableRow(cells, header, references, options) {
446
+ return {
447
+ type: "tableRow",
448
+ header,
449
+ children: cells.map((cell) => ({
450
+ type: "tableCell",
451
+ children: toInlineChildren(cell, references, options)
452
+ }))
453
+ };
454
+ }
455
+ function toTableNode(node, references, options) {
456
+ const sink = options.sink ?? require_diagnostics_diagnostics.NOOP_MARKDOWN_DIAGNOSTIC_SINK;
457
+ const columnCount = node.alignments.length;
458
+ const rows = [toTableRow(require_block_table.fitRowToColumns(require_block_table.splitTableRow(node.headerLine), columnCount), true, references, options)];
459
+ for (const rowLine of node.content.split("\n")) {
460
+ if (rowLine.trim().length === 0) continue;
461
+ const cells = require_block_table.splitTableRow(rowLine);
462
+ if (cells.length !== columnCount) sink({
463
+ code: require_diagnostics_diagnostics.MarkdownDiagnosticCodes.TABLE_CELL_COUNT_MISMATCH,
464
+ severity: "warning",
465
+ message: `table row has ${String(cells.length)} cell(s), but the header row declares ${String(columnCount)}; the row is padded with empty cells or truncated to fit`,
466
+ line: node.startLine
467
+ });
468
+ rows.push(toTableRow(require_block_table.fitRowToColumns(cells, columnCount), false, references, options));
469
+ }
470
+ return {
471
+ type: "table",
472
+ alignments: node.alignments,
473
+ children: rows
474
+ };
475
+ }
476
+ function toAstBlock(node, references, options) {
477
+ switch (node.kind) {
478
+ case "paragraph": return {
479
+ type: "paragraph",
480
+ children: toInlineChildren(node.content, references, options)
481
+ };
482
+ case "heading": return toHeadingNode(node, references, options);
483
+ case "blockquote": return {
484
+ type: "blockquote",
485
+ children: toAstBlocks(node.children, references, options)
486
+ };
487
+ case "list": return toListNode(node, references, options);
488
+ case "codeBlock": return node.fenced ? {
489
+ type: "codeBlock",
490
+ fenced: true,
491
+ fenceChar: node.fenceChar,
492
+ infoString: node.infoString,
493
+ literal: node.literal
494
+ } : {
495
+ type: "codeBlock",
496
+ fenced: false,
497
+ literal: node.literal
498
+ };
499
+ case "htmlBlock": return {
500
+ type: "htmlBlock",
501
+ literal: node.literal
502
+ };
503
+ case "thematicBreak": return { type: "thematicBreak" };
504
+ case "table": return toTableNode(node, references, options);
505
+ case "document":
506
+ case "listItem": return;
507
+ }
508
+ }
509
+ function toAstBlocks(nodes, references, options) {
510
+ const blocks = [];
511
+ for (const node of nodes) {
512
+ const converted = toAstBlock(node, references, options);
513
+ if (converted !== void 0) blocks.push(converted);
514
+ }
515
+ return blocks;
516
+ }
517
+ function parseMarkdown(source, options = {}) {
518
+ const parser = new BlockParser(options);
519
+ const root = parser.parse(source);
520
+ const references = parser.references;
521
+ return {
522
+ document: {
523
+ type: "document",
524
+ children: toAstBlocks(root.children, references, options)
525
+ },
526
+ references
527
+ };
528
+ }
529
+ //#endregion
530
+ exports.parseMarkdown = parseMarkdown;
@@ -0,0 +1,18 @@
1
+ import { s as MarkdownDocumentNode } from "../ast-BXYwy08e.cjs";
2
+ import { i as MarkdownDiagnosticSink } from "../diagnostics-DmuWc7d8.cjs";
3
+ import { n as LinkReferenceMap } from "../link-Dv4kxVjk.cjs";
4
+ import { t as InlineParseOptions } from "../inline-C9YEGDV5.cjs";
5
+ //#region src/block/block.d.ts
6
+ interface MarkdownParseOptions extends InlineParseOptions {
7
+ readonly gfmTables?: boolean;
8
+ readonly gfmTaskLists?: boolean;
9
+ readonly maxNesting?: number;
10
+ readonly sink?: MarkdownDiagnosticSink;
11
+ }
12
+ interface ParsedMarkdown {
13
+ readonly document: MarkdownDocumentNode;
14
+ readonly references: LinkReferenceMap;
15
+ }
16
+ declare function parseMarkdown(source: string, options?: MarkdownParseOptions): ParsedMarkdown;
17
+ //#endregion
18
+ export { MarkdownParseOptions, ParsedMarkdown, parseMarkdown };
@@ -0,0 +1,18 @@
1
+ import { s as MarkdownDocumentNode } from "../ast-BXYwy08e.js";
2
+ import { i as MarkdownDiagnosticSink } from "../diagnostics-DmuWc7d8.js";
3
+ import { n as LinkReferenceMap } from "../link-Dv4kxVjk.js";
4
+ import { t as InlineParseOptions } from "../inline-B50cM6cn.js";
5
+ //#region src/block/block.d.ts
6
+ interface MarkdownParseOptions extends InlineParseOptions {
7
+ readonly gfmTables?: boolean;
8
+ readonly gfmTaskLists?: boolean;
9
+ readonly maxNesting?: number;
10
+ readonly sink?: MarkdownDiagnosticSink;
11
+ }
12
+ interface ParsedMarkdown {
13
+ readonly document: MarkdownDocumentNode;
14
+ readonly references: LinkReferenceMap;
15
+ }
16
+ declare function parseMarkdown(source: string, options?: MarkdownParseOptions): ParsedMarkdown;
17
+ //#endregion
18
+ export { MarkdownParseOptions, ParsedMarkdown, parseMarkdown };