toolcraft 0.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 (135) hide show
  1. package/README.md +91 -0
  2. package/dist/cli.compile-check.d.ts +1 -0
  3. package/dist/cli.compile-check.js +26 -0
  4. package/dist/cli.d.ts +12 -0
  5. package/dist/cli.js +1312 -0
  6. package/dist/index.compile-check.d.ts +1 -0
  7. package/dist/index.compile-check.js +50 -0
  8. package/dist/index.d.ts +164 -0
  9. package/dist/index.js +366 -0
  10. package/dist/mcp.compile-check.d.ts +1 -0
  11. package/dist/mcp.compile-check.js +26 -0
  12. package/dist/mcp.d.ts +31 -0
  13. package/dist/mcp.js +354 -0
  14. package/dist/number-schema.d.ts +3 -0
  15. package/dist/number-schema.js +8 -0
  16. package/dist/renderer.d.ts +5 -0
  17. package/dist/renderer.js +148 -0
  18. package/dist/schema-scope.d.ts +4 -0
  19. package/dist/schema-scope.js +34 -0
  20. package/dist/sdk.compile-check.d.ts +1 -0
  21. package/dist/sdk.compile-check.js +79 -0
  22. package/dist/sdk.d.ts +63 -0
  23. package/dist/sdk.js +218 -0
  24. package/node_modules/@poe-code/design-system/dist/acp/components.d.ts +11 -0
  25. package/node_modules/@poe-code/design-system/dist/acp/components.js +121 -0
  26. package/node_modules/@poe-code/design-system/dist/acp/index.d.ts +3 -0
  27. package/node_modules/@poe-code/design-system/dist/acp/index.js +2 -0
  28. package/node_modules/@poe-code/design-system/dist/acp/writer.d.ts +13 -0
  29. package/node_modules/@poe-code/design-system/dist/acp/writer.js +21 -0
  30. package/node_modules/@poe-code/design-system/dist/components/command-errors.d.ts +16 -0
  31. package/node_modules/@poe-code/design-system/dist/components/command-errors.js +22 -0
  32. package/node_modules/@poe-code/design-system/dist/components/help-formatter.d.ts +20 -0
  33. package/node_modules/@poe-code/design-system/dist/components/help-formatter.js +27 -0
  34. package/node_modules/@poe-code/design-system/dist/components/index.d.ts +10 -0
  35. package/node_modules/@poe-code/design-system/dist/components/index.js +7 -0
  36. package/node_modules/@poe-code/design-system/dist/components/logger.d.ts +11 -0
  37. package/node_modules/@poe-code/design-system/dist/components/logger.js +60 -0
  38. package/node_modules/@poe-code/design-system/dist/components/symbols.d.ts +12 -0
  39. package/node_modules/@poe-code/design-system/dist/components/symbols.js +71 -0
  40. package/node_modules/@poe-code/design-system/dist/components/table.d.ts +13 -0
  41. package/node_modules/@poe-code/design-system/dist/components/table.js +74 -0
  42. package/node_modules/@poe-code/design-system/dist/components/text.d.ts +14 -0
  43. package/node_modules/@poe-code/design-system/dist/components/text.js +104 -0
  44. package/node_modules/@poe-code/design-system/dist/dashboard/ansi.d.ts +18 -0
  45. package/node_modules/@poe-code/design-system/dist/dashboard/ansi.js +298 -0
  46. package/node_modules/@poe-code/design-system/dist/dashboard/buffer.d.ts +25 -0
  47. package/node_modules/@poe-code/design-system/dist/dashboard/buffer.js +189 -0
  48. package/node_modules/@poe-code/design-system/dist/dashboard/components/border.d.ts +9 -0
  49. package/node_modules/@poe-code/design-system/dist/dashboard/components/border.js +123 -0
  50. package/node_modules/@poe-code/design-system/dist/dashboard/components/footer.d.ts +8 -0
  51. package/node_modules/@poe-code/design-system/dist/dashboard/components/footer.js +57 -0
  52. package/node_modules/@poe-code/design-system/dist/dashboard/components/output-pane.d.ts +12 -0
  53. package/node_modules/@poe-code/design-system/dist/dashboard/components/output-pane.js +254 -0
  54. package/node_modules/@poe-code/design-system/dist/dashboard/components/stats-pane.d.ts +7 -0
  55. package/node_modules/@poe-code/design-system/dist/dashboard/components/stats-pane.js +121 -0
  56. package/node_modules/@poe-code/design-system/dist/dashboard/dashboard.d.ts +20 -0
  57. package/node_modules/@poe-code/design-system/dist/dashboard/dashboard.js +167 -0
  58. package/node_modules/@poe-code/design-system/dist/dashboard/demo.d.ts +13 -0
  59. package/node_modules/@poe-code/design-system/dist/dashboard/demo.js +145 -0
  60. package/node_modules/@poe-code/design-system/dist/dashboard/index.d.ts +8 -0
  61. package/node_modules/@poe-code/design-system/dist/dashboard/index.js +4 -0
  62. package/node_modules/@poe-code/design-system/dist/dashboard/keymap.d.ts +3 -0
  63. package/node_modules/@poe-code/design-system/dist/dashboard/keymap.js +99 -0
  64. package/node_modules/@poe-code/design-system/dist/dashboard/layout.d.ts +25 -0
  65. package/node_modules/@poe-code/design-system/dist/dashboard/layout.js +79 -0
  66. package/node_modules/@poe-code/design-system/dist/dashboard/should-use-dashboard.d.ts +10 -0
  67. package/node_modules/@poe-code/design-system/dist/dashboard/should-use-dashboard.js +7 -0
  68. package/node_modules/@poe-code/design-system/dist/dashboard/snapshot.d.ts +10 -0
  69. package/node_modules/@poe-code/design-system/dist/dashboard/snapshot.js +68 -0
  70. package/node_modules/@poe-code/design-system/dist/dashboard/store.d.ts +8 -0
  71. package/node_modules/@poe-code/design-system/dist/dashboard/store.js +51 -0
  72. package/node_modules/@poe-code/design-system/dist/dashboard/terminal.d.ts +37 -0
  73. package/node_modules/@poe-code/design-system/dist/dashboard/terminal.js +233 -0
  74. package/node_modules/@poe-code/design-system/dist/dashboard/types.d.ts +36 -0
  75. package/node_modules/@poe-code/design-system/dist/dashboard/types.js +1 -0
  76. package/node_modules/@poe-code/design-system/dist/index.d.ts +33 -0
  77. package/node_modules/@poe-code/design-system/dist/index.js +31 -0
  78. package/node_modules/@poe-code/design-system/dist/internal/output-format.d.ts +6 -0
  79. package/node_modules/@poe-code/design-system/dist/internal/output-format.js +22 -0
  80. package/node_modules/@poe-code/design-system/dist/internal/strip-ansi.d.ts +1 -0
  81. package/node_modules/@poe-code/design-system/dist/internal/strip-ansi.js +3 -0
  82. package/node_modules/@poe-code/design-system/dist/internal/theme-detect.d.ts +11 -0
  83. package/node_modules/@poe-code/design-system/dist/internal/theme-detect.js +49 -0
  84. package/node_modules/@poe-code/design-system/dist/prompts/index.d.ts +66 -0
  85. package/node_modules/@poe-code/design-system/dist/prompts/index.js +132 -0
  86. package/node_modules/@poe-code/design-system/dist/prompts/primitives/cancel.d.ts +2 -0
  87. package/node_modules/@poe-code/design-system/dist/prompts/primitives/cancel.js +9 -0
  88. package/node_modules/@poe-code/design-system/dist/prompts/primitives/intro.d.ts +1 -0
  89. package/node_modules/@poe-code/design-system/dist/prompts/primitives/intro.js +15 -0
  90. package/node_modules/@poe-code/design-system/dist/prompts/primitives/log.d.ts +18 -0
  91. package/node_modules/@poe-code/design-system/dist/prompts/primitives/log.js +101 -0
  92. package/node_modules/@poe-code/design-system/dist/prompts/primitives/note.d.ts +1 -0
  93. package/node_modules/@poe-code/design-system/dist/prompts/primitives/note.js +39 -0
  94. package/node_modules/@poe-code/design-system/dist/prompts/primitives/outro.d.ts +1 -0
  95. package/node_modules/@poe-code/design-system/dist/prompts/primitives/outro.js +16 -0
  96. package/node_modules/@poe-code/design-system/dist/prompts/primitives/spinner.d.ts +6 -0
  97. package/node_modules/@poe-code/design-system/dist/prompts/primitives/spinner.js +74 -0
  98. package/node_modules/@poe-code/design-system/dist/prompts/theme.d.ts +11 -0
  99. package/node_modules/@poe-code/design-system/dist/prompts/theme.js +12 -0
  100. package/node_modules/@poe-code/design-system/dist/static/index.d.ts +4 -0
  101. package/node_modules/@poe-code/design-system/dist/static/index.js +2 -0
  102. package/node_modules/@poe-code/design-system/dist/static/menu.d.ts +11 -0
  103. package/node_modules/@poe-code/design-system/dist/static/menu.js +36 -0
  104. package/node_modules/@poe-code/design-system/dist/static/spinner.d.ts +14 -0
  105. package/node_modules/@poe-code/design-system/dist/static/spinner.js +46 -0
  106. package/node_modules/@poe-code/design-system/dist/terminal-markdown/ast.d.ts +92 -0
  107. package/node_modules/@poe-code/design-system/dist/terminal-markdown/ast.js +1 -0
  108. package/node_modules/@poe-code/design-system/dist/terminal-markdown/demo-content.d.ts +2 -0
  109. package/node_modules/@poe-code/design-system/dist/terminal-markdown/demo-content.js +139 -0
  110. package/node_modules/@poe-code/design-system/dist/terminal-markdown/index.d.ts +6 -0
  111. package/node_modules/@poe-code/design-system/dist/terminal-markdown/index.js +8 -0
  112. package/node_modules/@poe-code/design-system/dist/terminal-markdown/parser/block.d.ts +7 -0
  113. package/node_modules/@poe-code/design-system/dist/terminal-markdown/parser/block.js +1495 -0
  114. package/node_modules/@poe-code/design-system/dist/terminal-markdown/parser/frontmatter.d.ts +8 -0
  115. package/node_modules/@poe-code/design-system/dist/terminal-markdown/parser/frontmatter.js +412 -0
  116. package/node_modules/@poe-code/design-system/dist/terminal-markdown/parser/inline.d.ts +10 -0
  117. package/node_modules/@poe-code/design-system/dist/terminal-markdown/parser/inline.js +1166 -0
  118. package/node_modules/@poe-code/design-system/dist/terminal-markdown/parser.d.ts +5 -0
  119. package/node_modules/@poe-code/design-system/dist/terminal-markdown/parser.js +42 -0
  120. package/node_modules/@poe-code/design-system/dist/terminal-markdown/renderer.d.ts +6 -0
  121. package/node_modules/@poe-code/design-system/dist/terminal-markdown/renderer.js +572 -0
  122. package/node_modules/@poe-code/design-system/dist/terminal-markdown/testing/theme-render-fixture.d.ts +1 -0
  123. package/node_modules/@poe-code/design-system/dist/terminal-markdown/testing/theme-render-fixture.js +27 -0
  124. package/node_modules/@poe-code/design-system/dist/tokens/colors.d.ts +35 -0
  125. package/node_modules/@poe-code/design-system/dist/tokens/colors.js +34 -0
  126. package/node_modules/@poe-code/design-system/dist/tokens/index.d.ts +4 -0
  127. package/node_modules/@poe-code/design-system/dist/tokens/index.js +4 -0
  128. package/node_modules/@poe-code/design-system/dist/tokens/spacing.d.ts +6 -0
  129. package/node_modules/@poe-code/design-system/dist/tokens/spacing.js +6 -0
  130. package/node_modules/@poe-code/design-system/dist/tokens/typography.d.ts +7 -0
  131. package/node_modules/@poe-code/design-system/dist/tokens/typography.js +8 -0
  132. package/node_modules/@poe-code/design-system/dist/tokens/widths.d.ts +5 -0
  133. package/node_modules/@poe-code/design-system/dist/tokens/widths.js +5 -0
  134. package/node_modules/@poe-code/design-system/package.json +25 -0
  135. package/package.json +57 -0
@@ -0,0 +1,1495 @@
1
+ import { extractFrontmatter } from "./frontmatter.js";
2
+ import { parseInline } from "./inline.js";
3
+ const BLOCK_HTML_TAGS = new Set([
4
+ "address",
5
+ "article",
6
+ "aside",
7
+ "base",
8
+ "basefont",
9
+ "blockquote",
10
+ "body",
11
+ "caption",
12
+ "center",
13
+ "col",
14
+ "colgroup",
15
+ "dd",
16
+ "details",
17
+ "dialog",
18
+ "dir",
19
+ "div",
20
+ "dl",
21
+ "dt",
22
+ "fieldset",
23
+ "figcaption",
24
+ "figure",
25
+ "footer",
26
+ "form",
27
+ "frame",
28
+ "frameset",
29
+ "h1",
30
+ "h2",
31
+ "h3",
32
+ "h4",
33
+ "h5",
34
+ "h6",
35
+ "head",
36
+ "header",
37
+ "hr",
38
+ "html",
39
+ "iframe",
40
+ "legend",
41
+ "li",
42
+ "link",
43
+ "main",
44
+ "menu",
45
+ "menuitem",
46
+ "nav",
47
+ "noframes",
48
+ "ol",
49
+ "optgroup",
50
+ "option",
51
+ "p",
52
+ "param",
53
+ "search",
54
+ "section",
55
+ "summary",
56
+ "table",
57
+ "tbody",
58
+ "td",
59
+ "tfoot",
60
+ "th",
61
+ "thead",
62
+ "title",
63
+ "tr",
64
+ "track",
65
+ "ul"
66
+ ]);
67
+ const VOID_BLOCK_HTML_TAGS = new Set(["base", "basefont", "col", "hr", "link", "param", "track"]);
68
+ const SOURCE_OFFSETS = Symbol("sourceOffsets");
69
+ export function parseBlocks(input) {
70
+ return parseBlockDocument(input).children;
71
+ }
72
+ export function parseBlockDocument(input) {
73
+ const { frontmatter, body, range } = extractFrontmatter(input);
74
+ const children = applyInlineParsing(parseBlocksWithOptions(body, {
75
+ preferListToThematicBreak: false,
76
+ offsets: createOffsetMap(body, range?.end ?? 0)
77
+ }));
78
+ return frontmatter === undefined
79
+ ? { children }
80
+ : { frontmatter, frontmatterRange: range, children };
81
+ }
82
+ function parseBlocksWithOptions(input, options) {
83
+ const state = {
84
+ input,
85
+ position: 0,
86
+ offsets: options.offsets,
87
+ preferListToThematicBreak: options.preferListToThematicBreak
88
+ };
89
+ const blocks = [];
90
+ const rules = createBlockRules(state.preferListToThematicBreak);
91
+ while (state.position < state.input.length) {
92
+ const line = readLine(state.input, state.position);
93
+ if (isBlankLine(line.text)) {
94
+ state.position = line.nextPosition;
95
+ continue;
96
+ }
97
+ let matchedNode = null;
98
+ for (const rule of rules) {
99
+ matchedNode = rule(state);
100
+ if (matchedNode !== null) {
101
+ blocks.push(matchedNode);
102
+ break;
103
+ }
104
+ }
105
+ if (matchedNode !== null) {
106
+ continue;
107
+ }
108
+ blocks.push(parseParagraph(state));
109
+ }
110
+ return blocks;
111
+ }
112
+ function applyInlineParsing(nodes) {
113
+ const footnoteLabels = collectFootnoteLabels(nodes);
114
+ return nodes.map((node) => applyInlineParsingToNode(node, footnoteLabels));
115
+ }
116
+ function collectFootnoteLabels(nodes) {
117
+ const labels = new Set();
118
+ for (const node of nodes) {
119
+ collectFootnoteLabelsFromNode(node, labels);
120
+ }
121
+ return labels;
122
+ }
123
+ function collectFootnoteLabelsFromNode(node, labels) {
124
+ if (node.type === "footnoteDefinition") {
125
+ labels.add(node.label);
126
+ }
127
+ if (hasBlockChildren(node)) {
128
+ for (const child of node.children) {
129
+ collectFootnoteLabelsFromNode(child, labels);
130
+ }
131
+ }
132
+ }
133
+ function applyInlineParsingToNode(node, footnoteLabels) {
134
+ if (node.type === "paragraph" || node.type === "heading" || node.type === "tableCell") {
135
+ return replaceNodeChildren(node, applyInlineParsingToTextChildren(node.children, footnoteLabels));
136
+ }
137
+ if (hasBlockChildren(node)) {
138
+ return replaceNodeChildren(node, node.children.map((child) => applyInlineParsingToNode(child, footnoteLabels)));
139
+ }
140
+ return node;
141
+ }
142
+ function applyInlineParsingToTextChildren(children, footnoteLabels) {
143
+ let rawText = "";
144
+ let offsets;
145
+ for (const child of children) {
146
+ if (child.type !== "text") {
147
+ return children;
148
+ }
149
+ rawText += child.value;
150
+ const childOffsets = getTextNodeSourceOffsets(child);
151
+ if (childOffsets === undefined) {
152
+ return children;
153
+ }
154
+ if (offsets === undefined) {
155
+ offsets = [...childOffsets];
156
+ continue;
157
+ }
158
+ offsets.push(...childOffsets.slice(1));
159
+ }
160
+ return parseInline(rawText, {
161
+ footnoteLabels,
162
+ ...(offsets === undefined ? {} : { offsets })
163
+ });
164
+ }
165
+ function hasBlockChildren(node) {
166
+ return (node.type === "root" ||
167
+ node.type === "blockquote" ||
168
+ node.type === "list" ||
169
+ node.type === "listItem" ||
170
+ node.type === "table" ||
171
+ node.type === "tableRow" ||
172
+ node.type === "alert" ||
173
+ node.type === "footnoteDefinition");
174
+ }
175
+ function createBlockRules(preferListToThematicBreak) {
176
+ const middleRules = preferListToThematicBreak
177
+ ? [parseAlert, parseBlockquote, parseList, parseHtmlBlock, parseThematicBreak]
178
+ : [parseThematicBreak, parseAlert, parseBlockquote, parseList, parseHtmlBlock];
179
+ return [
180
+ parseFencedCodeBlock,
181
+ parseAtxHeading,
182
+ ...middleRules,
183
+ parseTable,
184
+ parseFootnoteDefinition,
185
+ parseSetextHeading
186
+ ];
187
+ }
188
+ function parseFencedCodeBlock(state) {
189
+ const rangeStart = state.position;
190
+ const openingLine = readLine(state.input, state.position);
191
+ const fence = parseOpeningFence(openingLine.text);
192
+ if (fence === null) {
193
+ return null;
194
+ }
195
+ state.position = openingLine.nextPosition;
196
+ const contentLines = [];
197
+ while (state.position < state.input.length) {
198
+ const line = readLine(state.input, state.position);
199
+ if (isClosingFence(line.text, fence)) {
200
+ state.position = line.nextPosition;
201
+ return withRange(createCodeNode(fence, contentLines), createNodeRange(state, rangeStart));
202
+ }
203
+ contentLines.push(line.text);
204
+ state.position = line.nextPosition;
205
+ }
206
+ return withRange(createCodeNode(fence, contentLines), createNodeRange(state, rangeStart));
207
+ }
208
+ function parseAtxHeading(state) {
209
+ const rangeStart = state.position;
210
+ const line = readLine(state.input, state.position);
211
+ const heading = parseAtxHeadingLine(line.text);
212
+ if (heading === null) {
213
+ return null;
214
+ }
215
+ state.position = line.nextPosition;
216
+ const content = createMappedTextFromLineSlice(state, line, heading.contentStart, heading.contentEnd);
217
+ return withRange({
218
+ type: "heading",
219
+ depth: heading.depth,
220
+ children: createTextChildren(heading.text, { start: content.offsets[0] ?? 0, end: content.offsets[content.offsets.length - 1] ?? 0 }, content.offsets)
221
+ }, createNodeRange(state, rangeStart));
222
+ }
223
+ function parseThematicBreak(state) {
224
+ const rangeStart = state.position;
225
+ const line = readLine(state.input, state.position);
226
+ if (!isThematicBreakLine(line.text)) {
227
+ return null;
228
+ }
229
+ state.position = line.nextPosition;
230
+ return withRange({ type: "thematicBreak" }, createNodeRange(state, rangeStart));
231
+ }
232
+ function parseSetextHeading(state) {
233
+ const rangeStart = state.position;
234
+ const contentLine = readLine(state.input, state.position);
235
+ if (isBlankLine(contentLine.text)) {
236
+ return null;
237
+ }
238
+ if (contentLine.nextPosition >= state.input.length) {
239
+ return null;
240
+ }
241
+ const underlineLine = readLine(state.input, contentLine.nextPosition);
242
+ const depth = parseSetextUnderline(underlineLine.text);
243
+ if (depth === null) {
244
+ return null;
245
+ }
246
+ state.position = underlineLine.nextPosition;
247
+ const content = trimAsciiWhitespaceRange(contentLine.text);
248
+ const mappedContent = createMappedTextFromLineSlice(state, contentLine, content.start, content.end);
249
+ return withRange({
250
+ type: "heading",
251
+ depth,
252
+ children: createTextChildren(content.value, {
253
+ start: mappedContent.offsets[0] ?? 0,
254
+ end: mappedContent.offsets[mappedContent.offsets.length - 1] ?? 0
255
+ }, mappedContent.offsets)
256
+ }, createNodeRange(state, rangeStart));
257
+ }
258
+ function parseBlockquote(state) {
259
+ const rangeStart = state.position;
260
+ const firstLine = readLine(state.input, state.position);
261
+ if (parseBlockquoteLine(firstLine.text) === null) {
262
+ return null;
263
+ }
264
+ const contentParts = [];
265
+ let previousContentLine;
266
+ while (state.position < state.input.length) {
267
+ const line = readLine(state.input, state.position);
268
+ const content = parseBlockquoteLine(line.text);
269
+ if (content === null) {
270
+ break;
271
+ }
272
+ if (previousContentLine !== undefined) {
273
+ const lineBreak = createMappedLineBreak(state, previousContentLine);
274
+ if (lineBreak !== null) {
275
+ contentParts.push(lineBreak);
276
+ }
277
+ }
278
+ contentParts.push(createMappedTextFromLineSlice(state, line, content.contentStart, line.text.length));
279
+ state.position = line.nextPosition;
280
+ previousContentLine = line;
281
+ }
282
+ const content = joinMappedTexts(contentParts);
283
+ return withRange({
284
+ type: "blockquote",
285
+ children: parseBlocksWithOptions(content.text, {
286
+ preferListToThematicBreak: state.preferListToThematicBreak,
287
+ offsets: content.offsets
288
+ })
289
+ }, createNodeRange(state, rangeStart));
290
+ }
291
+ function parseAlert(state) {
292
+ const rangeStart = state.position;
293
+ const firstLine = readLine(state.input, state.position);
294
+ const firstLineContent = parseBlockquoteLine(firstLine.text);
295
+ if (firstLineContent === null) {
296
+ return null;
297
+ }
298
+ const alertMarker = parseAlertMarker(firstLineContent.content);
299
+ if (alertMarker === null) {
300
+ return null;
301
+ }
302
+ state.position = firstLine.nextPosition;
303
+ const contentParts = [];
304
+ let previousContentLine;
305
+ if (alertMarker.content.length > 0) {
306
+ contentParts.push(createMappedTextFromLineSlice(state, firstLine, firstLineContent.contentStart + alertMarker.contentStart, firstLine.text.length));
307
+ previousContentLine = firstLine;
308
+ }
309
+ while (state.position < state.input.length) {
310
+ const line = readLine(state.input, state.position);
311
+ const content = parseBlockquoteLine(line.text);
312
+ if (content === null) {
313
+ break;
314
+ }
315
+ if (previousContentLine !== undefined) {
316
+ const previousLineBreak = createMappedLineBreak(state, previousContentLine);
317
+ if (previousLineBreak !== null) {
318
+ contentParts.push(previousLineBreak);
319
+ }
320
+ }
321
+ contentParts.push(createMappedTextFromLineSlice(state, line, content.contentStart, line.text.length));
322
+ state.position = line.nextPosition;
323
+ previousContentLine = line;
324
+ }
325
+ const content = joinMappedTexts(contentParts);
326
+ return withRange({
327
+ type: "alert",
328
+ kind: alertMarker.kind,
329
+ children: parseBlocksWithOptions(content.text, {
330
+ preferListToThematicBreak: state.preferListToThematicBreak,
331
+ offsets: content.offsets
332
+ })
333
+ }, createNodeRange(state, rangeStart));
334
+ }
335
+ function parseList(state) {
336
+ const rangeStart = state.position;
337
+ const firstLine = readLine(state.input, state.position);
338
+ const firstMarker = parseListMarker(firstLine.text);
339
+ if (firstMarker === null) {
340
+ return null;
341
+ }
342
+ const children = [];
343
+ const ordered = firstMarker.ordered;
344
+ const start = firstMarker.start;
345
+ const indent = firstMarker.indent;
346
+ while (state.position < state.input.length) {
347
+ const item = parseListItem(state, {
348
+ ordered,
349
+ indent,
350
+ preferListToThematicBreak: true
351
+ });
352
+ if (item === null) {
353
+ break;
354
+ }
355
+ children.push(item);
356
+ }
357
+ return withRange({
358
+ type: "list",
359
+ ordered,
360
+ ...(ordered && start !== undefined ? { start } : {}),
361
+ children
362
+ }, createNodeRange(state, rangeStart));
363
+ }
364
+ function parseTable(state) {
365
+ const rangeStart = state.position;
366
+ const table = parseTableAt(state.input, state.position, state.preferListToThematicBreak);
367
+ if (table === null) {
368
+ return null;
369
+ }
370
+ state.position = table.nextPosition;
371
+ return withRange({
372
+ type: "table",
373
+ align: table.align,
374
+ children: [
375
+ createTableRowNode(state, table.header.line, table.header.cells),
376
+ ...table.rows.map((row) => createTableRowNode(state, row.line, row.cells))
377
+ ]
378
+ }, createNodeRange(state, rangeStart));
379
+ }
380
+ function parseHtmlBlock(state) {
381
+ const rangeStart = state.position;
382
+ const firstLine = readLine(state.input, state.position);
383
+ const openingTag = parseBlockHtmlTagStart(firstLine.text);
384
+ if (openingTag === null) {
385
+ return null;
386
+ }
387
+ state.position = firstLine.nextPosition;
388
+ const lines = [firstLine.text];
389
+ const isSelfContained = openingTag.closing ||
390
+ openingTag.selfClosing ||
391
+ VOID_BLOCK_HTML_TAGS.has(openingTag.tagName) ||
392
+ containsClosingHtmlTag(firstLine.text, openingTag.tagName, openingTag.tagEnd);
393
+ if (!isSelfContained) {
394
+ while (state.position < state.input.length) {
395
+ const line = readLine(state.input, state.position);
396
+ lines.push(line.text);
397
+ state.position = line.nextPosition;
398
+ if (containsClosingHtmlTag(line.text, openingTag.tagName)) {
399
+ break;
400
+ }
401
+ }
402
+ }
403
+ return withRange({ type: "html", value: lines.join("\n") }, createNodeRange(state, rangeStart));
404
+ }
405
+ function parseFootnoteDefinition(state) {
406
+ const rangeStart = state.position;
407
+ const firstLine = readLine(state.input, state.position);
408
+ const definition = parseFootnoteDefinitionMarker(firstLine.text);
409
+ if (definition === null) {
410
+ return null;
411
+ }
412
+ state.position = firstLine.nextPosition;
413
+ const continuationLines = [];
414
+ const pendingBlankLines = [];
415
+ while (state.position < state.input.length) {
416
+ const line = readLine(state.input, state.position);
417
+ if (isBlankLine(line.text)) {
418
+ pendingBlankLines.push({
419
+ text: "",
420
+ offsets: [state.offsets[line.end] ?? 0],
421
+ lineBreakEnd: state.offsets[line.nextPosition] ?? state.offsets[state.offsets.length - 1] ?? 0
422
+ });
423
+ state.position = line.nextPosition;
424
+ continue;
425
+ }
426
+ const strippedLine = stripIndentLine(state, line, 4);
427
+ if (strippedLine === null) {
428
+ break;
429
+ }
430
+ state.position = line.nextPosition;
431
+ continuationLines.push(...pendingBlankLines, strippedLine);
432
+ pendingBlankLines.length = 0;
433
+ }
434
+ return withRange({
435
+ type: "footnoteDefinition",
436
+ label: definition.label,
437
+ children: parseListItemChildren(createMappedTextFromLineSlice(state, firstLine, definition.contentStart, firstLine.text.length), continuationLines, state.preferListToThematicBreak)
438
+ }, createNodeRange(state, rangeStart));
439
+ }
440
+ function parseParagraph(state) {
441
+ const rangeStart = state.position;
442
+ const lines = [];
443
+ while (state.position < state.input.length) {
444
+ const line = readLine(state.input, state.position);
445
+ if (isBlankLine(line.text)) {
446
+ break;
447
+ }
448
+ if (lines.length > 0 &&
449
+ startsBlockAt(state.input, state.position, state.preferListToThematicBreak)) {
450
+ break;
451
+ }
452
+ lines.push(line);
453
+ state.position = line.nextPosition;
454
+ }
455
+ const content = createMappedTextFromLines(state, lines);
456
+ return withRange({
457
+ type: "paragraph",
458
+ children: createTextChildren(content.text, {
459
+ start: content.offsets[0] ?? 0,
460
+ end: content.offsets[content.offsets.length - 1] ?? 0
461
+ }, content.offsets)
462
+ }, createNodeRange(state, rangeStart));
463
+ }
464
+ function createCodeNode(fence, contentLines) {
465
+ return {
466
+ type: "code",
467
+ ...(fence.lang === undefined ? {} : { lang: fence.lang }),
468
+ ...(fence.meta === undefined ? {} : { meta: fence.meta }),
469
+ value: contentLines.join("\n")
470
+ };
471
+ }
472
+ function createTextChildren(value, range, offsets) {
473
+ return value.length === 0 ? [] : [createTextNode(value, range, offsets)];
474
+ }
475
+ function createTableRowNode(state, line, cells) {
476
+ return withRange({
477
+ type: "tableRow",
478
+ children: cells.map((cell) => {
479
+ const content = createMappedTextFromLineSlice(state, line, cell.start, cell.end);
480
+ return withRange({
481
+ type: "tableCell",
482
+ children: createTextChildren(cell.value, {
483
+ start: content.offsets[0] ?? 0,
484
+ end: content.offsets[content.offsets.length - 1] ?? 0
485
+ }, content.offsets)
486
+ }, {
487
+ start: content.offsets[0] ?? 0,
488
+ end: content.offsets[content.offsets.length - 1] ?? 0
489
+ });
490
+ })
491
+ }, {
492
+ start: state.offsets[line.start] ?? 0,
493
+ end: state.offsets[line.nextPosition] ?? state.offsets[line.end] ?? 0
494
+ });
495
+ }
496
+ function createTextNode(value, range, sourceOffsets) {
497
+ const node = range === undefined ? { type: "text", value } : withRange({ type: "text", value }, range);
498
+ if (sourceOffsets !== undefined) {
499
+ Object.defineProperty(node, SOURCE_OFFSETS, {
500
+ value: [...sourceOffsets],
501
+ enumerable: false,
502
+ configurable: true,
503
+ writable: true
504
+ });
505
+ }
506
+ return node;
507
+ }
508
+ function createNodeRange(state, start, end = state.position) {
509
+ return {
510
+ start: state.offsets[start] ?? state.offsets[state.offsets.length - 1] ?? 0,
511
+ end: state.offsets[end] ?? state.offsets[state.offsets.length - 1] ?? 0
512
+ };
513
+ }
514
+ function withRange(node, range) {
515
+ Object.defineProperty(node, "range", {
516
+ value: range,
517
+ enumerable: false,
518
+ configurable: true,
519
+ writable: true
520
+ });
521
+ return node;
522
+ }
523
+ function replaceNodeChildren(node, children) {
524
+ const nextNode = { ...node, children };
525
+ return node.range === undefined ? nextNode : withRange(nextNode, node.range);
526
+ }
527
+ function getTextNodeSourceOffsets(node) {
528
+ const sourceOffsets = node[SOURCE_OFFSETS];
529
+ if (sourceOffsets !== undefined) {
530
+ return sourceOffsets;
531
+ }
532
+ return node.range === undefined ? undefined : createOffsetMap(node.value, node.range.start, node.range.end);
533
+ }
534
+ function createOffsetMap(input, absoluteStart = 0, absoluteEnd) {
535
+ const offsets = new Array(input.length + 1).fill(absoluteStart);
536
+ let byteOffset = absoluteStart;
537
+ let index = 0;
538
+ while (index < input.length) {
539
+ offsets[index] = byteOffset;
540
+ const codePoint = input.codePointAt(index) ?? 0;
541
+ const codeUnitLength = codePoint > 0xffff ? 2 : 1;
542
+ const byteLength = codePoint <= 0x7f ? 1 : codePoint <= 0x7ff ? 2 : codePoint <= 0xffff ? 3 : 4;
543
+ for (let offsetIndex = 1; offsetIndex < codeUnitLength; offsetIndex += 1) {
544
+ offsets[index + offsetIndex] = byteOffset;
545
+ }
546
+ byteOffset += byteLength;
547
+ index += codeUnitLength;
548
+ offsets[index] = byteOffset;
549
+ }
550
+ offsets[input.length] = absoluteEnd ?? byteOffset;
551
+ return offsets;
552
+ }
553
+ function createMappedText(value, offsets) {
554
+ return { text: value, offsets };
555
+ }
556
+ function createMappedTextFromSlice(state, start, end) {
557
+ return createMappedText(state.input.slice(start, end), state.offsets.slice(start, end + 1));
558
+ }
559
+ function createMappedTextFromLineSlice(state, line, start, end) {
560
+ return {
561
+ ...createMappedTextFromSlice(state, line.start + start, line.start + end),
562
+ lineBreakEnd: state.offsets[line.nextPosition] ?? state.offsets[state.offsets.length - 1] ?? 0
563
+ };
564
+ }
565
+ function createMappedLineBreak(state, line) {
566
+ if (line.nextPosition <= line.end) {
567
+ return null;
568
+ }
569
+ return createMappedText("\n", [state.offsets[line.end] ?? 0, state.offsets[line.nextPosition] ?? 0]);
570
+ }
571
+ function joinMappedTexts(parts) {
572
+ if (parts.length === 0) {
573
+ return createMappedText("", [0]);
574
+ }
575
+ let text = "";
576
+ const offsets = [];
577
+ for (const [index, part] of parts.entries()) {
578
+ text += part.text;
579
+ if (index === 0) {
580
+ offsets.push(...part.offsets);
581
+ continue;
582
+ }
583
+ offsets.push(...part.offsets.slice(1));
584
+ }
585
+ return createMappedText(text, offsets);
586
+ }
587
+ function createMappedTextFromLines(state, lines) {
588
+ const parts = [];
589
+ for (const [index, line] of lines.entries()) {
590
+ parts.push(createMappedTextFromLineSlice(state, line, 0, line.text.length));
591
+ if (index + 1 < lines.length) {
592
+ const lineBreak = createMappedLineBreak(state, line);
593
+ if (lineBreak !== null) {
594
+ parts.push(lineBreak);
595
+ }
596
+ }
597
+ }
598
+ return joinMappedTexts(parts);
599
+ }
600
+ function joinMappedLines(lines) {
601
+ const parts = [];
602
+ for (const [index, line] of lines.entries()) {
603
+ parts.push(createMappedText(line.text, line.offsets));
604
+ if (index + 1 < lines.length) {
605
+ const lineBreakStart = line.offsets[line.offsets.length - 1] ?? 0;
606
+ const lineBreakEnd = line.lineBreakEnd ?? lineBreakStart;
607
+ parts.push(createMappedText("\n", [lineBreakStart, lineBreakEnd]));
608
+ }
609
+ }
610
+ return joinMappedTexts(parts);
611
+ }
612
+ function startsBlockAt(input, position, preferListToThematicBreak) {
613
+ const line = readLine(input, position);
614
+ if (startsSimpleBlock(line.text, preferListToThematicBreak)) {
615
+ return true;
616
+ }
617
+ if (line.nextPosition >= input.length) {
618
+ return false;
619
+ }
620
+ const nextLine = readLine(input, line.nextPosition);
621
+ return parseTableHeaderAndSeparator(line.text, nextLine.text) !== null;
622
+ }
623
+ function startsBlockInLines(lines, lineIndex, preferListToThematicBreak) {
624
+ const line = lines[lineIndex]?.text ?? "";
625
+ if (startsSimpleBlock(line, preferListToThematicBreak)) {
626
+ return true;
627
+ }
628
+ return (lineIndex + 1 < lines.length &&
629
+ parseTableHeaderAndSeparator(line, lines[lineIndex + 1]?.text ?? "") !== null);
630
+ }
631
+ function startsSimpleBlock(line, preferListToThematicBreak) {
632
+ if (parseOpeningFence(line) !== null ||
633
+ parseAtxHeadingLine(line) !== null ||
634
+ parseBlockHtmlTagStart(line) !== null ||
635
+ parseFootnoteDefinitionMarker(line) !== null) {
636
+ return true;
637
+ }
638
+ if (parseBlockquoteLine(line) !== null) {
639
+ return true;
640
+ }
641
+ if (preferListToThematicBreak) {
642
+ return parseListMarker(line) !== null || isThematicBreakLine(line);
643
+ }
644
+ return isThematicBreakLine(line) || parseListMarker(line) !== null;
645
+ }
646
+ function parseOpeningFence(line) {
647
+ const fenceStart = skipLeadingBlockIndent(line);
648
+ if (fenceStart === -1 || fenceStart >= line.length) {
649
+ return null;
650
+ }
651
+ const char = line[fenceStart];
652
+ if (char !== "`" && char !== "~") {
653
+ return null;
654
+ }
655
+ let fenceEnd = fenceStart;
656
+ while (fenceEnd < line.length && line[fenceEnd] === char) {
657
+ fenceEnd += 1;
658
+ }
659
+ const fenceLength = fenceEnd - fenceStart;
660
+ if (fenceLength < 3) {
661
+ return null;
662
+ }
663
+ const info = trimAsciiWhitespace(line.slice(fenceEnd));
664
+ if (info.length === 0) {
665
+ return { char, length: fenceLength };
666
+ }
667
+ const languageEnd = findWhitespaceIndex(info);
668
+ const lang = languageEnd === -1 ? info : info.slice(0, languageEnd);
669
+ const meta = languageEnd === -1 ? undefined : trimAsciiWhitespaceStart(info.slice(languageEnd));
670
+ return {
671
+ char,
672
+ length: fenceLength,
673
+ lang,
674
+ ...(meta === undefined || meta.length === 0 ? {} : { meta })
675
+ };
676
+ }
677
+ function parseAtxHeadingLine(line) {
678
+ const markerStart = skipLeadingBlockIndent(line);
679
+ if (markerStart === -1 || markerStart >= line.length || line[markerStart] !== "#") {
680
+ return null;
681
+ }
682
+ let markerEnd = markerStart;
683
+ while (markerEnd < line.length && line[markerEnd] === "#") {
684
+ markerEnd += 1;
685
+ }
686
+ const depth = markerEnd - markerStart;
687
+ if (depth < 1 || depth > 6) {
688
+ return null;
689
+ }
690
+ if (markerEnd < line.length && line[markerEnd] !== " " && line[markerEnd] !== "\t") {
691
+ return null;
692
+ }
693
+ let contentStart = markerEnd;
694
+ while (contentStart < line.length && (line[contentStart] === " " || line[contentStart] === "\t")) {
695
+ contentStart += 1;
696
+ }
697
+ const rawContent = line.slice(contentStart);
698
+ const text = stripClosingHeadingSequence(rawContent);
699
+ return {
700
+ depth: depth,
701
+ text,
702
+ contentStart,
703
+ contentEnd: contentStart + text.length
704
+ };
705
+ }
706
+ function stripClosingHeadingSequence(value) {
707
+ const trimmedValue = trimAsciiWhitespaceEnd(value);
708
+ if (trimmedValue.length === 0) {
709
+ return "";
710
+ }
711
+ let hashStart = trimmedValue.length;
712
+ while (hashStart > 0 && trimmedValue[hashStart - 1] === "#") {
713
+ hashStart -= 1;
714
+ }
715
+ if (hashStart === trimmedValue.length) {
716
+ return trimmedValue;
717
+ }
718
+ if (hashStart === 0) {
719
+ return "";
720
+ }
721
+ const prefixEnd = hashStart - 1;
722
+ if (trimmedValue[prefixEnd] !== " " && trimmedValue[prefixEnd] !== "\t") {
723
+ return trimmedValue;
724
+ }
725
+ return trimAsciiWhitespaceEnd(trimmedValue.slice(0, prefixEnd));
726
+ }
727
+ function isClosingFence(line, fence) {
728
+ const fenceStart = skipLeadingBlockIndent(line);
729
+ if (fenceStart === -1 || fenceStart >= line.length) {
730
+ return false;
731
+ }
732
+ let index = fenceStart;
733
+ while (index < line.length && line[index] === fence.char) {
734
+ index += 1;
735
+ }
736
+ if (index - fenceStart < fence.length) {
737
+ return false;
738
+ }
739
+ if (fenceStart === index) {
740
+ return false;
741
+ }
742
+ for (let restIndex = index; restIndex < line.length; restIndex += 1) {
743
+ const char = line[restIndex];
744
+ if (char !== " " && char !== "\t") {
745
+ return false;
746
+ }
747
+ }
748
+ return true;
749
+ }
750
+ function isThematicBreakLine(line) {
751
+ const start = skipLeadingBlockIndent(line);
752
+ if (start === -1 || start >= line.length) {
753
+ return false;
754
+ }
755
+ const marker = line[start];
756
+ if (marker !== "-" && marker !== "*" && marker !== "_") {
757
+ return false;
758
+ }
759
+ let markerCount = 0;
760
+ for (let index = start; index < line.length; index += 1) {
761
+ const char = line[index];
762
+ if (char === marker) {
763
+ markerCount += 1;
764
+ continue;
765
+ }
766
+ if (char === " " || char === "\t") {
767
+ continue;
768
+ }
769
+ return false;
770
+ }
771
+ return markerCount >= 3;
772
+ }
773
+ function parseSetextUnderline(line) {
774
+ const start = skipLeadingBlockIndent(line);
775
+ if (start === -1 || start >= line.length) {
776
+ return null;
777
+ }
778
+ const marker = line[start];
779
+ if (marker !== "=" && marker !== "-") {
780
+ return null;
781
+ }
782
+ let index = start;
783
+ while (index < line.length && line[index] === marker) {
784
+ index += 1;
785
+ }
786
+ const markerCount = index - start;
787
+ if (markerCount === 0) {
788
+ return null;
789
+ }
790
+ for (; index < line.length; index += 1) {
791
+ const char = line[index];
792
+ if (char !== " " && char !== "\t") {
793
+ return null;
794
+ }
795
+ }
796
+ return marker === "=" ? 1 : 2;
797
+ }
798
+ function parseTableAt(input, position, preferListToThematicBreak) {
799
+ const headerLine = readLine(input, position);
800
+ if (parseListMarker(headerLine.text) !== null ||
801
+ parseBlockquoteLine(headerLine.text) !== null) {
802
+ return null;
803
+ }
804
+ if (headerLine.nextPosition >= input.length) {
805
+ return null;
806
+ }
807
+ const separatorLine = readLine(input, headerLine.nextPosition);
808
+ const header = parseTableHeaderAndSeparator(headerLine.text, separatorLine.text);
809
+ if (header === null) {
810
+ return null;
811
+ }
812
+ const rows = [];
813
+ let nextPosition = separatorLine.nextPosition;
814
+ while (nextPosition < input.length) {
815
+ const rowLine = readLine(input, nextPosition);
816
+ if (isBlankLine(rowLine.text)) {
817
+ break;
818
+ }
819
+ if (startsSimpleBlock(rowLine.text, preferListToThematicBreak)) {
820
+ break;
821
+ }
822
+ const cells = parsePipeTableCellSegments(rowLine.text);
823
+ if (cells === null) {
824
+ break;
825
+ }
826
+ rows.push({
827
+ line: rowLine,
828
+ cells: normalizeTableCellSegments(cells, header.headerCells.length)
829
+ });
830
+ nextPosition = rowLine.nextPosition;
831
+ }
832
+ return {
833
+ align: header.align,
834
+ header: { line: headerLine, cells: header.headerCells },
835
+ rows,
836
+ nextPosition
837
+ };
838
+ }
839
+ function parseTableHeaderAndSeparator(headerLine, separatorLine) {
840
+ const headerCells = parsePipeTableCellSegments(headerLine);
841
+ if (headerCells === null || headerCells.length === 0) {
842
+ return null;
843
+ }
844
+ const align = parsePipeTableSeparator(separatorLine);
845
+ if (align === null || align.length !== headerCells.length) {
846
+ return null;
847
+ }
848
+ return { align, headerCells };
849
+ }
850
+ function parsePipeTableCells(line) {
851
+ return parsePipeTableCellSegments(line)?.map((cell) => cell.value) ?? null;
852
+ }
853
+ function parsePipeTableCellSegments(line) {
854
+ const start = skipLeadingBlockIndent(line);
855
+ if (start === -1 || start >= line.length) {
856
+ return null;
857
+ }
858
+ const content = trimAsciiWhitespaceEnd(line.slice(start));
859
+ let hasPipe = false;
860
+ const cells = [];
861
+ let cellStart = 0;
862
+ let index = 0;
863
+ while (index < content.length) {
864
+ const char = content[index];
865
+ if (char === "\\" && index + 1 < content.length && content[index + 1] === "|") {
866
+ index += 2;
867
+ continue;
868
+ }
869
+ if (char === "|") {
870
+ cells.push({ start: cellStart, end: index });
871
+ cellStart = index + 1;
872
+ hasPipe = true;
873
+ index += 1;
874
+ continue;
875
+ }
876
+ index += 1;
877
+ }
878
+ if (!hasPipe) {
879
+ return null;
880
+ }
881
+ cells.push({ start: cellStart, end: content.length });
882
+ if (content[0] === "|") {
883
+ cells.shift();
884
+ }
885
+ if (content[content.length - 1] === "|") {
886
+ cells.pop();
887
+ }
888
+ if (cells.length === 0) {
889
+ return null;
890
+ }
891
+ return cells.map((cell) => {
892
+ let cellStart = cell.start;
893
+ let cellEnd = cell.end;
894
+ while (cellStart < cellEnd && (content[cellStart] === " " || content[cellStart] === "\t")) {
895
+ cellStart += 1;
896
+ }
897
+ while (cellEnd > cellStart && (content[cellEnd - 1] === " " || content[cellEnd - 1] === "\t")) {
898
+ cellEnd -= 1;
899
+ }
900
+ return {
901
+ value: decodePipeTableCell(trimAsciiWhitespace(content.slice(cell.start, cell.end))),
902
+ start: start + cellStart,
903
+ end: start + cellEnd
904
+ };
905
+ });
906
+ }
907
+ function parsePipeTableSeparator(line) {
908
+ const cells = parsePipeTableCells(line);
909
+ if (cells === null || cells.length === 0) {
910
+ return null;
911
+ }
912
+ const alignments = [];
913
+ for (const cell of cells) {
914
+ const alignment = parseTableAlignmentCell(cell);
915
+ if (alignment === undefined) {
916
+ return null;
917
+ }
918
+ alignments.push(alignment);
919
+ }
920
+ return alignments;
921
+ }
922
+ function parseTableAlignmentCell(value) {
923
+ if (value.length === 0) {
924
+ return undefined;
925
+ }
926
+ const hasLeadingColon = value[0] === ":";
927
+ const hasTrailingColon = value[value.length - 1] === ":";
928
+ const dashStart = hasLeadingColon ? 1 : 0;
929
+ const dashEnd = hasTrailingColon ? value.length - 1 : value.length;
930
+ if (dashEnd - dashStart < 3) {
931
+ return undefined;
932
+ }
933
+ for (let index = dashStart; index < dashEnd; index += 1) {
934
+ if (value[index] !== "-") {
935
+ return undefined;
936
+ }
937
+ }
938
+ if (hasLeadingColon && hasTrailingColon) {
939
+ return "center";
940
+ }
941
+ if (hasLeadingColon) {
942
+ return "left";
943
+ }
944
+ if (hasTrailingColon) {
945
+ return "right";
946
+ }
947
+ return null;
948
+ }
949
+ function normalizeTableCellSegments(cells, columnCount) {
950
+ if (cells.length === columnCount) {
951
+ return cells;
952
+ }
953
+ if (cells.length > columnCount) {
954
+ return cells.slice(0, columnCount);
955
+ }
956
+ return [
957
+ ...cells,
958
+ ...Array.from({ length: columnCount - cells.length }, () => ({
959
+ value: "",
960
+ start: cells[cells.length - 1]?.end ?? 0,
961
+ end: cells[cells.length - 1]?.end ?? 0
962
+ }))
963
+ ];
964
+ }
965
+ function decodePipeTableCell(value) {
966
+ let decoded = "";
967
+ let index = 0;
968
+ while (index < value.length) {
969
+ if (value[index] === "\\" && value[index + 1] === "|") {
970
+ decoded += "|";
971
+ index += 2;
972
+ continue;
973
+ }
974
+ decoded += value[index] ?? "";
975
+ index += 1;
976
+ }
977
+ return decoded;
978
+ }
979
+ function parseBlockquoteLine(line) {
980
+ const markerStart = skipLeadingBlockIndent(line);
981
+ if (markerStart === -1 || markerStart >= line.length || line[markerStart] !== ">") {
982
+ return null;
983
+ }
984
+ let contentStart = markerStart + 1;
985
+ if (contentStart < line.length && (line[contentStart] === " " || line[contentStart] === "\t")) {
986
+ contentStart += 1;
987
+ }
988
+ return {
989
+ content: line.slice(contentStart),
990
+ contentStart
991
+ };
992
+ }
993
+ function parseAlertMarker(content) {
994
+ const endOfKind = content.indexOf("]");
995
+ if (content.length === 0 || !content.startsWith("[!") || endOfKind === -1) {
996
+ return null;
997
+ }
998
+ const kind = content.slice(2, endOfKind);
999
+ if (!isAlertKind(kind)) {
1000
+ return null;
1001
+ }
1002
+ const rawContent = content.slice(endOfKind + 1);
1003
+ const trimmedContent = trimAsciiWhitespaceStart(rawContent);
1004
+ return {
1005
+ kind,
1006
+ content: trimmedContent,
1007
+ contentStart: endOfKind + 1 + (rawContent.length - trimmedContent.length)
1008
+ };
1009
+ }
1010
+ function parseFootnoteDefinitionMarker(line) {
1011
+ const markerStart = skipLeadingBlockIndent(line);
1012
+ if (markerStart === -1 ||
1013
+ markerStart + 3 >= line.length ||
1014
+ line[markerStart] !== "[" ||
1015
+ line[markerStart + 1] !== "^") {
1016
+ return null;
1017
+ }
1018
+ let labelEnd = markerStart + 2;
1019
+ while (labelEnd < line.length && line[labelEnd] !== "]") {
1020
+ if (!isFootnoteLabelChar(line[labelEnd])) {
1021
+ return null;
1022
+ }
1023
+ labelEnd += 1;
1024
+ }
1025
+ if (labelEnd === markerStart + 2 || labelEnd + 1 >= line.length || line[labelEnd + 1] !== ":") {
1026
+ return null;
1027
+ }
1028
+ let contentStart = labelEnd + 2;
1029
+ while (contentStart < line.length &&
1030
+ (line[contentStart] === " " || line[contentStart] === "\t")) {
1031
+ contentStart += 1;
1032
+ }
1033
+ return {
1034
+ label: line.slice(markerStart + 2, labelEnd),
1035
+ contentStart
1036
+ };
1037
+ }
1038
+ function parseBlockHtmlTagStart(line) {
1039
+ const start = skipLeadingBlockIndent(line);
1040
+ if (start === -1 || start >= line.length || line[start] !== "<") {
1041
+ return null;
1042
+ }
1043
+ let index = start + 1;
1044
+ let closing = false;
1045
+ if (index < line.length && line[index] === "/") {
1046
+ closing = true;
1047
+ index += 1;
1048
+ }
1049
+ if (index >= line.length || !isAsciiLetter(line[index])) {
1050
+ return null;
1051
+ }
1052
+ const tagNameStart = index;
1053
+ while (index < line.length && isHtmlTagNameChar(line[index])) {
1054
+ index += 1;
1055
+ }
1056
+ const tagName = line.slice(tagNameStart, index).toLowerCase();
1057
+ if (!BLOCK_HTML_TAGS.has(tagName)) {
1058
+ return null;
1059
+ }
1060
+ if (closing) {
1061
+ index = skipHtmlWhitespace(line, index);
1062
+ if (index >= line.length || line[index] !== ">") {
1063
+ return null;
1064
+ }
1065
+ return {
1066
+ tagName,
1067
+ tagEnd: index + 1,
1068
+ closing: true,
1069
+ selfClosing: false
1070
+ };
1071
+ }
1072
+ while (index < line.length) {
1073
+ index = skipHtmlWhitespace(line, index);
1074
+ if (index >= line.length) {
1075
+ return null;
1076
+ }
1077
+ if (line[index] === ">") {
1078
+ return {
1079
+ tagName,
1080
+ tagEnd: index + 1,
1081
+ closing: false,
1082
+ selfClosing: false
1083
+ };
1084
+ }
1085
+ if (line[index] === "/") {
1086
+ const selfClosingStart = skipHtmlWhitespace(line, index + 1);
1087
+ if (selfClosingStart >= line.length || line[selfClosingStart] !== ">") {
1088
+ return null;
1089
+ }
1090
+ return {
1091
+ tagName,
1092
+ tagEnd: selfClosingStart + 1,
1093
+ closing: false,
1094
+ selfClosing: true
1095
+ };
1096
+ }
1097
+ if (!isHtmlAttributeNameStartChar(line[index])) {
1098
+ return null;
1099
+ }
1100
+ index += 1;
1101
+ while (index < line.length && isHtmlAttributeNameChar(line[index])) {
1102
+ index += 1;
1103
+ }
1104
+ index = skipHtmlWhitespace(line, index);
1105
+ if (index >= line.length || line[index] !== "=") {
1106
+ continue;
1107
+ }
1108
+ index = skipHtmlWhitespace(line, index + 1);
1109
+ if (index >= line.length) {
1110
+ return null;
1111
+ }
1112
+ const quote = line[index];
1113
+ if (quote === '"' || quote === "'") {
1114
+ index += 1;
1115
+ while (index < line.length && line[index] !== quote) {
1116
+ index += 1;
1117
+ }
1118
+ if (index >= line.length) {
1119
+ return null;
1120
+ }
1121
+ index += 1;
1122
+ continue;
1123
+ }
1124
+ while (index < line.length && !isHtmlWhitespace(line[index]) && line[index] !== ">") {
1125
+ const char = line[index];
1126
+ if (char === '"' || char === "'" || char === "<" || char === "=" || char === "`") {
1127
+ return null;
1128
+ }
1129
+ index += 1;
1130
+ }
1131
+ }
1132
+ return null;
1133
+ }
1134
+ function containsClosingHtmlTag(line, tagName, fromIndex = 0) {
1135
+ const lowerLine = line.toLowerCase();
1136
+ const needle = `</${tagName}`;
1137
+ let searchIndex = fromIndex;
1138
+ while (searchIndex < lowerLine.length) {
1139
+ const matchIndex = lowerLine.indexOf(needle, searchIndex);
1140
+ if (matchIndex === -1) {
1141
+ return false;
1142
+ }
1143
+ let endIndex = matchIndex + needle.length;
1144
+ while (endIndex < lowerLine.length && isHtmlWhitespace(lowerLine[endIndex])) {
1145
+ endIndex += 1;
1146
+ }
1147
+ if (endIndex < lowerLine.length && lowerLine[endIndex] === ">") {
1148
+ return true;
1149
+ }
1150
+ searchIndex = matchIndex + 1;
1151
+ }
1152
+ return false;
1153
+ }
1154
+ function parseListMarker(line) {
1155
+ const markerStart = skipLeadingBlockIndent(line);
1156
+ if (markerStart === -1 || markerStart >= line.length) {
1157
+ return null;
1158
+ }
1159
+ const marker = line[markerStart];
1160
+ if (marker === "-" || marker === "+" || marker === "*") {
1161
+ return parseBulletListMarker(line, markerStart);
1162
+ }
1163
+ if (isDigit(marker)) {
1164
+ return parseOrderedListMarker(line, markerStart);
1165
+ }
1166
+ return null;
1167
+ }
1168
+ function parseBulletListMarker(line, markerStart) {
1169
+ const contentStart = parseContentStart(line, markerStart + 1);
1170
+ if (contentStart === null) {
1171
+ return null;
1172
+ }
1173
+ return {
1174
+ ordered: false,
1175
+ indent: measureColumns(line.slice(0, markerStart)),
1176
+ contentIndent: measureColumns(line.slice(0, contentStart)),
1177
+ contentStart
1178
+ };
1179
+ }
1180
+ function parseOrderedListMarker(line, markerStart) {
1181
+ let markerEnd = markerStart;
1182
+ while (markerEnd < line.length && isDigit(line[markerEnd])) {
1183
+ markerEnd += 1;
1184
+ }
1185
+ if (markerEnd >= line.length || (line[markerEnd] !== "." && line[markerEnd] !== ")")) {
1186
+ return null;
1187
+ }
1188
+ const contentStart = parseContentStart(line, markerEnd + 1);
1189
+ if (contentStart === null) {
1190
+ return null;
1191
+ }
1192
+ return {
1193
+ ordered: true,
1194
+ start: Number.parseInt(line.slice(markerStart, markerEnd), 10),
1195
+ indent: measureColumns(line.slice(0, markerStart)),
1196
+ contentIndent: measureColumns(line.slice(0, contentStart)),
1197
+ contentStart
1198
+ };
1199
+ }
1200
+ function parseContentStart(line, afterMarker) {
1201
+ if (afterMarker >= line.length) {
1202
+ return afterMarker;
1203
+ }
1204
+ if (line[afterMarker] !== " " && line[afterMarker] !== "\t") {
1205
+ return null;
1206
+ }
1207
+ let pos = afterMarker;
1208
+ while (pos < line.length && (line[pos] === " " || line[pos] === "\t")) {
1209
+ pos += 1;
1210
+ }
1211
+ return pos;
1212
+ }
1213
+ function readLine(input, position) {
1214
+ let index = position;
1215
+ while (index < input.length) {
1216
+ const char = input[index];
1217
+ if (char === "\n" || char === "\r") {
1218
+ break;
1219
+ }
1220
+ index += 1;
1221
+ }
1222
+ const text = input.slice(position, index);
1223
+ if (index >= input.length) {
1224
+ return { start: position, text, end: index, nextPosition: input.length };
1225
+ }
1226
+ if (input[index] === "\r" && input[index + 1] === "\n") {
1227
+ return { start: position, text, end: index, nextPosition: index + 2 };
1228
+ }
1229
+ return { start: position, text, end: index, nextPosition: index + 1 };
1230
+ }
1231
+ function isBlankLine(line) {
1232
+ for (let index = 0; index < line.length; index += 1) {
1233
+ const char = line[index];
1234
+ if (char !== " " && char !== "\t") {
1235
+ return false;
1236
+ }
1237
+ }
1238
+ return true;
1239
+ }
1240
+ function skipLeadingBlockIndent(line) {
1241
+ const bomOffset = line.startsWith("\uFEFF") ? 1 : 0;
1242
+ const leadingWhitespace = readLeadingWhitespace(line.slice(bomOffset));
1243
+ if (leadingWhitespace.columns > 3) {
1244
+ return -1;
1245
+ }
1246
+ return bomOffset + leadingWhitespace.offset;
1247
+ }
1248
+ function trimAsciiWhitespace(value) {
1249
+ return trimAsciiWhitespaceEnd(trimAsciiWhitespaceStart(value));
1250
+ }
1251
+ function trimAsciiWhitespaceStart(value) {
1252
+ let start = 0;
1253
+ while (start < value.length) {
1254
+ const char = value[start];
1255
+ if (char !== " " && char !== "\t") {
1256
+ break;
1257
+ }
1258
+ start += 1;
1259
+ }
1260
+ return value.slice(start);
1261
+ }
1262
+ function trimAsciiWhitespaceEnd(value) {
1263
+ let end = value.length;
1264
+ while (end > 0) {
1265
+ const char = value[end - 1];
1266
+ if (char !== " " && char !== "\t") {
1267
+ break;
1268
+ }
1269
+ end -= 1;
1270
+ }
1271
+ return value.slice(0, end);
1272
+ }
1273
+ function trimAsciiWhitespaceRange(value) {
1274
+ let start = 0;
1275
+ let end = value.length;
1276
+ while (start < end && (value[start] === " " || value[start] === "\t")) {
1277
+ start += 1;
1278
+ }
1279
+ while (end > start && (value[end - 1] === " " || value[end - 1] === "\t")) {
1280
+ end -= 1;
1281
+ }
1282
+ return {
1283
+ value: value.slice(start, end),
1284
+ start,
1285
+ end
1286
+ };
1287
+ }
1288
+ function findWhitespaceIndex(value) {
1289
+ for (let index = 0; index < value.length; index += 1) {
1290
+ const char = value[index];
1291
+ if (char === " " || char === "\t") {
1292
+ return index;
1293
+ }
1294
+ }
1295
+ return -1;
1296
+ }
1297
+ function skipHtmlWhitespace(line, start) {
1298
+ let index = start;
1299
+ while (index < line.length && isHtmlWhitespace(line[index])) {
1300
+ index += 1;
1301
+ }
1302
+ return index;
1303
+ }
1304
+ function isDigit(value) {
1305
+ return value >= "0" && value <= "9";
1306
+ }
1307
+ function isAsciiLetter(value) {
1308
+ return (value >= "a" && value <= "z") || (value >= "A" && value <= "Z");
1309
+ }
1310
+ function isHtmlTagNameChar(value) {
1311
+ return isAsciiLetter(value) || isDigit(value) || value === "-";
1312
+ }
1313
+ function isHtmlAttributeNameStartChar(value) {
1314
+ return isAsciiLetter(value) || value === ":" || value === "_";
1315
+ }
1316
+ function isHtmlAttributeNameChar(value) {
1317
+ return isHtmlAttributeNameStartChar(value) || isDigit(value) || value === "-" || value === ".";
1318
+ }
1319
+ function isHtmlWhitespace(value) {
1320
+ return value === " " || value === "\t";
1321
+ }
1322
+ function isFootnoteLabelChar(value) {
1323
+ return isAsciiLetter(value) || isDigit(value) || value === "-" || value === "_";
1324
+ }
1325
+ function isAlertKind(value) {
1326
+ return (value === "NOTE" ||
1327
+ value === "TIP" ||
1328
+ value === "IMPORTANT" ||
1329
+ value === "WARNING" ||
1330
+ value === "CAUTION");
1331
+ }
1332
+ function parseListItem(state, list) {
1333
+ const rangeStart = state.position;
1334
+ const firstLine = readLine(state.input, state.position);
1335
+ const marker = parseListMarker(firstLine.text);
1336
+ if (marker === null || marker.ordered !== list.ordered || marker.indent !== list.indent) {
1337
+ return null;
1338
+ }
1339
+ state.position = firstLine.nextPosition;
1340
+ let firstLineContentStart = marker.contentStart;
1341
+ let firstLineContent = firstLine.text.slice(firstLineContentStart);
1342
+ let checked;
1343
+ const taskMarker = parseTaskMarker(firstLineContent);
1344
+ if (taskMarker !== null) {
1345
+ checked = taskMarker.checked;
1346
+ firstLineContent = taskMarker.content;
1347
+ firstLineContentStart += taskMarker.contentStart;
1348
+ }
1349
+ const continuationLines = [];
1350
+ const pendingBlankLines = [];
1351
+ while (state.position < state.input.length) {
1352
+ const line = readLine(state.input, state.position);
1353
+ if (isBlankLine(line.text)) {
1354
+ pendingBlankLines.push({
1355
+ text: "",
1356
+ offsets: [state.offsets[line.end] ?? 0],
1357
+ lineBreakEnd: state.offsets[line.nextPosition] ?? state.offsets[state.offsets.length - 1] ?? 0
1358
+ });
1359
+ state.position = line.nextPosition;
1360
+ continue;
1361
+ }
1362
+ const nextMarker = parseListMarker(line.text);
1363
+ if (nextMarker !== null &&
1364
+ nextMarker.ordered === list.ordered &&
1365
+ nextMarker.indent === list.indent) {
1366
+ break;
1367
+ }
1368
+ const strippedLine = stripIndentLine(state, line, marker.contentIndent);
1369
+ if (strippedLine === null) {
1370
+ break;
1371
+ }
1372
+ state.position = line.nextPosition;
1373
+ continuationLines.push(...pendingBlankLines, strippedLine);
1374
+ pendingBlankLines.length = 0;
1375
+ }
1376
+ const children = parseListItemChildren(createMappedTextFromLineSlice(state, firstLine, firstLineContentStart, firstLine.text.length), continuationLines, list.preferListToThematicBreak);
1377
+ return withRange({
1378
+ type: "listItem",
1379
+ ...(checked === undefined ? {} : { checked }),
1380
+ children
1381
+ }, createNodeRange(state, rangeStart));
1382
+ }
1383
+ function parseListItemChildren(firstLineContent, continuationLines, preferListToThematicBreak) {
1384
+ const blocks = [];
1385
+ let paragraphLines = firstLineContent.text.length === 0 ? [] : [firstLineContent];
1386
+ let lineIndex = 0;
1387
+ while (lineIndex < continuationLines.length) {
1388
+ const line = continuationLines[lineIndex];
1389
+ if (isBlankLine(line.text)) {
1390
+ if (paragraphLines.length > 0) {
1391
+ blocks.push(createParagraphNode(paragraphLines));
1392
+ paragraphLines = [];
1393
+ }
1394
+ lineIndex += 1;
1395
+ continue;
1396
+ }
1397
+ if (startsBlockInLines(continuationLines, lineIndex, preferListToThematicBreak)) {
1398
+ if (paragraphLines.length > 0) {
1399
+ blocks.push(createParagraphNode(paragraphLines));
1400
+ }
1401
+ const nestedContent = joinMappedLines(continuationLines.slice(lineIndex));
1402
+ blocks.push(...parseBlocksWithOptions(nestedContent.text, {
1403
+ preferListToThematicBreak,
1404
+ offsets: nestedContent.offsets
1405
+ }));
1406
+ return blocks;
1407
+ }
1408
+ paragraphLines.push(line);
1409
+ lineIndex += 1;
1410
+ }
1411
+ if (paragraphLines.length > 0) {
1412
+ blocks.push(createParagraphNode(paragraphLines));
1413
+ }
1414
+ return blocks;
1415
+ }
1416
+ function createParagraphNode(lines) {
1417
+ const content = joinMappedLines(lines);
1418
+ return withRange({
1419
+ type: "paragraph",
1420
+ children: createTextChildren(content.text, {
1421
+ start: content.offsets[0] ?? 0,
1422
+ end: content.offsets[content.offsets.length - 1] ?? 0
1423
+ }, content.offsets)
1424
+ }, {
1425
+ start: lines[0]?.offsets[0] ?? 0,
1426
+ end: content.offsets[content.offsets.length - 1] ?? 0
1427
+ });
1428
+ }
1429
+ function parseTaskMarker(content) {
1430
+ if (content.length < 3 || content[0] !== "[" || content[2] !== "]") {
1431
+ return null;
1432
+ }
1433
+ const marker = content[1];
1434
+ if (marker !== " " && marker !== "x" && marker !== "X") {
1435
+ return null;
1436
+ }
1437
+ if (content.length > 3 && content[3] !== " " && content[3] !== "\t") {
1438
+ return null;
1439
+ }
1440
+ return {
1441
+ checked: marker === "x" || marker === "X",
1442
+ content: trimAsciiWhitespaceStart(content.slice(3)),
1443
+ contentStart: 3 + (content.slice(3).length - trimAsciiWhitespaceStart(content.slice(3)).length)
1444
+ };
1445
+ }
1446
+ function stripIndentLine(state, line, columns) {
1447
+ const leadingWhitespace = readLeadingWhitespace(line.text);
1448
+ if (leadingWhitespace.columns < columns) {
1449
+ return null;
1450
+ }
1451
+ const prefix = leadingWhitespace.normalized.slice(columns);
1452
+ const prefixStart = state.offsets[line.start + leadingWhitespace.offset] ?? 0;
1453
+ const parts = [];
1454
+ if (prefix.length > 0) {
1455
+ parts.push(createMappedText(prefix, new Array(prefix.length + 1).fill(prefixStart)));
1456
+ }
1457
+ parts.push({
1458
+ ...createMappedTextFromLineSlice(state, line, leadingWhitespace.offset, line.text.length),
1459
+ lineBreakEnd: state.offsets[line.nextPosition] ?? state.offsets[state.offsets.length - 1] ?? 0
1460
+ });
1461
+ const stripped = joinMappedTexts(parts);
1462
+ return {
1463
+ ...stripped,
1464
+ lineBreakEnd: state.offsets[line.nextPosition] ?? state.offsets[state.offsets.length - 1] ?? 0
1465
+ };
1466
+ }
1467
+ function readLeadingWhitespace(line) {
1468
+ let columns = 0;
1469
+ let offset = 0;
1470
+ let normalized = "";
1471
+ while (offset < line.length) {
1472
+ const char = line[offset];
1473
+ if (char === " ") {
1474
+ columns += 1;
1475
+ normalized += " ";
1476
+ offset += 1;
1477
+ continue;
1478
+ }
1479
+ if (char === "\t") {
1480
+ columns += 4;
1481
+ normalized += " ";
1482
+ offset += 1;
1483
+ continue;
1484
+ }
1485
+ break;
1486
+ }
1487
+ return { columns, offset, normalized };
1488
+ }
1489
+ function measureColumns(value) {
1490
+ let columns = 0;
1491
+ for (let index = 0; index < value.length; index += 1) {
1492
+ columns += value[index] === "\t" ? 4 : 1;
1493
+ }
1494
+ return columns;
1495
+ }