@aljazmc/syntax 0.4.0 → 0.5.0

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.
package/bin/index.js CHANGED
@@ -1,1749 +1,2375 @@
1
1
  var __defProp = Object.defineProperty;
2
2
  var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
3
3
 
4
- // shared/syntax/src/markdown/scan.ts
5
- var TAGNAME = "[A-Za-z][A-Za-z0-9-]*";
6
- var ATTRIBUTE = `(?:\\s+[a-zA-Z_:][a-zA-Z0-9:._-]*(?:\\s*=\\s*(?:[^"'=<>\`\\x00-\\x20]+|'[^']*'|"[^"]*"))?)`;
7
- var OPEN_TAG = `<${TAGNAME}${ATTRIBUTE}*\\s*/?>`;
8
- var CLOSE_TAG = `</${TAGNAME}\\s*[>]`;
9
- var Scan = class {
4
+ // shared/syntax/src/scanner.ts
5
+ var ParseError = class extends SyntaxError {
10
6
  static {
11
- __name(this, "Scan");
7
+ __name(this, "ParseError");
12
8
  }
13
- /** Normalizes a link label for matching, like `[Foo Bar]` and `[foo bar]`. */
14
- static label(label) {
15
- return label.slice(1, -1).trim().replace(/[ \t\r\n]+/g, " ").toLowerCase().toUpperCase();
9
+ position;
10
+ constructor(message, position) {
11
+ super(message);
12
+ this.position = position;
16
13
  }
17
- /** A link destination, `<...>` or without spaces and with balanced parentheses. */
18
- static destination(text, from) {
19
- if (text[from] === "<") {
20
- for (let index2 = from + 1; index2 < text.length; index2++) {
21
- const char = text[index2];
22
- if (char === "\\") index2++;
23
- else if (char === "\n" || char === "<") return;
24
- else if (char === ">")
25
- return [index2 + 1, text.slice(from, index2 + 1)];
26
- }
27
- return;
28
- }
29
- let depth = 0;
30
- let index = from;
31
- while (index < text.length) {
32
- const char = text[index];
33
- if (char === "\\" && /[!-/:-@[-`{-~]/.test(text[index + 1] ?? "")) {
34
- index += 2;
35
- continue;
14
+ };
15
+ var Scanner = class _Scanner {
16
+ static {
17
+ __name(this, "Scanner");
18
+ }
19
+ static #punctuators = [
20
+ ">>>=",
21
+ "...",
22
+ "===",
23
+ "!==",
24
+ "**=",
25
+ "&&=",
26
+ "||=",
27
+ "??=",
28
+ "=>",
29
+ "==",
30
+ "!=",
31
+ "<=",
32
+ "&&",
33
+ "||",
34
+ "??",
35
+ "?.",
36
+ "++",
37
+ "--",
38
+ "+=",
39
+ "-=",
40
+ "*=",
41
+ "/=",
42
+ "%=",
43
+ "&=",
44
+ "|=",
45
+ "^=",
46
+ "**"
47
+ ];
48
+ static #greater = [">>>=", ">>>", ">>=", ">>", ">=", ">"];
49
+ static #less = ["<<=", "<<", "<=", "<"];
50
+ source;
51
+ comments = [];
52
+ #position = 0;
53
+ constructor(source) {
54
+ this.source = source;
55
+ }
56
+ get position() {
57
+ return this.#position;
58
+ }
59
+ /** Moves back (or forward) and forgets comments scanned beyond. */
60
+ reset(position) {
61
+ this.#position = position;
62
+ while ((this.comments.at(-1)?.start ?? -1) >= position)
63
+ this.comments.pop();
64
+ }
65
+ next() {
66
+ const newline = this.#trivia();
67
+ const start = this.#position;
68
+ const char = this.source[start];
69
+ if (char === void 0) return this.#token("eof", start, newline);
70
+ if (_Scanner.#identifierStart(char) || char === "\\")
71
+ return this.#word("name", start, newline);
72
+ if (char === "#") return this.#word("private", start, newline);
73
+ if (/[0-9]/.test(char) || char === "." && /[0-9]/.test(this.source[start + 1] ?? ""))
74
+ return this.#number(start, newline);
75
+ if (char === '"' || char === "'") return this.#string(start, newline);
76
+ if (char === "`") return this.template(start, newline);
77
+ const punctuator = _Scanner.#punctuators.find(
78
+ (value) => this.source.startsWith(value, start) && !(value === "?." && /[0-9]/.test(this.source[start + 2] ?? ""))
79
+ ) ?? char;
80
+ this.#position = start + punctuator.length;
81
+ return this.#token("punct", start, newline);
82
+ }
83
+ /** Rescans a `/` or `/=` token as a regular expression literal. */
84
+ regex(token) {
85
+ let inClass = false;
86
+ for (let index = token.start + 1; ; index++) {
87
+ const char = this.source[index];
88
+ if (char === void 0 || char === "\n" || char === "\r")
89
+ return this.#invalid(token.start, "Unterminated regex");
90
+ if (char === "\\") index++;
91
+ else if (char === "[") inClass = true;
92
+ else if (char === "]") inClass = false;
93
+ else if (char === "/" && !inClass) {
94
+ this.#position = index + 1;
95
+ while (_Scanner.#identifierPart(this.source[this.#position] ?? ""))
96
+ this.#position++;
97
+ return this.#token("regex", token.start, token.newline);
36
98
  }
37
- if (char === "(") depth++;
38
- else if (char === ")") {
39
- if (depth === 0) break;
40
- depth--;
41
- } else if (char <= " " || char === "\x7F") break;
42
- index++;
43
99
  }
44
- if (depth !== 0) return;
45
- return [index, text.slice(from, index)];
46
100
  }
47
- /** The position after a link title in quotes or parentheses. */
48
- static title(text, from) {
49
- const open = text[from];
50
- const close = open === "(" ? ")" : open;
51
- if (open !== '"' && open !== "'" && open !== "(") return;
52
- for (let index = from + 1; index < text.length; index++) {
53
- const char = text[index];
101
+ /** Rescans `>` as the longest of `>>>=`, `>>>`, `>>=`, `>>`, `>=`. */
102
+ greater(token) {
103
+ return this.#operator(token, _Scanner.#greater);
104
+ }
105
+ /** Rescans `<` as the longest of `<<=`, `<<`, `<=`, since types nest `<<T>() => T>`. */
106
+ less(token) {
107
+ return this.#operator(token, _Scanner.#less);
108
+ }
109
+ #operator(token, candidates) {
110
+ const value = candidates.find(
111
+ (value2) => this.source.startsWith(value2, token.start)
112
+ ) ?? token.value;
113
+ this.reset(token.start + value.length);
114
+ return { ...token, value, end: token.start + value.length };
115
+ }
116
+ /** Scans a template part, from a backtick or a closing `}`, up to `${` or a backtick. */
117
+ template(start, newline = false) {
118
+ for (let index = start + 1; index < this.source.length; index++) {
119
+ const char = this.source[index];
54
120
  if (char === "\\") index++;
55
- else if (char === close) return index + 1;
56
- else if (open === "(" && char === "(") return;
57
- else if (char === "\n" && text[index + 1] === "\n") return;
121
+ else if (char === "`" || char === "$" && this.source[index + 1] === "{") {
122
+ this.#position = index + (char === "`" ? 1 : 2);
123
+ return this.#token("template", start, newline);
124
+ }
58
125
  }
126
+ return this.#invalid(start, "Unterminated template");
59
127
  }
60
- };
61
-
62
- // shared/syntax/src/markdown/inlines.ts
63
- var Item = class {
64
- static {
65
- __name(this, "Item");
128
+ /** Scans JSX text up to `{` or `<`. */
129
+ jsxText(start) {
130
+ let index = start;
131
+ while (index < this.source.length && !"{<".includes(this.source[index] ?? ""))
132
+ index++;
133
+ this.#position = index;
134
+ return this.#token("jsxText", start, false);
66
135
  }
67
- kind;
68
- start;
69
- end;
70
- parent = null;
71
- first = null;
72
- last = null;
73
- prev = null;
74
- next = null;
75
- delimiter = "";
76
- tail = "";
77
- reference = null;
78
- label = null;
79
- destination = "";
80
- title = null;
81
- constructor(kind, start, end) {
82
- this.kind = kind;
83
- this.start = start;
84
- this.end = end;
136
+ /** Rescans a name token to include JSX's `-` characters. */
137
+ jsxName(token) {
138
+ let index = token.end;
139
+ while (/[-\p{ID_Continue}$]/u.test(this.source[index] ?? "")) index++;
140
+ this.reset(index);
141
+ return {
142
+ ...token,
143
+ value: this.source.slice(token.start, index),
144
+ end: index
145
+ };
85
146
  }
86
- append(child) {
87
- child.unlink();
88
- child.parent = this;
89
- if (this.last) {
90
- this.last.next = child;
91
- child.prev = this.last;
92
- } else this.first = child;
93
- this.last = child;
147
+ /** Rescans a quoted JSX attribute value, which has no escapes. */
148
+ jsxString(token) {
149
+ const quote = this.source[token.start] ?? "";
150
+ const end = this.source.indexOf(quote, token.start + 1);
151
+ if (end < 0) return this.#invalid(token.start, "Unterminated string");
152
+ this.reset(end + 1);
153
+ return {
154
+ ...token,
155
+ type: "string",
156
+ value: this.source.slice(token.start, end + 1),
157
+ end: end + 1
158
+ };
94
159
  }
95
- insertAfter(sibling) {
96
- sibling.unlink();
97
- sibling.next = this.next;
98
- if (sibling.next) sibling.next.prev = sibling;
99
- sibling.prev = this;
100
- this.next = sibling;
101
- sibling.parent = this.parent;
102
- if (this.parent && !sibling.next) this.parent.last = sibling;
160
+ #token(type, start, newline) {
161
+ return {
162
+ type,
163
+ value: this.source.slice(start, this.#position),
164
+ start,
165
+ end: this.#position,
166
+ newline
167
+ };
103
168
  }
104
- unlink() {
105
- if (this.prev) this.prev.next = this.next;
106
- else if (this.parent) this.parent.first = this.next;
107
- if (this.next) this.next.prev = this.prev;
108
- else if (this.parent) this.parent.last = this.prev;
109
- this.parent = null;
110
- this.next = null;
111
- this.prev = null;
169
+ #invalid(start, message) {
170
+ this.#position = this.source.length;
171
+ return {
172
+ type: "invalid",
173
+ value: message,
174
+ start,
175
+ end: start,
176
+ newline: false
177
+ };
178
+ }
179
+ /** Skips whitespace and comments, returns whether a line break was crossed. */
180
+ #trivia() {
181
+ let newline = false;
182
+ if (this.#position === 0 && this.source.startsWith("#!")) {
183
+ const end = this.#lineEnd(0);
184
+ this.comments.push({
185
+ type: "Line",
186
+ value: this.source.slice(0, end),
187
+ start: 0,
188
+ end
189
+ });
190
+ this.#position = end;
191
+ }
192
+ while (this.#position < this.source.length) {
193
+ const char = this.source[this.#position] ?? "";
194
+ const next = this.source[this.#position + 1];
195
+ if (char === "\n" || char === "\r" || char === "\u2028" || char === "\u2029") {
196
+ newline = true;
197
+ this.#position++;
198
+ } else if (/\s/.test(char)) this.#position++;
199
+ else if (char === "/" && next === "/") {
200
+ const end = this.#lineEnd(this.#position);
201
+ this.#comment("Line", end);
202
+ } else if (char === "/" && next === "*") {
203
+ const close = this.source.indexOf("*/", this.#position + 2);
204
+ const end = close < 0 ? this.source.length : close + 2;
205
+ if (/[\r\n\u2028\u2029]/.test(
206
+ this.source.slice(this.#position, end)
207
+ ))
208
+ newline = true;
209
+ this.#comment("Block", end);
210
+ } else break;
211
+ }
212
+ return newline;
213
+ }
214
+ #comment(type, end) {
215
+ const start = this.#position;
216
+ this.comments.push({
217
+ type,
218
+ value: this.source.slice(start, end),
219
+ start,
220
+ end
221
+ });
222
+ this.#position = end;
223
+ }
224
+ #lineEnd(start) {
225
+ const match = /[\r\n\u2028\u2029]/.exec(this.source.slice(start));
226
+ return match ? start + match.index : this.source.length;
227
+ }
228
+ #word(type, start, newline) {
229
+ let index = start + 1;
230
+ while (index < this.source.length) {
231
+ const char = this.source[index] ?? "";
232
+ if (char === "\\")
233
+ index += /^\\u\{/.test(this.source.slice(index, index + 3)) ? this.source.indexOf("}", index) - index + 1 : 6;
234
+ else if (_Scanner.#identifierPart(char)) index += char.length;
235
+ else {
236
+ const code = this.source.codePointAt(index) ?? 0;
237
+ const symbol = String.fromCodePoint(code);
238
+ if (code > 65535 && _Scanner.#identifierPart(symbol))
239
+ index += 2;
240
+ else break;
241
+ }
242
+ }
243
+ this.#position = index;
244
+ return this.#token(type, start, newline);
245
+ }
246
+ #number(start, newline) {
247
+ const pattern = /0[xX][\da-fA-F_]+n?|0[oO][0-7_]+n?|0[bB][01_]+n?|(?:\d[\d_]*\.?[\d_]*|\.\d[\d_]*)(?:[eE][+-]?\d[\d_]*)?n?/y;
248
+ pattern.lastIndex = start;
249
+ pattern.exec(this.source);
250
+ this.#position = pattern.lastIndex;
251
+ return this.#token("number", start, newline);
252
+ }
253
+ #string(start, newline) {
254
+ const quote = this.source[start];
255
+ for (let index = start + 1; index < this.source.length; index++) {
256
+ const char = this.source[index];
257
+ if (char === "\\")
258
+ index += this.source.startsWith("\r\n", index + 1) ? 2 : 1;
259
+ else if (char === quote) {
260
+ this.#position = index + 1;
261
+ return this.#token("string", start, newline);
262
+ } else if (char === "\n" || char === "\r") break;
263
+ }
264
+ return this.#invalid(start, "Unterminated string");
265
+ }
266
+ static #identifierStart(char) {
267
+ return /[\p{ID_Start}$_]/u.test(char) || /[\ud800-\udbff]/.test(char);
268
+ }
269
+ static #identifierPart(char) {
270
+ return /[\p{ID_Continue}$‌‍]/u.test(char);
112
271
  }
113
272
  };
114
- var ENTITY = /^&(?:#[xX][a-fA-F0-9]{1,6}|#[0-9]{1,7}|[A-Za-z][A-Za-z0-9]{1,31});/;
115
- var AUTOLINK = /^<[A-Za-z][A-Za-z0-9.+-]{1,31}:[^<>\p{Cc} ]*>/u;
116
- var EMAIL = /^<[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*>/;
117
- var HTML_TAG = new RegExp(
118
- `^(?:${OPEN_TAG}|${CLOSE_TAG}|<!-->|<!--->|<!--[\\s\\S]*?-->|[<][?][\\s\\S]*?[?][>]|<![A-Za-z][^>]*>|<!\\[CDATA\\[[\\s\\S]*?\\]\\]>)`,
119
- "i"
120
- );
121
- var PUNCTUATION = /[\p{P}\p{S}]/u;
122
- var WHITESPACE = /[\s\p{Zs}]/u;
123
- var Inlines = class _Inlines {
273
+
274
+ // shared/syntax/src/html/tags.ts
275
+ var tagNames = /* @__PURE__ */ new Set([
276
+ "a",
277
+ "abbr",
278
+ "acronym",
279
+ "address",
280
+ "altGlyph",
281
+ "altGlyphDef",
282
+ "altGlyphItem",
283
+ "animate",
284
+ "animateColor",
285
+ "animateMotion",
286
+ "animateTransform",
287
+ "area",
288
+ "article",
289
+ "aside",
290
+ "audio",
291
+ "b",
292
+ "base",
293
+ "basefont",
294
+ "bdi",
295
+ "bdo",
296
+ "bgsound",
297
+ "big",
298
+ "blink",
299
+ "blockquote",
300
+ "body",
301
+ "br",
302
+ "button",
303
+ "canvas",
304
+ "caption",
305
+ "center",
306
+ "circle",
307
+ "cite",
308
+ "clipPath",
309
+ "code",
310
+ "col",
311
+ "colgroup",
312
+ "color-profile",
313
+ "component",
314
+ "cursor",
315
+ "data",
316
+ "datalist",
317
+ "dd",
318
+ "defs",
319
+ "del",
320
+ "desc",
321
+ "details",
322
+ "dfn",
323
+ "dialog",
324
+ "dir",
325
+ "discard",
326
+ "div",
327
+ "dl",
328
+ "dt",
329
+ "ellipse",
330
+ "em",
331
+ "embed",
332
+ "feBlend",
333
+ "feColorMatrix",
334
+ "feComponentTransfer",
335
+ "feComposite",
336
+ "feConvolveMatrix",
337
+ "feDiffuseLighting",
338
+ "feDisplacementMap",
339
+ "feDistantLight",
340
+ "feDropShadow",
341
+ "feFlood",
342
+ "feFuncA",
343
+ "feFuncB",
344
+ "feFuncG",
345
+ "feFuncR",
346
+ "feGaussianBlur",
347
+ "feImage",
348
+ "feMerge",
349
+ "feMergeNode",
350
+ "feMorphology",
351
+ "feOffset",
352
+ "fePointLight",
353
+ "feSpecularLighting",
354
+ "feSpotLight",
355
+ "feTile",
356
+ "feTurbulence",
357
+ "fieldset",
358
+ "figcaption",
359
+ "figure",
360
+ "filter",
361
+ "font",
362
+ "font-face",
363
+ "font-face-format",
364
+ "font-face-name",
365
+ "font-face-src",
366
+ "font-face-uri",
367
+ "footer",
368
+ "foreignObject",
369
+ "form",
370
+ "frame",
371
+ "frameset",
372
+ "g",
373
+ "glyph",
374
+ "glyphRef",
375
+ "h1",
376
+ "h2",
377
+ "h3",
378
+ "h4",
379
+ "h5",
380
+ "h6",
381
+ "hatch",
382
+ "hatchpath",
383
+ "head",
384
+ "header",
385
+ "hgroup",
386
+ "hkern",
387
+ "hr",
388
+ "html",
389
+ "i",
390
+ "iframe",
391
+ "image",
392
+ "img",
393
+ "input",
394
+ "ins",
395
+ "kbd",
396
+ "keygen",
397
+ "label",
398
+ "legend",
399
+ "li",
400
+ "line",
401
+ "linearGradient",
402
+ "link",
403
+ "main",
404
+ "map",
405
+ "mark",
406
+ "marker",
407
+ "marquee",
408
+ "mask",
409
+ "menu",
410
+ "menuitem",
411
+ "mesh",
412
+ "meshgradient",
413
+ "meshpatch",
414
+ "meshrow",
415
+ "meta",
416
+ "metadata",
417
+ "meter",
418
+ "missing-glyph",
419
+ "mpath",
420
+ "nav",
421
+ "nobr",
422
+ "noembed",
423
+ "noframes",
424
+ "noscript",
425
+ "object",
426
+ "ol",
427
+ "optgroup",
428
+ "option",
429
+ "output",
430
+ "p",
431
+ "param",
432
+ "path",
433
+ "pattern",
434
+ "picture",
435
+ "plaintext",
436
+ "polygon",
437
+ "polyline",
438
+ "pre",
439
+ "progress",
440
+ "q",
441
+ "radialGradient",
442
+ "rb",
443
+ "rect",
444
+ "rp",
445
+ "rt",
446
+ "rtc",
447
+ "ruby",
448
+ "s",
449
+ "samp",
450
+ "script",
451
+ "search",
452
+ "section",
453
+ "select",
454
+ "set",
455
+ "slot",
456
+ "small",
457
+ "solidcolor",
458
+ "source",
459
+ "span",
460
+ "stop",
461
+ "strike",
462
+ "strong",
463
+ "style",
464
+ "sub",
465
+ "summary",
466
+ "sup",
467
+ "svg",
468
+ "switch",
469
+ "symbol",
470
+ "table",
471
+ "tbody",
472
+ "td",
473
+ "template",
474
+ "text",
475
+ "textPath",
476
+ "textarea",
477
+ "tfoot",
478
+ "th",
479
+ "thead",
480
+ "time",
481
+ "title",
482
+ "tr",
483
+ "track",
484
+ "tref",
485
+ "tspan",
486
+ "tt",
487
+ "u",
488
+ "ul",
489
+ "use",
490
+ "var",
491
+ "video",
492
+ "view",
493
+ "vkern",
494
+ "wbr",
495
+ "xmp"
496
+ ]);
497
+
498
+ // shared/syntax/src/html/parser.ts
499
+ var VOID = /* @__PURE__ */ new Set([
500
+ "area",
501
+ "base",
502
+ "br",
503
+ "col",
504
+ "embed",
505
+ "hr",
506
+ "img",
507
+ "input",
508
+ "link",
509
+ "meta",
510
+ "param",
511
+ "source",
512
+ "track",
513
+ "wbr"
514
+ ]);
515
+ var RAW = /* @__PURE__ */ new Set(["script", "style", "pre", "textarea", "xmp", "plaintext"]);
516
+ var isWhitespace = /* @__PURE__ */ __name((char) => char === " " || char === " " || char === "\n" || char === "\r", "isWhitespace");
517
+ var isTagNameChar = /* @__PURE__ */ __name((char) => char !== void 0 && /^[A-Za-z0-9_:.-]$/.test(char), "isTagNameChar");
518
+ var isAttributeNameChar = /* @__PURE__ */ __name((char) => char !== void 0 && char.charCodeAt(0) < 127 && char.charCodeAt(0) > 31 && !/[ \t\n"'<>/=]/.test(char), "isAttributeNameChar");
519
+ function tagOf(name) {
520
+ if (tagNames.has(name)) return name;
521
+ const lower = name.toLowerCase();
522
+ return tagNames.has(lower) ? lower : null;
523
+ }
524
+ __name(tagOf, "tagOf");
525
+ var Parser = class {
124
526
  static {
125
- __name(this, "Inlines");
527
+ __name(this, "Parser");
126
528
  }
127
- #labels;
128
- #gfm;
129
- #text = "";
529
+ #source;
130
530
  #position = 0;
131
- #delimiters = null;
132
- #brackets = null;
133
- constructor(labels, gfm) {
134
- this.#labels = labels;
135
- this.#gfm = gfm;
531
+ #comments = [];
532
+ constructor(source) {
533
+ this.#source = source;
136
534
  }
137
- parse(text) {
138
- this.#text = text;
139
- this.#position = 0;
140
- this.#delimiters = null;
141
- this.#brackets = null;
142
- const root = new Item("text", 0, text.length);
143
- while (this.#position < text.length) this.#inline(root);
144
- this.#processEmphasis(null);
145
- return _Inlines.#convert(root, text);
535
+ parse() {
536
+ const source = this.#source;
537
+ const bom = source.startsWith("\uFEFF");
538
+ if (bom) this.#position = 1;
539
+ if (source.startsWith("---", this.#position))
540
+ this.#error("Frontmatter is only valid inside Astro files.");
541
+ this.#trivia();
542
+ const instruction = source.startsWith("<?", this.#position) ? this.#instruction() : null;
543
+ this.#trivia();
544
+ const doctype = source.startsWith("<!", this.#position) && !source.startsWith("<!--", this.#position) && !source.startsWith("<![CDATA[", this.#position) ? this.#doctype() : null;
545
+ const children = this.#children(null);
546
+ return {
547
+ type: "Root",
548
+ start: 0,
549
+ end: source.length,
550
+ bom,
551
+ instruction,
552
+ doctype,
553
+ children,
554
+ comments: this.#comments
555
+ };
146
556
  }
147
- #inline(block) {
148
- const text = this.#text;
149
- const char = text[this.#position];
150
- switch (char) {
151
- case "\n":
152
- this.#newline(block);
153
- return;
154
- case "\\":
155
- this.#backslash(block);
156
- return;
157
- case "`":
158
- this.#backticks(block);
159
- return;
160
- case "~":
161
- if (!this.#gfm) break;
162
- this.#delimiter(block, char);
163
- return;
164
- case "*":
165
- case "_":
166
- this.#delimiter(block, char);
167
- return;
168
- case "!":
169
- if (text[this.#position + 1] !== "[") break;
170
- this.#openBracket(block, true);
171
- return;
172
- case "[":
173
- this.#openBracket(block, false);
174
- return;
175
- case "]":
176
- this.#closeBracket(block);
177
- return;
178
- case "<": {
179
- const rest = text.slice(this.#position);
180
- const match = AUTOLINK.exec(rest) ?? EMAIL.exec(rest);
181
- const tag = match ? null : HTML_TAG.exec(rest);
182
- if (match || tag) {
183
- this.#add(
184
- block,
185
- match ? "autolink" : "html",
186
- (match ?? tag)[0].length
557
+ #error(message, position = this.#position) {
558
+ throw new ParseError(message, position);
559
+ }
560
+ /** Skips whitespace and comments. */
561
+ #trivia() {
562
+ const source = this.#source;
563
+ for (; ; ) {
564
+ while (isWhitespace(source[this.#position])) this.#position++;
565
+ if (!source.startsWith("<!--", this.#position)) return;
566
+ const start = this.#position;
567
+ const close = source.indexOf("-->", start + 4);
568
+ this.#position = close < 0 ? source.length : close + 3;
569
+ this.#comments.push({
570
+ type: "Comment",
571
+ start,
572
+ end: this.#position,
573
+ value: source.slice(start, this.#position)
574
+ });
575
+ }
576
+ }
577
+ /** Children up to the closing tag of `parent`, or the end of the file at the root. */
578
+ #children(parent) {
579
+ const source = this.#source;
580
+ const children = [];
581
+ for (; ; ) {
582
+ this.#trivia();
583
+ if (this.#position >= source.length) {
584
+ if (parent)
585
+ this.#error(
586
+ "Expected a closing tag but instead found the end of the file."
187
587
  );
188
- return;
189
- }
190
- break;
588
+ return children;
191
589
  }
192
- case "&": {
193
- const entity = ENTITY.exec(text.slice(this.#position));
194
- if (entity) {
195
- this.#add(block, "text", entity[0].length);
196
- return;
197
- }
198
- break;
590
+ if (source[this.#position] !== "<") {
591
+ children.push(this.#text());
592
+ continue;
593
+ }
594
+ if (source.startsWith("</", this.#position)) {
595
+ const start = this.#position;
596
+ const name = this.#closingName();
597
+ this.#position = start;
598
+ if (VOID.has(tagOf(name) ?? ""))
599
+ this.#error(
600
+ "Void elements should not have a closing tag. Remove the closing tag.",
601
+ start
602
+ );
603
+ if (!parent)
604
+ this.#error(
605
+ "This closing tag has no matching opening tag.",
606
+ start
607
+ );
608
+ if (name.toLowerCase() !== parent.name.toLowerCase())
609
+ this.#error(
610
+ `Expected a matching closing tag but instead found '${source.slice(start, this.#closingEnd(start))}'.`,
611
+ start
612
+ );
613
+ return children;
614
+ }
615
+ if (source.startsWith("<![CDATA[", this.#position)) {
616
+ const start = this.#position;
617
+ const close = source.indexOf("]]>", start);
618
+ if (close < 0)
619
+ this.#error("Unterminated CDATA section.", start);
620
+ this.#position = close + 3;
621
+ children.push({
622
+ type: "Cdata",
623
+ start,
624
+ end: this.#position,
625
+ value: source.slice(start, this.#position)
626
+ });
627
+ continue;
199
628
  }
629
+ if (source[this.#position + 1] === "!")
630
+ this.#error(
631
+ "Expected an element name but instead found '!'.",
632
+ this.#position + 1
633
+ );
634
+ if (!/[A-Za-z]/.test(source[this.#position + 1] ?? ""))
635
+ this.#error(
636
+ "Unescaped `<` bracket character. Expected a tag or escaped character."
637
+ );
638
+ children.push(this.#element());
200
639
  }
201
- const special = this.#gfm ? /[\n`[\]\\!<&*_~]/g : /[\n`[\]\\!<&*_]/g;
202
- special.lastIndex = this.#position + 1;
203
- const next = special.exec(text);
204
- this.#add(
205
- block,
206
- "text",
207
- (next ? next.index : text.length) - this.#position
208
- );
209
- }
210
- #add(block, kind, length) {
211
- const item = new Item(kind, this.#position, this.#position + length);
212
- block.append(item);
213
- this.#position += length;
214
- return item;
215
640
  }
216
- #newline(block) {
217
- const last = block.last;
218
- let start = this.#position;
219
- let hard = false;
220
- if (last?.kind === "text" && this.#text[last.end - 1] === " ") {
221
- let spaces = last.end;
222
- while (spaces > last.start && this.#text[spaces - 1] === " ")
223
- spaces--;
224
- hard = last.end - spaces >= 2;
225
- start = spaces;
226
- last.end = spaces;
227
- if (last.end === last.start) last.unlink();
641
+ /** Text up to `<`, without trailing whitespace, ending at a blank line unless it has braces. */
642
+ #text() {
643
+ const source = this.#source;
644
+ const start = this.#position;
645
+ let whitespace;
646
+ let newlines = 0;
647
+ let curly = false;
648
+ while (this.#position < source.length) {
649
+ const char = source[this.#position];
650
+ if (char === "<") break;
651
+ if (char === "\n" || char === "\r") {
652
+ whitespace ??= this.#position;
653
+ newlines++;
654
+ if (newlines > 1 && !curly) break;
655
+ } else if (char === " " || char === " ") {
656
+ whitespace ??= this.#position;
657
+ } else {
658
+ if (char === "{" || char === "}") curly = true;
659
+ whitespace = void 0;
660
+ newlines = 0;
661
+ }
662
+ this.#position++;
228
663
  }
229
- const item = new Item(
230
- hard ? "hardbreak" : "softbreak",
664
+ if (whitespace !== void 0) this.#position = whitespace;
665
+ return {
666
+ type: "Text",
231
667
  start,
232
- this.#position + 1
233
- );
234
- block.append(item);
235
- this.#position++;
236
- while (this.#text[this.#position] === " ") this.#position++;
237
- }
238
- #backslash(block) {
239
- const next = this.#text[this.#position + 1];
240
- if (next === "\n") {
241
- this.#add(block, "hardbreak", 2);
242
- while (this.#text[this.#position] === " ") this.#position++;
243
- } else if (next !== void 0 && /[!-/:-@[-`{-~]/.test(next))
244
- this.#add(block, "text", 2);
245
- else this.#add(block, "text", 1);
668
+ end: this.#position,
669
+ value: source.slice(start, this.#position)
670
+ };
246
671
  }
247
- #backticks(block) {
248
- const text = this.#text;
249
- const run = /^`+/.exec(text.slice(this.#position))?.[0];
250
- const pattern = /`+/g;
251
- pattern.lastIndex = this.#position + run.length;
252
- for (let match = pattern.exec(text); match; match = pattern.exec(text))
253
- if (match[0].length === run.length) {
254
- this.#add(
255
- block,
256
- "code",
257
- match.index + run.length - this.#position
672
+ #element() {
673
+ const source = this.#source;
674
+ const start = this.#position;
675
+ this.#position++;
676
+ const nameStart = this.#position;
677
+ while (isTagNameChar(source[this.#position])) this.#position++;
678
+ const name = source.slice(nameStart, this.#position);
679
+ const tag = tagOf(name);
680
+ const attributes = this.#attributes();
681
+ const element = {
682
+ type: "Element",
683
+ start,
684
+ end: 0,
685
+ name,
686
+ tag,
687
+ attributes,
688
+ openingEnd: 0,
689
+ selfClosing: false,
690
+ closing: null,
691
+ raw: false,
692
+ children: []
693
+ };
694
+ if (source[this.#position] === "/") {
695
+ this.#position++;
696
+ this.#expect(">");
697
+ element.selfClosing = true;
698
+ element.openingEnd = element.end = this.#position;
699
+ return element;
700
+ }
701
+ this.#expect(">");
702
+ element.openingEnd = this.#position;
703
+ if (VOID.has(tag ?? "")) {
704
+ element.end = this.#position;
705
+ return element;
706
+ }
707
+ if (RAW.has(tag ?? "")) {
708
+ element.raw = true;
709
+ const contentStart = this.#position;
710
+ const pattern = new RegExp(`</${name}[\\s>/]`, "iy");
711
+ while (this.#position < source.length) {
712
+ pattern.lastIndex = this.#position;
713
+ if (pattern.test(source)) break;
714
+ this.#position++;
715
+ }
716
+ if (this.#position > contentStart)
717
+ element.children.push({
718
+ type: "Text",
719
+ start: contentStart,
720
+ end: this.#position,
721
+ value: source.slice(contentStart, this.#position)
722
+ });
723
+ if (this.#position >= source.length)
724
+ this.#error(
725
+ "Expected a closing tag but instead found the end of the file."
258
726
  );
259
- return;
727
+ } else element.children = this.#children(element);
728
+ const closingStart = this.#position;
729
+ const closingName = this.#closingName();
730
+ this.#position = this.#closingEnd(closingStart);
731
+ element.closing = {
732
+ type: "ClosingTag",
733
+ start: closingStart,
734
+ end: this.#position,
735
+ name: closingName
736
+ };
737
+ element.end = this.#position;
738
+ return element;
739
+ }
740
+ /** The name of the closing tag at the position, leaving the position after it. */
741
+ #closingName() {
742
+ const source = this.#source;
743
+ this.#position += 2;
744
+ const start = this.#position;
745
+ while (isTagNameChar(source[this.#position])) this.#position++;
746
+ return source.slice(start, this.#position);
747
+ }
748
+ /** The end of the closing tag starting at `start`, after its `>`. */
749
+ #closingEnd(start) {
750
+ const source = this.#source;
751
+ let position = start + 2;
752
+ while (isTagNameChar(source[position])) position++;
753
+ while (isWhitespace(source[position])) position++;
754
+ if (source[position] !== ">")
755
+ this.#error("Expected a `>` to end the closing tag.", position);
756
+ return position + 1;
757
+ }
758
+ #expect(char) {
759
+ if (this.#source[this.#position] !== char)
760
+ this.#error(
761
+ this.#position >= this.#source.length ? `Expected \`${char}\` but instead found the end of the file.` : `Expected \`${char}\` but instead found \`${this.#source[this.#position]}\`.`
762
+ );
763
+ this.#position++;
764
+ }
765
+ /** Attributes up to `>`, `/` or `?`. */
766
+ #attributes() {
767
+ const source = this.#source;
768
+ const attributes = [];
769
+ for (; ; ) {
770
+ while (isWhitespace(source[this.#position])) this.#position++;
771
+ const char = source[this.#position];
772
+ if (char === void 0 || char === ">" || char === "/" || char === "?" && source[this.#position + 1] === ">")
773
+ return attributes;
774
+ if (!isAttributeNameChar(char))
775
+ this.#error(`Unexpected character \`${char}\`.`);
776
+ const start = this.#position;
777
+ while (isAttributeNameChar(source[this.#position])) {
778
+ if (source[this.#position] === "?" && source[this.#position + 1] === ">")
779
+ break;
780
+ this.#position++;
260
781
  }
261
- this.#add(block, "text", run.length);
782
+ const nameEnd = this.#position;
783
+ const attribute = {
784
+ type: "Attribute",
785
+ start,
786
+ end: nameEnd,
787
+ name: source.slice(start, nameEnd),
788
+ nameEnd,
789
+ value: null
790
+ };
791
+ let after = this.#position;
792
+ while (isWhitespace(source[after])) after++;
793
+ if (source[after] === "=") {
794
+ this.#position = after + 1;
795
+ while (isWhitespace(source[this.#position])) this.#position++;
796
+ attribute.value = this.#value();
797
+ attribute.end = attribute.value.end;
798
+ }
799
+ attributes.push(attribute);
800
+ }
262
801
  }
263
- #delimiter(block, char) {
264
- const text = this.#text;
802
+ #value() {
803
+ const source = this.#source;
265
804
  const start = this.#position;
266
- let end = start;
267
- while (text[end] === char) end++;
268
- const count = end - start;
269
- const before = start === 0 ? "\n" : _Inlines.#charBefore(text, start);
270
- const after = end >= text.length ? "\n" : _Inlines.#charAt(text, end);
271
- const afterSpace = WHITESPACE.test(after);
272
- const afterPunct = PUNCTUATION.test(after);
273
- const beforeSpace = WHITESPACE.test(before);
274
- const beforePunct = PUNCTUATION.test(before);
275
- const left = !afterSpace && (!afterPunct || beforeSpace || beforePunct);
276
- const right = !beforeSpace && (!beforePunct || afterSpace || afterPunct);
277
- let canOpen;
278
- let canClose;
279
- if (char === "_") {
280
- canOpen = left && (!right || beforePunct);
281
- canClose = right && (!left || afterPunct);
282
- } else {
283
- canOpen = left;
284
- canClose = right;
805
+ const quote = source[start];
806
+ if (quote === '"' || quote === "'") {
807
+ const close = source.indexOf(quote, start + 1);
808
+ if (close < 0) this.#error("Missing closing quote", start);
809
+ this.#position = close + 1;
810
+ return {
811
+ type: "AttributeValue",
812
+ start,
813
+ end: this.#position,
814
+ raw: source.slice(start, this.#position),
815
+ text: source.slice(start + 1, close),
816
+ quote
817
+ };
285
818
  }
286
- if (char === "~" && count > 2) {
287
- canOpen = false;
288
- canClose = false;
819
+ while (this.#position < source.length) {
820
+ const char = source[this.#position];
821
+ if (isWhitespace(char) || char === ">") break;
822
+ if (/[?'"=<`]/.test(char) || char.charCodeAt(0) > 127)
823
+ this.#error("Unexpected character in unquoted attribute value");
824
+ this.#position++;
289
825
  }
290
- const item = this.#add(block, "text", count);
291
- this.#delimiters = {
292
- char,
293
- count,
294
- original: count,
295
- item,
296
- previous: this.#delimiters,
297
- next: null,
298
- canOpen,
299
- canClose
826
+ if (this.#position === start)
827
+ this.#error("Unexpected character in unquoted attribute value");
828
+ const raw = source.slice(start, this.#position);
829
+ return {
830
+ type: "AttributeValue",
831
+ start,
832
+ end: this.#position,
833
+ raw,
834
+ text: raw,
835
+ quote: ""
300
836
  };
301
- if (this.#delimiters.previous)
302
- this.#delimiters.previous.next = this.#delimiters;
303
- }
304
- static #charBefore(text, index) {
305
- const code = text.codePointAt(index - 2);
306
- if (code !== void 0 && code > 65535)
307
- return String.fromCodePoint(code);
308
- return text[index - 1];
309
- }
310
- static #charAt(text, index) {
311
- return String.fromCodePoint(text.codePointAt(index));
312
837
  }
313
- #openBracket(block, image) {
838
+ /** `<!DOCTYPE html>`, or with a literal and quoted identifiers. */
839
+ #doctype() {
840
+ const source = this.#source;
314
841
  const start = this.#position;
315
- const item = this.#add(block, "text", image ? 2 : 1);
316
- if (this.#brackets) this.#brackets.bracketAfter = true;
317
- this.#brackets = {
318
- item,
319
- previous: this.#brackets,
320
- previousDelimiter: this.#delimiters,
321
- index: start + (image ? 2 : 1),
322
- image,
323
- active: true,
324
- bracketAfter: false
842
+ this.#position += 2;
843
+ const word = /* @__PURE__ */ __name(() => {
844
+ while (isWhitespace(source[this.#position])) this.#position++;
845
+ const from = this.#position;
846
+ while (this.#position < source.length && !isWhitespace(source[this.#position]) && !/[>"']/.test(source[this.#position]))
847
+ this.#position++;
848
+ return from < this.#position ? source.slice(from, this.#position) : null;
849
+ }, "word");
850
+ const keyword = word() ?? "";
851
+ const name = word();
852
+ const literal = word();
853
+ const identifiers = [];
854
+ for (let index = 0; index < 2; index++) {
855
+ while (isWhitespace(source[this.#position])) this.#position++;
856
+ const quote = source[this.#position];
857
+ if (quote !== '"' && quote !== "'") break;
858
+ const close = source.indexOf(quote, this.#position + 1);
859
+ if (close < 0) this.#error("Missing closing quote");
860
+ identifiers.push(source.slice(this.#position, close + 1));
861
+ this.#position = close + 1;
862
+ }
863
+ while (isWhitespace(source[this.#position])) this.#position++;
864
+ this.#expect(">");
865
+ return {
866
+ type: "Doctype",
867
+ start,
868
+ end: this.#position,
869
+ keyword,
870
+ name,
871
+ literal,
872
+ identifiers
325
873
  };
326
874
  }
327
- #closeBracket(block) {
328
- const text = this.#text;
329
- const close = this.#position;
330
- this.#position++;
331
- const opener = this.#brackets;
332
- if (!opener) {
333
- block.append(new Item("text", close, close + 1));
334
- return;
335
- }
336
- if (!opener.active) {
337
- block.append(new Item("text", close, close + 1));
338
- this.#brackets = opener.previous;
339
- return;
340
- }
341
- let matched = false;
342
- let reference = null;
343
- let label = null;
344
- let destination = "";
345
- let title = null;
346
- const save = this.#position;
347
- if (text[this.#position] === "(") {
875
+ /** `<?xml version="1.0"?>` at the start of a file. */
876
+ #instruction() {
877
+ const source = this.#source;
878
+ const start = this.#position;
879
+ this.#position += 2;
880
+ const nameStart = this.#position;
881
+ while (isAttributeNameChar(source[this.#position])) {
882
+ if (source.startsWith("?>", this.#position)) break;
348
883
  this.#position++;
349
- this.#spaces();
350
- const parsed = Scan.destination(text, this.#position);
351
- if (parsed && (parsed[0] > this.#position || text[parsed[0]] === ")")) {
352
- destination = parsed[1];
353
- this.#position = parsed[0];
354
- const beforeSpaces = this.#position;
355
- this.#spaces();
356
- if (this.#position > beforeSpaces) {
357
- const end = Scan.title(text, this.#position);
358
- if (end !== void 0) {
359
- title = text.slice(this.#position, end);
360
- this.#position = end;
361
- this.#spaces();
362
- }
363
- }
364
- if (text[this.#position] === ")") {
365
- this.#position++;
366
- matched = true;
367
- }
368
- }
369
- if (!matched) {
370
- this.#position = save;
371
- destination = "";
372
- title = null;
373
- }
374
- }
375
- if (!matched) {
376
- const before = this.#position;
377
- const length = this.#label();
378
- let key;
379
- if (length > 2) key = text.slice(before, before + length);
380
- else if (!opener.bracketAfter)
381
- key = `[${text.slice(opener.index, close)}]`;
382
- if (length === 0) this.#position = save;
383
- if (key && this.#labels.has(Scan.label(key))) {
384
- matched = true;
385
- label = key;
386
- reference = length > 2 ? "full" : length === 2 ? "collapsed" : "shortcut";
387
- } else this.#position = save;
388
884
  }
389
- if (!matched) {
390
- this.#brackets = opener.previous;
391
- block.append(new Item("text", close, close + 1));
885
+ const name = source.slice(nameStart, this.#position);
886
+ const attributes = this.#attributes();
887
+ if (!source.startsWith("?>", this.#position))
888
+ this.#error("Expected `?>` to end the processing instruction.");
889
+ this.#position += 2;
890
+ return {
891
+ type: "Instruction",
892
+ start,
893
+ end: this.#position,
894
+ name,
895
+ attributes
896
+ };
897
+ }
898
+ };
899
+
900
+ // shared/syntax/src/markdown/scan.ts
901
+ var TAGNAME = "[A-Za-z][A-Za-z0-9-]*";
902
+ var ATTRIBUTE = `(?:\\s+[a-zA-Z_:][a-zA-Z0-9:._-]*(?:\\s*=\\s*(?:[^"'=<>\`\\x00-\\x20]+|'[^']*'|"[^"]*"))?)`;
903
+ var OPEN_TAG = `<${TAGNAME}${ATTRIBUTE}*\\s*/?>`;
904
+ var CLOSE_TAG = `</${TAGNAME}\\s*[>]`;
905
+ var Scan = class {
906
+ static {
907
+ __name(this, "Scan");
908
+ }
909
+ /** Normalizes a link label for matching, like `[Foo Bar]` and `[foo bar]`. */
910
+ static label(label) {
911
+ return label.slice(1, -1).trim().replace(/[ \t\r\n]+/g, " ").toLowerCase().toUpperCase();
912
+ }
913
+ /** A link destination, `<...>` or without spaces and with balanced parentheses. */
914
+ static destination(text, from) {
915
+ if (text[from] === "<") {
916
+ for (let index2 = from + 1; index2 < text.length; index2++) {
917
+ const char = text[index2];
918
+ if (char === "\\") index2++;
919
+ else if (char === "\n" || char === "<") return;
920
+ else if (char === ">")
921
+ return [index2 + 1, text.slice(from, index2 + 1)];
922
+ }
392
923
  return;
393
924
  }
394
- const link = new Item(
395
- opener.image ? "image" : "link",
396
- opener.item.start,
397
- this.#position
398
- );
399
- link.tail = text.slice(close + 1, this.#position);
400
- link.reference = reference;
401
- link.label = label;
402
- link.destination = destination;
403
- link.title = title;
404
- let child = opener.item.next;
405
- while (child) {
406
- const next = child.next;
407
- link.append(child);
408
- child = next;
409
- }
410
- block.append(link);
411
- this.#processEmphasis(opener.previousDelimiter);
412
- this.#brackets = opener.previous;
413
- opener.item.unlink();
414
- if (!opener.image) {
415
- for (let bracket = this.#brackets; bracket; bracket = bracket.previous)
416
- if (!bracket.image) bracket.active = false;
925
+ let depth = 0;
926
+ let index = from;
927
+ while (index < text.length) {
928
+ const char = text[index];
929
+ if (char === "\\" && /[!-/:-@[-`{-~]/.test(text[index + 1] ?? "")) {
930
+ index += 2;
931
+ continue;
932
+ }
933
+ if (char === "(") depth++;
934
+ else if (char === ")") {
935
+ if (depth === 0) break;
936
+ depth--;
937
+ } else if (char <= " " || char === "\x7F") break;
938
+ index++;
417
939
  }
940
+ if (depth !== 0) return;
941
+ return [index, text.slice(from, index)];
418
942
  }
419
- /** The length of a link label at the position, 0 when there is none. */
420
- #label() {
421
- const text = this.#text;
422
- if (text[this.#position] !== "[") return 0;
423
- for (let index = this.#position + 1; index < text.length; index++) {
943
+ /** The position after a link title in quotes or parentheses. */
944
+ static title(text, from) {
945
+ const open = text[from];
946
+ const close = open === "(" ? ")" : open;
947
+ if (open !== '"' && open !== "'" && open !== "(") return;
948
+ for (let index = from + 1; index < text.length; index++) {
424
949
  const char = text[index];
425
950
  if (char === "\\") index++;
426
- else if (char === "[") return 0;
427
- else if (char === "]") {
428
- if (index - this.#position > 1e3) return 0;
429
- const length = index + 1 - this.#position;
430
- if (length > 2 && !/[^ \t\n]/.test(text.slice(this.#position + 1, index)))
431
- return 0;
432
- this.#position = index + 1;
433
- return length;
434
- }
951
+ else if (char === close) return index + 1;
952
+ else if (open === "(" && char === "(") return;
953
+ else if (char === "\n" && text[index + 1] === "\n") return;
435
954
  }
436
- return 0;
437
955
  }
438
- #spaces() {
439
- let newline = false;
440
- while (this.#position < this.#text.length) {
441
- const char = this.#text[this.#position];
442
- if (char === "\n" && !newline) newline = true;
443
- else if (char !== " " && char !== " ") break;
444
- this.#position++;
445
- }
956
+ };
957
+
958
+ // shared/syntax/src/markdown/inlines.ts
959
+ var Item = class {
960
+ static {
961
+ __name(this, "Item");
446
962
  }
447
- #remove(delimiter) {
448
- if (delimiter.previous) delimiter.previous.next = delimiter.next;
449
- if (delimiter.next) delimiter.next.previous = delimiter.previous;
450
- else this.#delimiters = delimiter.previous;
963
+ kind;
964
+ start;
965
+ end;
966
+ parent = null;
967
+ first = null;
968
+ last = null;
969
+ prev = null;
970
+ next = null;
971
+ delimiter = "";
972
+ tail = "";
973
+ reference = null;
974
+ label = null;
975
+ destination = "";
976
+ title = null;
977
+ constructor(kind, start, end) {
978
+ this.kind = kind;
979
+ this.start = start;
980
+ this.end = end;
451
981
  }
452
- #processEmphasis(bottom) {
453
- const openersBottom = /* @__PURE__ */ new Map();
454
- let closer = this.#delimiters;
455
- while (closer && closer.previous !== bottom) closer = closer.previous;
456
- while (closer) {
457
- if (!closer.canClose) {
458
- closer = closer.next;
459
- continue;
460
- }
461
- const key = closer.char === "~" ? `~${closer.count}` : `${closer.char}${closer.canOpen}${closer.original % 3}`;
462
- const floor = openersBottom.has(key) ? openersBottom.get(key) : bottom;
463
- let opener = closer.previous;
464
- let found = false;
465
- while (opener && opener !== bottom && opener !== floor) {
466
- if (opener.char === closer.char && opener.canOpen) {
467
- if (closer.char === "~") {
468
- if (opener.count === closer.count) {
469
- found = true;
470
- break;
471
- }
472
- } else {
473
- const odd = (closer.canOpen || opener.canClose) && closer.original % 3 !== 0 && (opener.original + closer.original) % 3 === 0;
474
- if (!odd) {
475
- found = true;
476
- break;
477
- }
478
- }
479
- }
480
- opener = opener.previous;
481
- }
482
- const old = closer;
483
- if (found && opener) {
484
- const use = closer.char === "~" ? closer.count : closer.count >= 2 && opener.count >= 2 ? 2 : 1;
485
- const openItem = opener.item;
486
- const closeItem = closer.item;
487
- opener.count -= use;
488
- closer.count -= use;
489
- openItem.end -= use;
490
- closeItem.start += use;
491
- const emph = new Item(
492
- closer.char === "~" ? "del" : use === 1 ? "emph" : "strong",
493
- openItem.end,
494
- closeItem.start
495
- );
496
- emph.delimiter = closer.char.repeat(use);
497
- let child = openItem.next;
498
- while (child && child !== closeItem) {
499
- const next = child.next;
500
- emph.append(child);
501
- child = next;
502
- }
503
- openItem.insertAfter(emph);
504
- let between = closer.previous;
505
- while (between && between !== opener) {
506
- const previous = between.previous;
507
- this.#remove(between);
508
- between = previous;
509
- }
510
- if (opener.count === 0) {
511
- openItem.unlink();
512
- this.#remove(opener);
513
- }
514
- if (closer.count === 0) {
515
- closeItem.unlink();
516
- const next = closer.next;
517
- this.#remove(closer);
518
- closer = next;
519
- }
520
- } else {
521
- closer = closer.next;
522
- openersBottom.set(key, old.previous);
523
- if (!old.canOpen) this.#remove(old);
524
- }
525
- }
526
- while (this.#delimiters && this.#delimiters !== bottom)
527
- this.#remove(this.#delimiters);
982
+ append(child) {
983
+ child.unlink();
984
+ child.parent = this;
985
+ if (this.last) {
986
+ this.last.next = child;
987
+ child.prev = this.last;
988
+ } else this.first = child;
989
+ this.last = child;
528
990
  }
529
- static #convert(parent, text) {
530
- const inlines = [];
531
- for (let item = parent.first; item; item = item.next) {
532
- const { start, end } = item;
533
- const raw = text.slice(start, end);
534
- const previous = inlines.at(-1);
535
- switch (item.kind) {
536
- case "text":
537
- if (start === end) break;
538
- if (previous?.type === "Text" && previous.end === start) {
539
- previous.end = end;
540
- previous.raw = text.slice(previous.start, end);
541
- } else inlines.push({ type: "Text", start, end, raw });
542
- break;
543
- case "softbreak":
544
- case "hardbreak":
545
- inlines.push({
546
- type: "Break",
547
- start,
548
- end,
549
- hard: item.kind === "hardbreak",
550
- raw
551
- });
552
- break;
553
- case "code":
554
- inlines.push({ type: "Code", start, end, raw });
555
- break;
556
- case "autolink":
557
- inlines.push({ type: "Autolink", start, end, raw });
558
- break;
559
- case "html":
560
- inlines.push({ type: "Html", start, end, raw });
561
- break;
562
- case "emph":
563
- case "strong":
564
- case "del":
565
- inlines.push({
566
- type: item.kind === "emph" ? "Emphasis" : item.kind === "strong" ? "Strong" : "Strikethrough",
567
- start,
568
- end,
569
- delimiter: item.delimiter,
570
- children: _Inlines.#convert(item, text)
571
- });
572
- break;
573
- case "link":
574
- case "image":
575
- inlines.push({
576
- type: item.kind === "link" ? "Link" : "Image",
577
- start,
578
- end,
579
- children: _Inlines.#convert(item, text),
580
- tail: item.tail,
581
- reference: item.reference,
582
- label: item.label,
583
- destination: item.destination,
584
- title: item.title
585
- });
586
- break;
587
- }
588
- }
589
- return inlines;
991
+ insertAfter(sibling) {
992
+ sibling.unlink();
993
+ sibling.next = this.next;
994
+ if (sibling.next) sibling.next.prev = sibling;
995
+ sibling.prev = this;
996
+ this.next = sibling;
997
+ sibling.parent = this.parent;
998
+ if (this.parent && !sibling.next) this.parent.last = sibling;
999
+ }
1000
+ unlink() {
1001
+ if (this.prev) this.prev.next = this.next;
1002
+ else if (this.parent) this.parent.first = this.next;
1003
+ if (this.next) this.next.prev = this.prev;
1004
+ else if (this.parent) this.parent.last = this.prev;
1005
+ this.parent = null;
1006
+ this.next = null;
1007
+ this.prev = null;
590
1008
  }
591
1009
  };
592
-
593
- // shared/syntax/src/markdown/blocks.ts
594
- var htmlOpen = [
595
- /./,
596
- /^<(?:script|pre|textarea|style)(?:\s|>|$)/i,
597
- /^<!--/,
598
- /^<[?]/,
599
- /^<![A-Za-z]/,
600
- /^<!\[CDATA\[/,
601
- /^<[/]?(?:address|article|aside|base|basefont|blockquote|body|caption|center|col|colgroup|dd|details|dialog|dir|div|dl|dt|fieldset|figcaption|figure|footer|form|frame|frameset|h[123456]|head|header|hr|html|iframe|legend|li|link|main|menu|menuitem|nav|noframes|ol|optgroup|option|p|param|search|section|summary|table|tbody|td|tfoot|th|thead|title|tr|track|ul)(?:\s|[/]?[>]|$)/i,
602
- new RegExp(`^(?:${OPEN_TAG}|${CLOSE_TAG})\\s*$`, "i")
603
- ];
604
- var htmlClose = [
605
- /./,
606
- /<\/(?:script|pre|textarea|style)>/i,
607
- /-->/,
608
- /\?>/,
609
- />/,
610
- /\]\]>/
611
- ];
612
- var thematicBreak = /^(?:\*[ \t]*){3,}$|^(?:_[ \t]*){3,}$|^(?:-[ \t]*){3,}$/;
613
- var delimiterRow = /^\|?[ \t]*:?-+:?[ \t]*(?:\|[ \t]*:?-+:?[ \t]*)*\|?[ \t]*$/;
614
- var Blocks = class _Blocks {
1010
+ var ENTITY = /^&(?:#[xX][a-fA-F0-9]{1,6}|#[0-9]{1,7}|[A-Za-z][A-Za-z0-9]{1,31});/;
1011
+ var AUTOLINK = /^<[A-Za-z][A-Za-z0-9.+-]{1,31}:[^<>\p{Cc} ]*>/u;
1012
+ var EMAIL = /^<[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*>/;
1013
+ var HTML_TAG = new RegExp(
1014
+ `^(?:${OPEN_TAG}|${CLOSE_TAG}|<!-->|<!--->|<!--[\\s\\S]*?-->|[<][?][\\s\\S]*?[?][>]|<![A-Za-z][^>]*>|<!\\[CDATA\\[[\\s\\S]*?\\]\\]>)`,
1015
+ "i"
1016
+ );
1017
+ var PUNCTUATION = /[\p{P}\p{S}]/u;
1018
+ var WHITESPACE = /[\s\p{Zs}]/u;
1019
+ var Inlines = class _Inlines {
615
1020
  static {
616
- __name(this, "Blocks");
1021
+ __name(this, "Inlines");
617
1022
  }
618
- #source;
1023
+ #labels;
619
1024
  #gfm;
620
- #lines = [];
621
- #root;
622
- #tip;
623
- #oldTip;
624
- #lastMatched;
625
- #allClosed = true;
626
- #lineNumber = 0;
627
- #line = "";
628
- #lineStart = 0;
629
- #offset = 0;
630
- #column = 0;
631
- #nextNonspace = 0;
632
- #nextNonspaceColumn = 0;
633
- #indent = 0;
634
- #indented = false;
635
- #blank = false;
636
- #partialTab = false;
637
- /** Whether a block took the whole line when it started, like a fence or a delimiter row. */
638
- #consumed = false;
639
- /** Where the content of the line starts, after the prefixes of the containers. */
640
- #contentStart = 0;
641
- constructor(source, options = {}) {
642
- this.#source = source;
643
- this.#gfm = options.gfm ?? true;
644
- this.#root = this.#create("Root", null, 0, 1);
645
- this.#tip = this.#root;
646
- this.#oldTip = this.#root;
647
- this.#lastMatched = this.#root;
648
- const pattern = /\r\n|\r|\n/g;
649
- let start = 0;
650
- for (const match of source.matchAll(pattern)) {
651
- this.#lines.push({
652
- text: source.slice(start, match.index),
653
- start
654
- });
655
- start = match.index + match[0].length;
656
- }
657
- if (start < source.length)
658
- this.#lines.push({ text: source.slice(start), start });
659
- }
660
- parse(frontmatter = false) {
661
- const bom = this.#source.startsWith("\uFEFF");
662
- let first = 0;
663
- let matter = null;
664
- if (bom && this.#lines[0])
665
- this.#lines[0] = {
666
- text: this.#lines[0].text.slice(1),
667
- start: 1
668
- };
669
- if (frontmatter && /^---[ \t]*$/.test(this.#lines[0]?.text ?? "")) {
670
- const close = this.#lines.findIndex(
671
- (line, index) => index > 0 && /^---[ \t]*$/.test(line.text)
672
- );
673
- if (close > 0) {
674
- const end = this.#lines[close];
675
- matter = {
676
- type: "Frontmatter",
677
- start: this.#lines[0]?.start ?? 0,
678
- end: end.start + end.text.length,
679
- line: 1,
680
- endLine: close + 1,
681
- raw: this.#source.slice(
682
- this.#lines[0]?.start ?? 0,
683
- end.start + end.text.length
684
- )
685
- };
686
- first = close + 1;
687
- this.#lineNumber = first;
688
- }
689
- }
690
- for (const { text, start } of this.#lines.slice(first)) {
691
- this.#lineStart = start;
692
- this.#incorporate(text);
693
- }
694
- while (this.#tip !== this.#root) this.#close(this.#tip);
695
- this.#close(this.#root);
696
- const definitions = /* @__PURE__ */ new Map();
697
- const children = this.#blocks(this.#root, definitions);
698
- const inlines = new Inlines(new Set(definitions.keys()), this.#gfm);
699
- _Blocks.#inlines(children, inlines);
700
- return {
701
- type: "Root",
702
- start: 0,
703
- end: this.#source.length,
704
- line: 1,
705
- endLine: Math.max(this.#lines.length, 1),
706
- bom,
707
- frontmatter: matter,
708
- children
709
- };
710
- }
711
- #create(kind, parent, start, line) {
712
- return {
713
- kind,
714
- parent,
715
- children: [],
716
- open: true,
717
- start,
718
- line,
719
- endLine: line,
720
- lines: [],
721
- offsets: [],
722
- markerOffset: 0,
723
- padding: 0,
724
- marker: "",
725
- spaces: 0,
726
- delimiter: "",
727
- ordered: false,
728
- number: null,
729
- fence: "",
730
- fenceOffset: 0,
731
- info: "",
732
- closed: false,
733
- htmlKind: 0,
734
- level: 0,
735
- setext: false,
736
- alignments: [],
737
- definitions: [],
738
- indents: [],
739
- prefixes: [],
740
- lazy: /* @__PURE__ */ new Set()
741
- };
742
- }
743
- // line state, like commonmark.js
744
- #findNextNonspace() {
745
- const line = this.#line;
746
- let index = this.#offset;
747
- let column = this.#column;
748
- let char = line[index];
749
- while (char === " " || char === " ") {
750
- column += char === " " ? 1 : 4 - column % 4;
751
- char = line[++index];
752
- }
753
- this.#blank = index >= line.length;
754
- this.#nextNonspace = index;
755
- this.#nextNonspaceColumn = column;
756
- this.#indent = column - this.#column;
757
- this.#indented = this.#indent >= 4;
1025
+ #text = "";
1026
+ #position = 0;
1027
+ #delimiters = null;
1028
+ #brackets = null;
1029
+ constructor(labels, gfm) {
1030
+ this.#labels = labels;
1031
+ this.#gfm = gfm;
758
1032
  }
759
- #advanceNextNonspace() {
760
- this.#offset = this.#nextNonspace;
761
- this.#column = this.#nextNonspaceColumn;
762
- this.#partialTab = false;
1033
+ parse(text) {
1034
+ this.#text = text;
1035
+ this.#position = 0;
1036
+ this.#delimiters = null;
1037
+ this.#brackets = null;
1038
+ const root = new Item("text", 0, text.length);
1039
+ while (this.#position < text.length) this.#inline(root);
1040
+ this.#processEmphasis(null);
1041
+ return _Inlines.#convert(root, text);
763
1042
  }
764
- #advance(count, columns = false) {
765
- const line = this.#line;
766
- let remaining = count;
767
- let char = line[this.#offset];
768
- while (remaining > 0 && char !== void 0) {
769
- if (char === " ") {
770
- const toTab = 4 - this.#column % 4;
771
- if (columns) {
772
- this.#partialTab = toTab > remaining;
773
- const advance = Math.min(toTab, remaining);
774
- this.#column += advance;
775
- this.#offset += this.#partialTab ? 0 : 1;
776
- remaining -= advance;
777
- } else {
778
- this.#partialTab = false;
779
- this.#column += toTab;
780
- this.#offset += 1;
781
- remaining -= 1;
1043
+ #inline(block) {
1044
+ const text = this.#text;
1045
+ const char = text[this.#position];
1046
+ switch (char) {
1047
+ case "\n":
1048
+ this.#newline(block);
1049
+ return;
1050
+ case "\\":
1051
+ this.#backslash(block);
1052
+ return;
1053
+ case "`":
1054
+ this.#backticks(block);
1055
+ return;
1056
+ case "~":
1057
+ if (!this.#gfm) break;
1058
+ this.#delimiter(block, char);
1059
+ return;
1060
+ case "*":
1061
+ case "_":
1062
+ this.#delimiter(block, char);
1063
+ return;
1064
+ case "!":
1065
+ if (text[this.#position + 1] !== "[") break;
1066
+ this.#openBracket(block, true);
1067
+ return;
1068
+ case "[":
1069
+ this.#openBracket(block, false);
1070
+ return;
1071
+ case "]":
1072
+ this.#closeBracket(block);
1073
+ return;
1074
+ case "<": {
1075
+ const rest = text.slice(this.#position);
1076
+ const match = AUTOLINK.exec(rest) ?? EMAIL.exec(rest);
1077
+ const tag = match ? null : HTML_TAG.exec(rest);
1078
+ if (match || tag) {
1079
+ this.#add(
1080
+ block,
1081
+ match ? "autolink" : "html",
1082
+ (match ?? tag)[0].length
1083
+ );
1084
+ return;
782
1085
  }
783
- } else {
784
- this.#partialTab = false;
785
- this.#offset += 1;
786
- this.#column += 1;
787
- remaining -= 1;
1086
+ break;
1087
+ }
1088
+ case "&": {
1089
+ const entity = ENTITY.exec(text.slice(this.#position));
1090
+ if (entity) {
1091
+ this.#add(block, "text", entity[0].length);
1092
+ return;
1093
+ }
1094
+ break;
788
1095
  }
789
- char = line[this.#offset];
790
1096
  }
1097
+ const special = this.#gfm ? /[\n`[\]\\!<&*_~]/g : /[\n`[\]\\!<&*_]/g;
1098
+ special.lastIndex = this.#position + 1;
1099
+ const next = special.exec(text);
1100
+ this.#add(
1101
+ block,
1102
+ "text",
1103
+ (next ? next.index : text.length) - this.#position
1104
+ );
791
1105
  }
792
- #rest() {
793
- return this.#line.slice(this.#nextNonspace);
1106
+ #add(block, kind, length) {
1107
+ const item = new Item(kind, this.#position, this.#position + length);
1108
+ block.append(item);
1109
+ this.#position += length;
1110
+ return item;
794
1111
  }
795
- #addLine() {
796
- this.#tip.prefixes.push(this.#line.slice(0, this.#offset));
797
- let text = this.#line.slice(this.#offset);
798
- if (this.#partialTab) {
799
- this.#offset += 1;
800
- const spaces = 4 - this.#column % 4;
801
- text = " ".repeat(spaces) + this.#line.slice(this.#offset);
1112
+ #newline(block) {
1113
+ const last = block.last;
1114
+ let start = this.#position;
1115
+ let hard = false;
1116
+ if (last?.kind === "text" && this.#text[last.end - 1] === " ") {
1117
+ let spaces = last.end;
1118
+ while (spaces > last.start && this.#text[spaces - 1] === " ")
1119
+ spaces--;
1120
+ hard = last.end - spaces >= 2;
1121
+ start = spaces;
1122
+ last.end = spaces;
1123
+ if (last.end === last.start) last.unlink();
802
1124
  }
803
- this.#tip.indents.push(
804
- this.#line.slice(
805
- this.#contentStart,
806
- Math.max(this.#offset, this.#contentStart)
807
- )
1125
+ const item = new Item(
1126
+ hard ? "hardbreak" : "softbreak",
1127
+ start,
1128
+ this.#position + 1
808
1129
  );
809
- this.#tip.lines.push(text);
810
- this.#tip.offsets.push(this.#lineStart + this.#offset);
811
- this.#tip.endLine = this.#lineNumber;
1130
+ block.append(item);
1131
+ this.#position++;
1132
+ while (this.#text[this.#position] === " ") this.#position++;
812
1133
  }
813
- #add(kind, offset) {
814
- while (!_Blocks.#canContain(this.#tip.kind, kind))
815
- this.#close(this.#tip);
816
- const block = this.#create(
817
- kind,
818
- this.#tip,
819
- this.#lineStart + offset,
820
- this.#lineNumber
821
- );
822
- this.#tip.children.push(block);
823
- this.#tip = block;
824
- return block;
1134
+ #backslash(block) {
1135
+ const next = this.#text[this.#position + 1];
1136
+ if (next === "\n") {
1137
+ this.#add(block, "hardbreak", 2);
1138
+ while (this.#text[this.#position] === " ") this.#position++;
1139
+ } else if (next !== void 0 && /[!-/:-@[-`{-~]/.test(next))
1140
+ this.#add(block, "text", 2);
1141
+ else this.#add(block, "text", 1);
825
1142
  }
826
- static #canContain(parent, child) {
827
- if (parent === "List") return child === "ListItem";
828
- return (parent === "Root" || parent === "BlockQuote" || parent === "ListItem") && child !== "ListItem";
1143
+ #backticks(block) {
1144
+ const text = this.#text;
1145
+ const run = /^`+/.exec(text.slice(this.#position))?.[0];
1146
+ const pattern = /`+/g;
1147
+ pattern.lastIndex = this.#position + run.length;
1148
+ for (let match = pattern.exec(text); match; match = pattern.exec(text))
1149
+ if (match[0].length === run.length) {
1150
+ this.#add(
1151
+ block,
1152
+ "code",
1153
+ match.index + run.length - this.#position
1154
+ );
1155
+ return;
1156
+ }
1157
+ this.#add(block, "text", run.length);
829
1158
  }
830
- #closeUnmatched() {
831
- if (this.#allClosed) return;
832
- while (this.#oldTip !== this.#lastMatched) {
833
- const parent = this.#oldTip.parent;
834
- this.#close(this.#oldTip);
835
- this.#oldTip = parent;
1159
+ #delimiter(block, char) {
1160
+ const text = this.#text;
1161
+ const start = this.#position;
1162
+ let end = start;
1163
+ while (text[end] === char) end++;
1164
+ const count = end - start;
1165
+ const before = start === 0 ? "\n" : _Inlines.#charBefore(text, start);
1166
+ const after = end >= text.length ? "\n" : _Inlines.#charAt(text, end);
1167
+ const afterSpace = WHITESPACE.test(after);
1168
+ const afterPunct = PUNCTUATION.test(after);
1169
+ const beforeSpace = WHITESPACE.test(before);
1170
+ const beforePunct = PUNCTUATION.test(before);
1171
+ const left = !afterSpace && (!afterPunct || beforeSpace || beforePunct);
1172
+ const right = !beforeSpace && (!beforePunct || afterSpace || afterPunct);
1173
+ let canOpen;
1174
+ let canClose;
1175
+ if (char === "_") {
1176
+ canOpen = left && (!right || beforePunct);
1177
+ canClose = right && (!left || afterPunct);
1178
+ } else {
1179
+ canOpen = left;
1180
+ canClose = right;
836
1181
  }
837
- this.#allClosed = true;
838
- }
839
- #close(block) {
840
- const parent = block.parent;
841
- block.open = false;
842
- if (block.kind === "Paragraph") this.#takeDefinitions(block);
843
- if (block.kind === "CodeBlock" && !block.fence) {
844
- while (block.lines.length > 0 && /^[ \t]*$/.test(block.lines.at(-1) ?? "")) {
845
- block.lines.pop();
846
- block.offsets.pop();
847
- block.prefixes.pop();
848
- }
849
- block.endLine = block.line + Math.max(block.lines.length - 1, 0);
1182
+ if (char === "~" && count > 2) {
1183
+ canOpen = false;
1184
+ canClose = false;
850
1185
  }
851
- if (block.children.length > 0)
852
- block.endLine = Math.max(
853
- block.endLine,
854
- ...block.children.map((child) => child.endLine)
855
- );
856
- if (parent) this.#tip = parent;
1186
+ const item = this.#add(block, "text", count);
1187
+ this.#delimiters = {
1188
+ char,
1189
+ count,
1190
+ original: count,
1191
+ item,
1192
+ previous: this.#delimiters,
1193
+ next: null,
1194
+ canOpen,
1195
+ canClose
1196
+ };
1197
+ if (this.#delimiters.previous)
1198
+ this.#delimiters.previous.next = this.#delimiters;
857
1199
  }
858
- /** Continues an open block on the current line: 0 matched, 1 not, 2 the line is consumed. */
859
- #continue(block) {
860
- const line = this.#line;
861
- switch (block.kind) {
862
- case "BlockQuote":
863
- if (!this.#indented && line[this.#nextNonspace] === ">") {
864
- this.#advanceNextNonspace();
865
- this.#advance(1);
866
- if (line[this.#offset] === " " || line[this.#offset] === " ")
867
- this.#advance(1, true);
868
- block.endLine = this.#lineNumber;
869
- return 0;
870
- }
871
- return 1;
872
- case "ListItem":
873
- if (this.#blank) {
874
- if (block.children.length === 0) return 1;
875
- this.#advanceNextNonspace();
876
- return 0;
877
- }
878
- if (this.#indent >= block.markerOffset + block.padding) {
879
- this.#advance(block.markerOffset + block.padding, true);
880
- return 0;
881
- }
882
- return 1;
883
- case "CodeBlock": {
884
- if (block.fence) {
885
- const match = this.#indent <= 3 && line[this.#nextNonspace] === block.fence[0] ? /^(?:`{3,}|~{3,})(?=[ \t]*$)/.exec(this.#rest()) : null;
886
- if (match && match[0].length >= block.fence.length) {
887
- block.closed = true;
888
- block.endLine = this.#lineNumber;
889
- this.#close(block);
890
- return 2;
891
- }
892
- let skip = block.fenceOffset;
893
- while (skip > 0 && (line[this.#offset] === " " || line[this.#offset] === " ")) {
894
- this.#advance(1, true);
895
- skip--;
1200
+ static #charBefore(text, index) {
1201
+ const code = text.codePointAt(index - 2);
1202
+ if (code !== void 0 && code > 65535)
1203
+ return String.fromCodePoint(code);
1204
+ return text[index - 1];
1205
+ }
1206
+ static #charAt(text, index) {
1207
+ return String.fromCodePoint(text.codePointAt(index));
1208
+ }
1209
+ #openBracket(block, image) {
1210
+ const start = this.#position;
1211
+ const item = this.#add(block, "text", image ? 2 : 1);
1212
+ if (this.#brackets) this.#brackets.bracketAfter = true;
1213
+ this.#brackets = {
1214
+ item,
1215
+ previous: this.#brackets,
1216
+ previousDelimiter: this.#delimiters,
1217
+ index: start + (image ? 2 : 1),
1218
+ image,
1219
+ active: true,
1220
+ bracketAfter: false
1221
+ };
1222
+ }
1223
+ #closeBracket(block) {
1224
+ const text = this.#text;
1225
+ const close = this.#position;
1226
+ this.#position++;
1227
+ const opener = this.#brackets;
1228
+ if (!opener) {
1229
+ block.append(new Item("text", close, close + 1));
1230
+ return;
1231
+ }
1232
+ if (!opener.active) {
1233
+ block.append(new Item("text", close, close + 1));
1234
+ this.#brackets = opener.previous;
1235
+ return;
1236
+ }
1237
+ let matched = false;
1238
+ let reference = null;
1239
+ let label = null;
1240
+ let destination = "";
1241
+ let title = null;
1242
+ const save = this.#position;
1243
+ if (text[this.#position] === "(") {
1244
+ this.#position++;
1245
+ this.#spaces();
1246
+ const parsed = Scan.destination(text, this.#position);
1247
+ if (parsed && (parsed[0] > this.#position || text[parsed[0]] === ")")) {
1248
+ destination = parsed[1];
1249
+ this.#position = parsed[0];
1250
+ const beforeSpaces = this.#position;
1251
+ this.#spaces();
1252
+ if (this.#position > beforeSpaces) {
1253
+ const end = Scan.title(text, this.#position);
1254
+ if (end !== void 0) {
1255
+ title = text.slice(this.#position, end);
1256
+ this.#position = end;
1257
+ this.#spaces();
896
1258
  }
897
- return 0;
898
- }
899
- if (this.#indent >= 4) {
900
- this.#advance(4, true);
901
- return 0;
902
1259
  }
903
- if (this.#blank) {
904
- this.#advanceNextNonspace();
905
- return 0;
1260
+ if (text[this.#position] === ")") {
1261
+ this.#position++;
1262
+ matched = true;
906
1263
  }
907
- return 1;
908
1264
  }
909
- case "HtmlBlock":
910
- return this.#blank && (block.htmlKind === 6 || block.htmlKind === 7) ? 1 : 0;
911
- case "Paragraph":
912
- case "Table":
913
- return this.#blank ? 1 : 0;
914
- case "Heading":
915
- case "ThematicBreak":
916
- return 1;
917
- default:
918
- return 0;
1265
+ if (!matched) {
1266
+ this.#position = save;
1267
+ destination = "";
1268
+ title = null;
1269
+ }
919
1270
  }
920
- }
921
- #incorporate(text) {
922
- let container = this.#root;
923
- this.#oldTip = this.#tip;
924
- this.#offset = 0;
925
- this.#column = 0;
926
- this.#blank = false;
927
- this.#partialTab = false;
928
- this.#consumed = false;
929
- this.#lineNumber++;
930
- this.#line = text.includes("\0") ? text.replaceAll("\0", "\uFFFD") : text;
931
- for (; ; ) {
932
- const last = container.children.at(-1);
933
- if (!last?.open) break;
934
- container = last;
935
- this.#findNextNonspace();
936
- const result = this.#continue(container);
937
- if (result === 2) return;
938
- if (result === 1) {
939
- container = container.parent;
940
- break;
941
- }
942
- }
943
- this.#allClosed = container === this.#oldTip;
944
- this.#lastMatched = container;
945
- this.#contentStart = this.#offset;
946
- let matchedLeaf = container.kind !== "Paragraph" && container.kind !== "Table" && (container.kind === "CodeBlock" || container.kind === "HtmlBlock");
947
- while (!matchedLeaf) {
948
- this.#findNextNonspace();
949
- if (!this.#indented && !/^[#`~*+_=<>0-9-|:]/.test(this.#rest())) {
950
- this.#advanceNextNonspace();
951
- break;
952
- }
953
- const result = this.#start(container);
954
- if (result === 0) {
955
- this.#advanceNextNonspace();
956
- break;
957
- }
958
- container = this.#tip;
959
- if (result === 1) this.#contentStart = this.#offset;
960
- if (result === 2) matchedLeaf = true;
1271
+ if (!matched) {
1272
+ const before = this.#position;
1273
+ const length = this.#label();
1274
+ let key;
1275
+ if (length > 2) key = text.slice(before, before + length);
1276
+ else if (!opener.bracketAfter)
1277
+ key = `[${text.slice(opener.index, close)}]`;
1278
+ if (length === 0) this.#position = save;
1279
+ if (key && this.#labels.has(Scan.label(key))) {
1280
+ matched = true;
1281
+ label = key;
1282
+ reference = length > 2 ? "full" : length === 2 ? "collapsed" : "shortcut";
1283
+ } else this.#position = save;
961
1284
  }
962
- if (!this.#allClosed && !this.#blank && this.#tip.kind === "Paragraph") {
963
- this.#tip.lazy.add(this.#lineNumber);
964
- this.#addLine();
965
- for (let block = this.#tip; block; block = block.parent)
966
- if (block.kind === "BlockQuote" || block.kind === "ListItem")
967
- block.endLine = this.#lineNumber;
1285
+ if (!matched) {
1286
+ this.#brackets = opener.previous;
1287
+ block.append(new Item("text", close, close + 1));
968
1288
  return;
969
1289
  }
970
- this.#closeUnmatched();
971
- const kind = container.kind;
972
- if (this.#consumed) return;
973
- if (kind === "CodeBlock" || kind === "HtmlBlock") {
974
- this.#addLine();
975
- if (kind === "HtmlBlock" && container.htmlKind >= 1 && container.htmlKind <= 5 && htmlClose[container.htmlKind]?.test(
976
- this.#line.slice(this.#offset)
977
- ))
978
- this.#close(container);
979
- } else if (kind === "Table") {
980
- if (!this.#blank) this.#addLine();
981
- } else if (kind === "Paragraph") {
982
- if (!this.#blank) this.#addLine();
983
- } else if (this.#offset < this.#line.length && !this.#blank) {
984
- this.#add("Paragraph", this.#nextNonspace);
985
- this.#advanceNextNonspace();
986
- this.#addLine();
1290
+ const link = new Item(
1291
+ opener.image ? "image" : "link",
1292
+ opener.item.start,
1293
+ this.#position
1294
+ );
1295
+ link.tail = text.slice(close + 1, this.#position);
1296
+ link.reference = reference;
1297
+ link.label = label;
1298
+ link.destination = destination;
1299
+ link.title = title;
1300
+ let child = opener.item.next;
1301
+ while (child) {
1302
+ const next = child.next;
1303
+ link.append(child);
1304
+ child = next;
987
1305
  }
988
- }
989
- /** Tries to start a block: 0 none, 1 a container, 2 a leaf that takes the rest of the line. */
990
- #start(container) {
991
- const line = this.#line;
992
- const rest = this.#rest();
993
- const char = line[this.#nextNonspace];
994
- if (!this.#indented && char === ">") {
995
- this.#advanceNextNonspace();
996
- this.#advance(1);
997
- if (line[this.#offset] === " " || line[this.#offset] === " ")
998
- this.#advance(1, true);
999
- this.#closeUnmatched();
1000
- this.#add("BlockQuote", this.#nextNonspace);
1001
- return 1;
1306
+ block.append(link);
1307
+ this.#processEmphasis(opener.previousDelimiter);
1308
+ this.#brackets = opener.previous;
1309
+ opener.item.unlink();
1310
+ if (!opener.image) {
1311
+ for (let bracket = this.#brackets; bracket; bracket = bracket.previous)
1312
+ if (!bracket.image) bracket.active = false;
1002
1313
  }
1003
- if (!this.#indented) {
1004
- const atx = /^#{1,6}(?:[ \t]+|$)/.exec(rest);
1005
- if (atx) {
1006
- const start = this.#nextNonspace;
1007
- this.#advanceNextNonspace();
1008
- this.#advance(atx[0].length);
1009
- this.#closeUnmatched();
1010
- const heading = this.#add("Heading", start);
1011
- heading.level = atx[0].trim().length;
1012
- heading.lines.push(
1013
- line.slice(this.#offset).replace(/^[ \t]*#+[ \t]*$/, "").replace(/[ \t]+#+[ \t]*$/, "")
1014
- );
1015
- heading.offsets.push(this.#lineStart + this.#offset);
1016
- this.#advance(line.length - this.#offset);
1017
- return 2;
1314
+ }
1315
+ /** The length of a link label at the position, 0 when there is none. */
1316
+ #label() {
1317
+ const text = this.#text;
1318
+ if (text[this.#position] !== "[") return 0;
1319
+ for (let index = this.#position + 1; index < text.length; index++) {
1320
+ const char = text[index];
1321
+ if (char === "\\") index++;
1322
+ else if (char === "[") return 0;
1323
+ else if (char === "]") {
1324
+ if (index - this.#position > 1e3) return 0;
1325
+ const length = index + 1 - this.#position;
1326
+ if (length > 2 && !/[^ \t\n]/.test(text.slice(this.#position + 1, index)))
1327
+ return 0;
1328
+ this.#position = index + 1;
1329
+ return length;
1018
1330
  }
1019
- const fence = /^`{3,}(?!.*`)|^~{3,}/.exec(rest);
1020
- if (fence) {
1021
- const start = this.#nextNonspace;
1022
- this.#closeUnmatched();
1023
- const code = this.#add("CodeBlock", start);
1024
- code.fence = fence[0];
1025
- code.fenceOffset = this.#indent;
1026
- code.info = rest.slice(fence[0].length).trim();
1027
- this.#advanceNextNonspace();
1028
- this.#advance(line.length - this.#offset);
1029
- this.#consumed = true;
1030
- return 2;
1331
+ }
1332
+ return 0;
1333
+ }
1334
+ #spaces() {
1335
+ let newline = false;
1336
+ while (this.#position < this.#text.length) {
1337
+ const char = this.#text[this.#position];
1338
+ if (char === "\n" && !newline) newline = true;
1339
+ else if (char !== " " && char !== " ") break;
1340
+ this.#position++;
1341
+ }
1342
+ }
1343
+ #remove(delimiter) {
1344
+ if (delimiter.previous) delimiter.previous.next = delimiter.next;
1345
+ if (delimiter.next) delimiter.next.previous = delimiter.previous;
1346
+ else this.#delimiters = delimiter.previous;
1347
+ }
1348
+ #processEmphasis(bottom) {
1349
+ const openersBottom = /* @__PURE__ */ new Map();
1350
+ let closer = this.#delimiters;
1351
+ while (closer && closer.previous !== bottom) closer = closer.previous;
1352
+ while (closer) {
1353
+ if (!closer.canClose) {
1354
+ closer = closer.next;
1355
+ continue;
1031
1356
  }
1032
- if (char === "<") {
1033
- for (let kind = 1; kind <= 7; kind++) {
1034
- if (htmlOpen[kind]?.test(rest) && (kind < 7 || container.kind !== "Paragraph" && !(!this.#allClosed && !this.#blank && this.#tip.kind === "Paragraph"))) {
1035
- this.#closeUnmatched();
1036
- const html = this.#add("HtmlBlock", this.#offset);
1037
- html.htmlKind = kind;
1038
- return 2;
1357
+ const key = closer.char === "~" ? `~${closer.count}` : `${closer.char}${closer.canOpen}${closer.original % 3}`;
1358
+ const floor = openersBottom.has(key) ? openersBottom.get(key) : bottom;
1359
+ let opener = closer.previous;
1360
+ let found = false;
1361
+ while (opener && opener !== bottom && opener !== floor) {
1362
+ if (opener.char === closer.char && opener.canOpen) {
1363
+ if (closer.char === "~") {
1364
+ if (opener.count === closer.count) {
1365
+ found = true;
1366
+ break;
1367
+ }
1368
+ } else {
1369
+ const odd = (closer.canOpen || opener.canClose) && closer.original % 3 !== 0 && (opener.original + closer.original) % 3 === 0;
1370
+ if (!odd) {
1371
+ found = true;
1372
+ break;
1373
+ }
1039
1374
  }
1040
1375
  }
1376
+ opener = opener.previous;
1041
1377
  }
1042
- if (this.#gfm && container.kind === "Paragraph" && this.#allClosed && delimiterRow.test(rest) && rest.includes("-")) {
1043
- const header = container.lines.at(-1) ?? "";
1044
- const delimiters = _Blocks.cells(rest);
1045
- if ((rest.includes("|") || /(?:^|[^\\])\|/.test(header)) && _Blocks.cells(header).length === delimiters.length) {
1046
- this.#closeUnmatched();
1047
- this.#table(container, delimiters);
1048
- return 2;
1378
+ const old = closer;
1379
+ if (found && opener) {
1380
+ const use = closer.char === "~" ? closer.count : closer.count >= 2 && opener.count >= 2 ? 2 : 1;
1381
+ const openItem = opener.item;
1382
+ const closeItem = closer.item;
1383
+ opener.count -= use;
1384
+ closer.count -= use;
1385
+ openItem.end -= use;
1386
+ closeItem.start += use;
1387
+ const emph = new Item(
1388
+ closer.char === "~" ? "del" : use === 1 ? "emph" : "strong",
1389
+ openItem.end,
1390
+ closeItem.start
1391
+ );
1392
+ emph.delimiter = closer.char.repeat(use);
1393
+ let child = openItem.next;
1394
+ while (child && child !== closeItem) {
1395
+ const next = child.next;
1396
+ emph.append(child);
1397
+ child = next;
1049
1398
  }
1050
- }
1051
- if (container.kind === "Paragraph" && /^(?:=+|-+)[ \t]*$/.test(rest)) {
1052
- this.#closeUnmatched();
1053
- this.#takeDefinitions(container);
1054
- if (container.lines.length > 0) {
1055
- container.info = rest.trim();
1056
- container.kind = "Heading";
1057
- container.setext = true;
1058
- container.level = char === "=" ? 1 : 2;
1059
- container.endLine = this.#lineNumber;
1060
- this.#advance(line.length - this.#offset);
1061
- return 2;
1399
+ openItem.insertAfter(emph);
1400
+ let between = closer.previous;
1401
+ while (between && between !== opener) {
1402
+ const previous = between.previous;
1403
+ this.#remove(between);
1404
+ between = previous;
1405
+ }
1406
+ if (opener.count === 0) {
1407
+ openItem.unlink();
1408
+ this.#remove(opener);
1409
+ }
1410
+ if (closer.count === 0) {
1411
+ closeItem.unlink();
1412
+ const next = closer.next;
1413
+ this.#remove(closer);
1414
+ closer = next;
1062
1415
  }
1416
+ } else {
1417
+ closer = closer.next;
1418
+ openersBottom.set(key, old.previous);
1419
+ if (!old.canOpen) this.#remove(old);
1063
1420
  }
1064
- if (thematicBreak.test(rest)) {
1065
- const start = this.#nextNonspace;
1066
- this.#closeUnmatched();
1067
- const block = this.#add("ThematicBreak", start);
1068
- block.lines.push(rest.trimEnd());
1069
- this.#advance(line.length - this.#offset);
1070
- return 2;
1421
+ }
1422
+ while (this.#delimiters && this.#delimiters !== bottom)
1423
+ this.#remove(this.#delimiters);
1424
+ }
1425
+ static #convert(parent, text) {
1426
+ const inlines = [];
1427
+ for (let item = parent.first; item; item = item.next) {
1428
+ const { start, end } = item;
1429
+ const raw = text.slice(start, end);
1430
+ const previous = inlines.at(-1);
1431
+ switch (item.kind) {
1432
+ case "text":
1433
+ if (start === end) break;
1434
+ if (previous?.type === "Text" && previous.end === start) {
1435
+ previous.end = end;
1436
+ previous.raw = text.slice(previous.start, end);
1437
+ } else inlines.push({ type: "Text", start, end, raw });
1438
+ break;
1439
+ case "softbreak":
1440
+ case "hardbreak":
1441
+ inlines.push({
1442
+ type: "Break",
1443
+ start,
1444
+ end,
1445
+ hard: item.kind === "hardbreak",
1446
+ raw
1447
+ });
1448
+ break;
1449
+ case "code":
1450
+ inlines.push({ type: "Code", start, end, raw });
1451
+ break;
1452
+ case "autolink":
1453
+ inlines.push({ type: "Autolink", start, end, raw });
1454
+ break;
1455
+ case "html":
1456
+ inlines.push({ type: "Html", start, end, raw });
1457
+ break;
1458
+ case "emph":
1459
+ case "strong":
1460
+ case "del":
1461
+ inlines.push({
1462
+ type: item.kind === "emph" ? "Emphasis" : item.kind === "strong" ? "Strong" : "Strikethrough",
1463
+ start,
1464
+ end,
1465
+ delimiter: item.delimiter,
1466
+ children: _Inlines.#convert(item, text)
1467
+ });
1468
+ break;
1469
+ case "link":
1470
+ case "image":
1471
+ inlines.push({
1472
+ type: item.kind === "link" ? "Link" : "Image",
1473
+ start,
1474
+ end,
1475
+ children: _Inlines.#convert(item, text),
1476
+ tail: item.tail,
1477
+ reference: item.reference,
1478
+ label: item.label,
1479
+ destination: item.destination,
1480
+ title: item.title
1481
+ });
1482
+ break;
1071
1483
  }
1072
1484
  }
1073
- if (!this.#indented || container.kind === "List") {
1074
- const item = this.#listMarker(container);
1075
- if (item) return 1;
1485
+ return inlines;
1486
+ }
1487
+ };
1488
+
1489
+ // shared/syntax/src/markdown/blocks.ts
1490
+ var htmlOpen = [
1491
+ /./,
1492
+ /^<(?:script|pre|textarea|style)(?:\s|>|$)/i,
1493
+ /^<!--/,
1494
+ /^<[?]/,
1495
+ /^<![A-Za-z]/,
1496
+ /^<!\[CDATA\[/,
1497
+ /^<[/]?(?:address|article|aside|base|basefont|blockquote|body|caption|center|col|colgroup|dd|details|dialog|dir|div|dl|dt|fieldset|figcaption|figure|footer|form|frame|frameset|h[123456]|head|header|hr|html|iframe|legend|li|link|main|menu|menuitem|nav|noframes|ol|optgroup|option|p|param|search|section|summary|table|tbody|td|tfoot|th|thead|title|tr|track|ul)(?:\s|[/]?[>]|$)/i,
1498
+ new RegExp(`^(?:${OPEN_TAG}|${CLOSE_TAG})\\s*$`, "i")
1499
+ ];
1500
+ var htmlClose = [
1501
+ /./,
1502
+ /<\/(?:script|pre|textarea|style)>/i,
1503
+ /-->/,
1504
+ /\?>/,
1505
+ />/,
1506
+ /\]\]>/
1507
+ ];
1508
+ var thematicBreak = /^(?:\*[ \t]*){3,}$|^(?:_[ \t]*){3,}$|^(?:-[ \t]*){3,}$/;
1509
+ var delimiterRow = /^\|?[ \t]*:?-+:?[ \t]*(?:\|[ \t]*:?-+:?[ \t]*)*\|?[ \t]*$/;
1510
+ var Blocks = class _Blocks {
1511
+ static {
1512
+ __name(this, "Blocks");
1513
+ }
1514
+ #source;
1515
+ #gfm;
1516
+ #lines = [];
1517
+ #root;
1518
+ #tip;
1519
+ #oldTip;
1520
+ #lastMatched;
1521
+ #allClosed = true;
1522
+ #lineNumber = 0;
1523
+ #line = "";
1524
+ #lineStart = 0;
1525
+ #offset = 0;
1526
+ #column = 0;
1527
+ #nextNonspace = 0;
1528
+ #nextNonspaceColumn = 0;
1529
+ #indent = 0;
1530
+ #indented = false;
1531
+ #blank = false;
1532
+ #partialTab = false;
1533
+ /** Whether a block took the whole line when it started, like a fence or a delimiter row. */
1534
+ #consumed = false;
1535
+ /** Where the content of the line starts, after the prefixes of the containers. */
1536
+ #contentStart = 0;
1537
+ constructor(source, options = {}) {
1538
+ this.#source = source;
1539
+ this.#gfm = options.gfm ?? true;
1540
+ this.#root = this.#create("Root", null, 0, 1);
1541
+ this.#tip = this.#root;
1542
+ this.#oldTip = this.#root;
1543
+ this.#lastMatched = this.#root;
1544
+ const pattern = /\r\n|\r|\n/g;
1545
+ let start = 0;
1546
+ for (const match of source.matchAll(pattern)) {
1547
+ this.#lines.push({
1548
+ text: source.slice(start, match.index),
1549
+ start
1550
+ });
1551
+ start = match.index + match[0].length;
1076
1552
  }
1077
- if (this.#indented && this.#tip.kind !== "Paragraph" && !this.#blank) {
1078
- this.#advance(4, true);
1079
- this.#closeUnmatched();
1080
- this.#add("CodeBlock", this.#offset);
1081
- return 2;
1553
+ if (start < source.length)
1554
+ this.#lines.push({ text: source.slice(start), start });
1555
+ }
1556
+ parse(frontmatter = false) {
1557
+ const bom = this.#source.startsWith("\uFEFF");
1558
+ let first = 0;
1559
+ let matter = null;
1560
+ if (bom && this.#lines[0])
1561
+ this.#lines[0] = {
1562
+ text: this.#lines[0].text.slice(1),
1563
+ start: 1
1564
+ };
1565
+ if (frontmatter && /^---[ \t]*$/.test(this.#lines[0]?.text ?? "")) {
1566
+ const close = this.#lines.findIndex(
1567
+ (line, index) => index > 0 && /^---[ \t]*$/.test(line.text)
1568
+ );
1569
+ if (close > 0) {
1570
+ const end = this.#lines[close];
1571
+ matter = {
1572
+ type: "Frontmatter",
1573
+ start: this.#lines[0]?.start ?? 0,
1574
+ end: end.start + end.text.length,
1575
+ line: 1,
1576
+ endLine: close + 1,
1577
+ raw: this.#source.slice(
1578
+ this.#lines[0]?.start ?? 0,
1579
+ end.start + end.text.length
1580
+ )
1581
+ };
1582
+ first = close + 1;
1583
+ this.#lineNumber = first;
1584
+ }
1082
1585
  }
1083
- return 0;
1586
+ for (const { text, start } of this.#lines.slice(first)) {
1587
+ this.#lineStart = start;
1588
+ this.#incorporate(text);
1589
+ }
1590
+ while (this.#tip !== this.#root) this.#close(this.#tip);
1591
+ this.#close(this.#root);
1592
+ const definitions = /* @__PURE__ */ new Map();
1593
+ const children = this.#blocks(this.#root, definitions);
1594
+ const inlines = new Inlines(new Set(definitions.keys()), this.#gfm);
1595
+ _Blocks.#inlines(children, inlines);
1596
+ return {
1597
+ type: "Root",
1598
+ start: 0,
1599
+ end: this.#source.length,
1600
+ line: 1,
1601
+ endLine: Math.max(this.#lines.length, 1),
1602
+ bom,
1603
+ frontmatter: matter,
1604
+ children
1605
+ };
1084
1606
  }
1085
- /** Turns the last line of a paragraph and the delimiter row into a table. */
1086
- #table(paragraph, delimiters) {
1087
- const headerLine = paragraph.lines.pop();
1088
- const headerOffset = paragraph.offsets.pop();
1089
- paragraph.indents.pop();
1090
- paragraph.prefixes.pop();
1091
- const parent = paragraph.parent;
1092
- const table = this.#create(
1093
- "Table",
1607
+ #create(kind, parent, start, line) {
1608
+ return {
1609
+ kind,
1094
1610
  parent,
1095
- headerOffset,
1096
- paragraph.line + paragraph.lines.length
1097
- );
1098
- table.alignments = delimiters.map((cell) => {
1099
- const left = cell.startsWith(":");
1100
- const right = cell.endsWith(":");
1101
- return left && right ? "center" : left ? "left" : right ? "right" : null;
1102
- });
1103
- table.lines.push(headerLine, this.#line.slice(this.#nextNonspace));
1104
- table.offsets.push(headerOffset, this.#lineStart + this.#nextNonspace);
1105
- table.endLine = this.#lineNumber;
1106
- if (paragraph.lines.length === 0) {
1107
- parent.children.splice(
1108
- parent.children.indexOf(paragraph),
1109
- 1,
1110
- table
1111
- );
1112
- } else {
1113
- paragraph.endLine = table.line - 1;
1114
- paragraph.open = false;
1115
- this.#takeDefinitions(paragraph);
1116
- parent.children.push(table);
1611
+ children: [],
1612
+ open: true,
1613
+ start,
1614
+ line,
1615
+ endLine: line,
1616
+ lines: [],
1617
+ offsets: [],
1618
+ markerOffset: 0,
1619
+ padding: 0,
1620
+ marker: "",
1621
+ spaces: 0,
1622
+ delimiter: "",
1623
+ ordered: false,
1624
+ number: null,
1625
+ fence: "",
1626
+ fenceOffset: 0,
1627
+ info: "",
1628
+ closed: false,
1629
+ htmlKind: 0,
1630
+ level: 0,
1631
+ setext: false,
1632
+ alignments: [],
1633
+ definitions: [],
1634
+ indents: [],
1635
+ prefixes: [],
1636
+ lazy: /* @__PURE__ */ new Set()
1637
+ };
1638
+ }
1639
+ // line state, like commonmark.js
1640
+ #findNextNonspace() {
1641
+ const line = this.#line;
1642
+ let index = this.#offset;
1643
+ let column = this.#column;
1644
+ let char = line[index];
1645
+ while (char === " " || char === " ") {
1646
+ column += char === " " ? 1 : 4 - column % 4;
1647
+ char = line[++index];
1117
1648
  }
1118
- this.#tip = table;
1119
- this.#advance(this.#line.length - this.#offset);
1120
- this.#consumed = true;
1649
+ this.#blank = index >= line.length;
1650
+ this.#nextNonspace = index;
1651
+ this.#nextNonspaceColumn = column;
1652
+ this.#indent = column - this.#column;
1653
+ this.#indented = this.#indent >= 4;
1121
1654
  }
1122
- #listMarker(container) {
1123
- if (this.#indent >= 4) return false;
1124
- const rest = this.#rest();
1125
- const bullet = /^[*+-]/.exec(rest);
1126
- const ordered = bullet ? null : /^(\d{1,9})([.)])/.exec(rest);
1127
- const match = bullet ?? ordered;
1128
- if (!match) return false;
1129
- if (ordered && container.kind === "Paragraph" && Number(ordered[1]) !== 1)
1130
- return false;
1131
- const after = this.#line[this.#nextNonspace + match[0].length];
1132
- if (after !== void 0 && after !== " " && after !== " ")
1133
- return false;
1134
- if (container.kind === "Paragraph" && !/[^ \t]/.test(
1135
- this.#line.slice(this.#nextNonspace + match[0].length)
1136
- ))
1137
- return false;
1138
- const markerOffset = this.#indent;
1139
- const start = this.#nextNonspace;
1140
- this.#advanceNextNonspace();
1141
- this.#advance(match[0].length, true);
1142
- const spacesColumn = this.#column;
1143
- const spacesOffset = this.#offset;
1144
- let next;
1145
- do {
1146
- this.#advance(1, true);
1147
- next = this.#line[this.#offset];
1148
- } while (this.#column - spacesColumn < 5 && (next === " " || next === " "));
1149
- const blankItem = this.#line[this.#offset] === void 0;
1150
- const spaces = this.#column - spacesColumn;
1151
- const written = /^ */.exec(this.#line.slice(spacesOffset))?.[0].length ?? 0;
1152
- let padding;
1153
- if (spaces >= 5 || spaces < 1 || blankItem) {
1154
- padding = match[0].length + 1;
1155
- this.#column = spacesColumn;
1156
- this.#offset = spacesOffset;
1157
- this.#partialTab = false;
1158
- const char = this.#line[this.#offset];
1159
- if (char === " " || char === " ") this.#advance(1, true);
1160
- } else padding = match[0].length + spaces;
1161
- this.#closeUnmatched();
1162
- const delimiter = bullet ? match[0] : ordered?.[2];
1163
- if (this.#tip.kind !== "List" || this.#tip.ordered !== !!ordered || this.#tip.delimiter !== delimiter) {
1164
- const list = this.#add("List", start);
1165
- list.ordered = !!ordered;
1166
- list.delimiter = delimiter;
1655
+ #advanceNextNonspace() {
1656
+ this.#offset = this.#nextNonspace;
1657
+ this.#column = this.#nextNonspaceColumn;
1658
+ this.#partialTab = false;
1659
+ }
1660
+ #advance(count, columns = false) {
1661
+ const line = this.#line;
1662
+ let remaining = count;
1663
+ let char = line[this.#offset];
1664
+ while (remaining > 0 && char !== void 0) {
1665
+ if (char === " ") {
1666
+ const toTab = 4 - this.#column % 4;
1667
+ if (columns) {
1668
+ this.#partialTab = toTab > remaining;
1669
+ const advance = Math.min(toTab, remaining);
1670
+ this.#column += advance;
1671
+ this.#offset += this.#partialTab ? 0 : 1;
1672
+ remaining -= advance;
1673
+ } else {
1674
+ this.#partialTab = false;
1675
+ this.#column += toTab;
1676
+ this.#offset += 1;
1677
+ remaining -= 1;
1678
+ }
1679
+ } else {
1680
+ this.#partialTab = false;
1681
+ this.#offset += 1;
1682
+ this.#column += 1;
1683
+ remaining -= 1;
1684
+ }
1685
+ char = line[this.#offset];
1167
1686
  }
1168
- const item = this.#add("ListItem", start);
1169
- item.markerOffset = markerOffset;
1170
- item.padding = padding;
1171
- item.marker = match[0];
1172
- item.number = ordered ? Number(ordered[1]) : null;
1173
- item.spaces = blankItem ? 0 : spaces >= 5 || written < 1 ? 1 : Math.min(written, spaces);
1174
- return true;
1175
1687
  }
1176
- /** Splits a table row into its cells, on pipes that are not escaped. */
1177
- static cells(row) {
1178
- let text = row.trim();
1179
- if (text.startsWith("|")) text = text.slice(1);
1180
- if (text.endsWith("|") && !text.endsWith("\\|"))
1181
- text = text.slice(0, -1);
1182
- const cells = [];
1183
- let cell = "";
1184
- for (let index = 0; index < text.length; index++) {
1185
- const char = text[index];
1186
- if (char === "\\" && text[index + 1] === "|") {
1187
- cell += "\\|";
1188
- index++;
1189
- } else if (char === "|") {
1190
- cells.push(cell.trim());
1191
- cell = "";
1192
- } else cell += char;
1688
+ #rest() {
1689
+ return this.#line.slice(this.#nextNonspace);
1690
+ }
1691
+ #addLine() {
1692
+ this.#tip.prefixes.push(this.#line.slice(0, this.#offset));
1693
+ let text = this.#line.slice(this.#offset);
1694
+ if (this.#partialTab) {
1695
+ this.#offset += 1;
1696
+ const spaces = 4 - this.#column % 4;
1697
+ text = " ".repeat(spaces) + this.#line.slice(this.#offset);
1193
1698
  }
1194
- cells.push(cell.trim());
1195
- return cells;
1699
+ this.#tip.indents.push(
1700
+ this.#line.slice(
1701
+ this.#contentStart,
1702
+ Math.max(this.#offset, this.#contentStart)
1703
+ )
1704
+ );
1705
+ this.#tip.lines.push(text);
1706
+ this.#tip.offsets.push(this.#lineStart + this.#offset);
1707
+ this.#tip.endLine = this.#lineNumber;
1196
1708
  }
1197
- /** Link reference definitions at the start of a paragraph become blocks of their own. */
1198
- #takeDefinitions(paragraph) {
1199
- if (paragraph.lines.length === 0) return;
1200
- const text = paragraph.lines.map((line) => line.trimStart()).join("\n");
1201
- let position = 0;
1202
- for (; ; ) {
1203
- if (text[position] !== "[") break;
1204
- const definition = _Blocks.#definition(text, position);
1205
- if (!definition) break;
1206
- const [end, label, destination, title] = definition;
1207
- const first = _Blocks.#countLines(text, 0, position);
1208
- const last = _Blocks.#countLines(
1209
- text,
1210
- 0,
1211
- Math.max(end - 1, position)
1709
+ #add(kind, offset) {
1710
+ while (!_Blocks.#canContain(this.#tip.kind, kind))
1711
+ this.#close(this.#tip);
1712
+ const block = this.#create(
1713
+ kind,
1714
+ this.#tip,
1715
+ this.#lineStart + offset,
1716
+ this.#lineNumber
1717
+ );
1718
+ this.#tip.children.push(block);
1719
+ this.#tip = block;
1720
+ return block;
1721
+ }
1722
+ static #canContain(parent, child) {
1723
+ if (parent === "List") return child === "ListItem";
1724
+ return (parent === "Root" || parent === "BlockQuote" || parent === "ListItem") && child !== "ListItem";
1725
+ }
1726
+ #closeUnmatched() {
1727
+ if (this.#allClosed) return;
1728
+ while (this.#oldTip !== this.#lastMatched) {
1729
+ const parent = this.#oldTip.parent;
1730
+ this.#close(this.#oldTip);
1731
+ this.#oldTip = parent;
1732
+ }
1733
+ this.#allClosed = true;
1734
+ }
1735
+ #close(block) {
1736
+ const parent = block.parent;
1737
+ block.open = false;
1738
+ if (block.kind === "Paragraph") this.#takeDefinitions(block);
1739
+ if (block.kind === "CodeBlock" && !block.fence) {
1740
+ while (block.lines.length > 0 && /^[ \t]*$/.test(block.lines.at(-1) ?? "")) {
1741
+ block.lines.pop();
1742
+ block.offsets.pop();
1743
+ block.prefixes.pop();
1744
+ }
1745
+ block.endLine = block.line + Math.max(block.lines.length - 1, 0);
1746
+ }
1747
+ if (block.children.length > 0)
1748
+ block.endLine = Math.max(
1749
+ block.endLine,
1750
+ ...block.children.map((child) => child.endLine)
1212
1751
  );
1213
- const line = paragraph.lines[first] ?? "";
1214
- paragraph.definitions.push({
1215
- type: "Definition",
1216
- indent: `${paragraph.indents[first] ?? ""}${/^[ \t]*/.exec(line)?.[0] ?? ""}`,
1217
- start: paragraph.offsets[first] ?? paragraph.start,
1218
- end: (paragraph.offsets[last] ?? paragraph.start) + (paragraph.lines[last]?.length ?? 0),
1219
- line: paragraph.line + first,
1220
- endLine: paragraph.line + last,
1221
- label,
1222
- destination,
1223
- title
1224
- });
1225
- position = end;
1752
+ if (parent) this.#tip = parent;
1753
+ }
1754
+ /** Continues an open block on the current line: 0 matched, 1 not, 2 the line is consumed. */
1755
+ #continue(block) {
1756
+ const line = this.#line;
1757
+ switch (block.kind) {
1758
+ case "BlockQuote":
1759
+ if (!this.#indented && line[this.#nextNonspace] === ">") {
1760
+ this.#advanceNextNonspace();
1761
+ this.#advance(1);
1762
+ if (line[this.#offset] === " " || line[this.#offset] === " ")
1763
+ this.#advance(1, true);
1764
+ block.endLine = this.#lineNumber;
1765
+ return 0;
1766
+ }
1767
+ return 1;
1768
+ case "ListItem":
1769
+ if (this.#blank) {
1770
+ if (block.children.length === 0) return 1;
1771
+ this.#advanceNextNonspace();
1772
+ return 0;
1773
+ }
1774
+ if (this.#indent >= block.markerOffset + block.padding) {
1775
+ this.#advance(block.markerOffset + block.padding, true);
1776
+ return 0;
1777
+ }
1778
+ return 1;
1779
+ case "CodeBlock": {
1780
+ if (block.fence) {
1781
+ const match = this.#indent <= 3 && line[this.#nextNonspace] === block.fence[0] ? /^(?:`{3,}|~{3,})(?=[ \t]*$)/.exec(this.#rest()) : null;
1782
+ if (match && match[0].length >= block.fence.length) {
1783
+ block.closed = true;
1784
+ block.endLine = this.#lineNumber;
1785
+ this.#close(block);
1786
+ return 2;
1787
+ }
1788
+ let skip = block.fenceOffset;
1789
+ while (skip > 0 && (line[this.#offset] === " " || line[this.#offset] === " ")) {
1790
+ this.#advance(1, true);
1791
+ skip--;
1792
+ }
1793
+ return 0;
1794
+ }
1795
+ if (this.#indent >= 4) {
1796
+ this.#advance(4, true);
1797
+ return 0;
1798
+ }
1799
+ if (this.#blank) {
1800
+ this.#advanceNextNonspace();
1801
+ return 0;
1802
+ }
1803
+ return 1;
1804
+ }
1805
+ case "HtmlBlock":
1806
+ return this.#blank && (block.htmlKind === 6 || block.htmlKind === 7) ? 1 : 0;
1807
+ case "Paragraph":
1808
+ case "Table":
1809
+ return this.#blank ? 1 : 0;
1810
+ case "Heading":
1811
+ case "ThematicBreak":
1812
+ return 1;
1813
+ default:
1814
+ return 0;
1226
1815
  }
1227
- if (position === 0) return;
1228
- const consumed = position >= text.length ? paragraph.lines.length : _Blocks.#countLines(text, 0, position);
1229
- paragraph.indents.splice(0, consumed);
1230
- paragraph.prefixes.splice(0, consumed);
1231
- paragraph.lines.splice(0, consumed);
1232
- paragraph.offsets.splice(0, consumed);
1233
- paragraph.line += consumed;
1234
- paragraph.start = paragraph.offsets[0] ?? paragraph.start;
1235
- }
1236
- static #countLines(text, from, to) {
1237
- let count = 0;
1238
- for (let index = from; index < to; index++)
1239
- if (text[index] === "\n") count++;
1240
- return count;
1241
1816
  }
1242
- /** Parses a definition at a position, to the position after its line ending. */
1243
- static #definition(text, from) {
1244
- const label = _Blocks.#linkLabel(text, from);
1245
- if (label === void 0 || text[label] !== ":") return;
1246
- const raw = text.slice(from, label);
1247
- if (!/[^ \t\r\n[\]]/.test(raw.slice(1, -1))) return;
1248
- let position = _Blocks.#spaces(text, label + 1, true);
1249
- const destination = Scan.destination(text, position);
1250
- if (!destination) return;
1251
- const [destinationEnd, destinationText] = destination;
1252
- if (destinationText === "" && text[position] !== "<") return;
1253
- position = destinationEnd;
1254
- const beforeTitle = position;
1255
- const afterSpaces = _Blocks.#spaces(text, position, true);
1256
- let title = null;
1257
- let titleEnd = -1;
1258
- if (afterSpaces !== position) {
1259
- const parsed = Scan.title(text, afterSpaces);
1260
- if (parsed !== void 0) {
1261
- titleEnd = parsed;
1262
- title = text.slice(afterSpaces, parsed);
1817
+ #incorporate(text) {
1818
+ let container = this.#root;
1819
+ this.#oldTip = this.#tip;
1820
+ this.#offset = 0;
1821
+ this.#column = 0;
1822
+ this.#blank = false;
1823
+ this.#partialTab = false;
1824
+ this.#consumed = false;
1825
+ this.#lineNumber++;
1826
+ this.#line = text.includes("\0") ? text.replaceAll("\0", "\uFFFD") : text;
1827
+ for (; ; ) {
1828
+ const last = container.children.at(-1);
1829
+ if (!last?.open) break;
1830
+ container = last;
1831
+ this.#findNextNonspace();
1832
+ const result = this.#continue(container);
1833
+ if (result === 2) return;
1834
+ if (result === 1) {
1835
+ container = container.parent;
1836
+ break;
1263
1837
  }
1264
1838
  }
1265
- const lineEnd = /* @__PURE__ */ __name((at) => {
1266
- let index = at;
1267
- while (text[index] === " " || text[index] === " ") index++;
1268
- if (index >= text.length) return index;
1269
- if (text[index] === "\n") return index + 1;
1270
- }, "lineEnd");
1271
- if (titleEnd >= 0) {
1272
- const end2 = lineEnd(titleEnd);
1273
- if (end2 !== void 0) return [end2, raw, destinationText, title];
1839
+ this.#allClosed = container === this.#oldTip;
1840
+ this.#lastMatched = container;
1841
+ this.#contentStart = this.#offset;
1842
+ let matchedLeaf = container.kind !== "Paragraph" && container.kind !== "Table" && (container.kind === "CodeBlock" || container.kind === "HtmlBlock");
1843
+ while (!matchedLeaf) {
1844
+ this.#findNextNonspace();
1845
+ if (!this.#indented && !/^[#`~*+_=<>0-9-|:]/.test(this.#rest())) {
1846
+ this.#advanceNextNonspace();
1847
+ break;
1848
+ }
1849
+ const result = this.#start(container);
1850
+ if (result === 0) {
1851
+ this.#advanceNextNonspace();
1852
+ break;
1853
+ }
1854
+ container = this.#tip;
1855
+ if (result === 1) this.#contentStart = this.#offset;
1856
+ if (result === 2) matchedLeaf = true;
1274
1857
  }
1275
- const end = lineEnd(beforeTitle);
1276
- if (end === void 0) return;
1277
- return [end, raw, destinationText, null];
1278
- }
1279
- /** The position after a link label `[...]`, if one starts at a position. */
1280
- static #linkLabel(text, from) {
1281
- if (text[from] !== "[") return;
1282
- for (let index = from + 1; index < text.length && index - from <= 1e3; index++) {
1283
- const char = text[index];
1284
- if (char === "\\") index++;
1285
- else if (char === "[") return;
1286
- else if (char === "]") return index + 1;
1858
+ if (!this.#allClosed && !this.#blank && this.#tip.kind === "Paragraph") {
1859
+ this.#tip.lazy.add(this.#lineNumber);
1860
+ this.#addLine();
1861
+ for (let block = this.#tip; block; block = block.parent)
1862
+ if (block.kind === "BlockQuote" || block.kind === "ListItem")
1863
+ block.endLine = this.#lineNumber;
1864
+ return;
1287
1865
  }
1288
- }
1289
- static #spaces(text, from, newline) {
1290
- let index = from;
1291
- let newlines = 0;
1292
- while (index < text.length) {
1293
- const char = text[index];
1294
- if (char === "\n" && newline && newlines === 0) newlines++;
1295
- else if (char !== " " && char !== " ") break;
1296
- index++;
1866
+ this.#closeUnmatched();
1867
+ const kind = container.kind;
1868
+ if (this.#consumed) return;
1869
+ if (kind === "CodeBlock" || kind === "HtmlBlock") {
1870
+ this.#addLine();
1871
+ if (kind === "HtmlBlock" && container.htmlKind >= 1 && container.htmlKind <= 5 && htmlClose[container.htmlKind]?.test(
1872
+ this.#line.slice(this.#offset)
1873
+ ))
1874
+ this.#close(container);
1875
+ } else if (kind === "Table") {
1876
+ if (!this.#blank) this.#addLine();
1877
+ } else if (kind === "Paragraph") {
1878
+ if (!this.#blank) this.#addLine();
1879
+ } else if (this.#offset < this.#line.length && !this.#blank) {
1880
+ this.#add("Paragraph", this.#nextNonspace);
1881
+ this.#advanceNextNonspace();
1882
+ this.#addLine();
1297
1883
  }
1298
- return index;
1299
1884
  }
1300
- // finishing
1301
- #blocks(parent, definitions) {
1302
- const blocks = [];
1303
- for (const child of parent.children) {
1304
- for (const definition of child.definitions) {
1305
- blocks.push(definition);
1306
- const key = Scan.label(definition.label);
1307
- if (!definitions.has(key)) definitions.set(key, definition);
1308
- }
1309
- const block = this.#block(child, definitions);
1310
- if (block) blocks.push(block);
1885
+ /** Tries to start a block: 0 none, 1 a container, 2 a leaf that takes the rest of the line. */
1886
+ #start(container) {
1887
+ const line = this.#line;
1888
+ const rest = this.#rest();
1889
+ const char = line[this.#nextNonspace];
1890
+ if (!this.#indented && char === ">") {
1891
+ this.#advanceNextNonspace();
1892
+ this.#advance(1);
1893
+ if (line[this.#offset] === " " || line[this.#offset] === " ")
1894
+ this.#advance(1, true);
1895
+ this.#closeUnmatched();
1896
+ this.#add("BlockQuote", this.#nextNonspace);
1897
+ return 1;
1311
1898
  }
1312
- return blocks;
1313
- }
1314
- #end(block) {
1315
- const line = this.#lines[block.endLine - 1];
1316
- return line ? line.start + line.text.length : this.#source.length;
1317
- }
1318
- #block(open, definitions) {
1319
- const base = {
1320
- start: open.start,
1321
- end: Math.max(this.#end(open), open.start),
1322
- line: open.line,
1323
- endLine: open.endLine
1324
- };
1325
- switch (open.kind) {
1326
- case "Paragraph": {
1327
- if (open.lines.length === 0) return;
1328
- const text = open.lines.map((line) => line.trimStart()).join("\n");
1329
- return {
1330
- type: "Paragraph",
1331
- ...base,
1332
- text: text.replace(/[ \t]+$/, ""),
1333
- indents: open.lines.map(
1334
- (line, index) => `${open.indents[index] ?? ""}${/^[ \t]*/.exec(line)?.[0] ?? ""}`
1335
- ),
1336
- prefixes: open.prefixes,
1337
- lazy: open.lines.map(
1338
- (_, index) => open.lazy.has(open.line + index)
1339
- ),
1340
- children: []
1341
- };
1342
- }
1343
- case "Heading": {
1344
- const text = open.setext ? open.lines.map((line) => line.trim()).join("\n") : (open.lines[0] ?? "").trimStart();
1345
- return {
1346
- type: "Heading",
1347
- ...base,
1348
- level: open.level,
1349
- setext: open.setext,
1350
- text,
1351
- underline: open.setext ? open.info : "",
1352
- children: []
1353
- };
1354
- }
1355
- case "ThematicBreak":
1356
- return {
1357
- type: "ThematicBreak",
1358
- ...base,
1359
- raw: open.lines[0] ?? ""
1360
- };
1361
- case "CodeBlock":
1362
- return {
1363
- type: "CodeBlock",
1364
- ...base,
1365
- fence: open.fence,
1366
- indent: open.fenceOffset,
1367
- info: open.info,
1368
- lines: open.lines,
1369
- prefixes: open.prefixes,
1370
- closed: open.closed
1371
- };
1372
- case "HtmlBlock":
1373
- return {
1374
- type: "HtmlBlock",
1375
- ...base,
1376
- kind: open.htmlKind,
1377
- lines: open.lines
1378
- };
1379
- case "BlockQuote":
1380
- return {
1381
- type: "BlockQuote",
1382
- ...base,
1383
- children: this.#blocks(open, definitions)
1384
- };
1385
- case "List": {
1386
- const items = open.children.map(
1387
- (item) => this.#block(item, definitions)
1388
- );
1389
- return {
1390
- type: "List",
1391
- ...base,
1392
- ordered: open.ordered,
1393
- delimiter: open.delimiter,
1394
- tight: _Blocks.#tight(items),
1395
- children: items
1396
- };
1899
+ if (!this.#indented) {
1900
+ const atx = /^#{1,6}(?:[ \t]+|$)/.exec(rest);
1901
+ if (atx) {
1902
+ const start = this.#nextNonspace;
1903
+ this.#advanceNextNonspace();
1904
+ this.#advance(atx[0].length);
1905
+ this.#closeUnmatched();
1906
+ const heading = this.#add("Heading", start);
1907
+ heading.level = atx[0].trim().length;
1908
+ heading.lines.push(
1909
+ line.slice(this.#offset).replace(/^[ \t]*#+[ \t]*$/, "").replace(/[ \t]+#+[ \t]*$/, "")
1910
+ );
1911
+ heading.offsets.push(this.#lineStart + this.#offset);
1912
+ this.#advance(line.length - this.#offset);
1913
+ return 2;
1397
1914
  }
1398
- case "ListItem": {
1399
- const children = this.#blocks(open, definitions);
1400
- let checked = null;
1401
- const first = children[0];
1402
- const task = this.#gfm && first?.type === "Paragraph" ? /^\[([ xX])\](?=[ \t]|$)/.exec(first.text) : null;
1403
- if (task && first?.type === "Paragraph" && first.line === open.line) {
1404
- checked = task[1] !== " ";
1915
+ const fence = /^`{3,}(?!.*`)|^~{3,}/.exec(rest);
1916
+ if (fence) {
1917
+ const start = this.#nextNonspace;
1918
+ this.#closeUnmatched();
1919
+ const code = this.#add("CodeBlock", start);
1920
+ code.fence = fence[0];
1921
+ code.fenceOffset = this.#indent;
1922
+ code.info = rest.slice(fence[0].length).trim();
1923
+ this.#advanceNextNonspace();
1924
+ this.#advance(line.length - this.#offset);
1925
+ this.#consumed = true;
1926
+ return 2;
1927
+ }
1928
+ if (char === "<") {
1929
+ for (let kind = 1; kind <= 7; kind++) {
1930
+ if (htmlOpen[kind]?.test(rest) && (kind < 7 || container.kind !== "Paragraph" && !(!this.#allClosed && !this.#blank && this.#tip.kind === "Paragraph"))) {
1931
+ this.#closeUnmatched();
1932
+ const html = this.#add("HtmlBlock", this.#offset);
1933
+ html.htmlKind = kind;
1934
+ return 2;
1935
+ }
1405
1936
  }
1406
- return {
1407
- type: "ListItem",
1408
- ...base,
1409
- marker: open.marker,
1410
- number: open.number,
1411
- indent: open.markerOffset,
1412
- spaces: open.spaces,
1413
- checked,
1414
- children
1415
- };
1416
1937
  }
1417
- case "Table": {
1418
- const rows = open.lines.map((line, index) => ({ line, index })).filter(({ index }) => index !== 1).map(({ line, index }) => {
1419
- const start = open.offsets[index] ?? open.start;
1420
- return {
1421
- type: "TableRow",
1422
- start,
1423
- end: start + line.length,
1424
- line: open.line + index,
1425
- endLine: open.line + index,
1426
- cells: _Blocks.cells(line).map((text) => ({
1427
- type: "TableCell",
1428
- start,
1429
- end: start + line.length,
1430
- text,
1431
- children: []
1432
- }))
1433
- };
1434
- });
1435
- return {
1436
- type: "Table",
1437
- ...base,
1438
- alignments: open.alignments,
1439
- rows
1440
- };
1938
+ if (this.#gfm && container.kind === "Paragraph" && this.#allClosed && delimiterRow.test(rest) && rest.includes("-")) {
1939
+ const header = container.lines.at(-1) ?? "";
1940
+ const delimiters = _Blocks.cells(rest);
1941
+ if ((rest.includes("|") || /(?:^|[^\\])\|/.test(header)) && _Blocks.cells(header).length === delimiters.length) {
1942
+ this.#closeUnmatched();
1943
+ this.#table(container, delimiters);
1944
+ return 2;
1945
+ }
1441
1946
  }
1442
- }
1443
- }
1444
- /** Lists are loose when blank lines separate items, or blocks of an item. */
1445
- static #tight(items) {
1446
- for (const [index, item] of items.entries()) {
1447
- const next = items[index + 1];
1448
- if (next && next.line > item.endLine + 1) return false;
1449
- for (const [position, child] of item.children.entries()) {
1450
- const sibling = item.children[position + 1];
1451
- if (sibling && sibling.line > child.endLine + 1) return false;
1947
+ if (container.kind === "Paragraph" && /^(?:=+|-+)[ \t]*$/.test(rest)) {
1948
+ this.#closeUnmatched();
1949
+ this.#takeDefinitions(container);
1950
+ if (container.lines.length > 0) {
1951
+ container.info = rest.trim();
1952
+ container.kind = "Heading";
1953
+ container.setext = true;
1954
+ container.level = char === "=" ? 1 : 2;
1955
+ container.endLine = this.#lineNumber;
1956
+ this.#advance(line.length - this.#offset);
1957
+ return 2;
1958
+ }
1452
1959
  }
1453
- }
1454
- return true;
1455
- }
1456
- static #inlines(blocks, inlines) {
1457
- for (const block of blocks) {
1458
- switch (block.type) {
1459
- case "Paragraph":
1460
- case "Heading":
1461
- block.children = inlines.parse(block.text);
1462
- break;
1463
- case "Table":
1464
- for (const row of block.rows)
1465
- for (const cell of row.cells)
1466
- cell.children = inlines.parse(cell.text);
1467
- break;
1468
- case "BlockQuote":
1469
- _Blocks.#inlines(block.children, inlines);
1470
- break;
1471
- case "List":
1472
- for (const item of block.children)
1473
- _Blocks.#inlines(item.children, inlines);
1474
- break;
1960
+ if (thematicBreak.test(rest)) {
1961
+ const start = this.#nextNonspace;
1962
+ this.#closeUnmatched();
1963
+ const block = this.#add("ThematicBreak", start);
1964
+ block.lines.push(rest.trimEnd());
1965
+ this.#advance(line.length - this.#offset);
1966
+ return 2;
1475
1967
  }
1476
1968
  }
1477
- }
1478
- };
1479
-
1480
- // shared/syntax/src/scanner.ts
1481
- var ParseError = class extends SyntaxError {
1482
- static {
1483
- __name(this, "ParseError");
1484
- }
1485
- position;
1486
- constructor(message, position) {
1487
- super(message);
1488
- this.position = position;
1489
- }
1490
- };
1491
- var Scanner = class _Scanner {
1492
- static {
1493
- __name(this, "Scanner");
1494
- }
1495
- static #punctuators = [
1496
- ">>>=",
1497
- "...",
1498
- "===",
1499
- "!==",
1500
- "**=",
1501
- "&&=",
1502
- "||=",
1503
- "??=",
1504
- "=>",
1505
- "==",
1506
- "!=",
1507
- "<=",
1508
- "&&",
1509
- "||",
1510
- "??",
1511
- "?.",
1512
- "++",
1513
- "--",
1514
- "+=",
1515
- "-=",
1516
- "*=",
1517
- "/=",
1518
- "%=",
1519
- "&=",
1520
- "|=",
1521
- "^=",
1522
- "**"
1523
- ];
1524
- static #greater = [">>>=", ">>>", ">>=", ">>", ">=", ">"];
1525
- static #less = ["<<=", "<<", "<=", "<"];
1526
- source;
1527
- comments = [];
1528
- #position = 0;
1529
- constructor(source) {
1530
- this.source = source;
1531
- }
1532
- get position() {
1533
- return this.#position;
1534
- }
1535
- /** Moves back (or forward) and forgets comments scanned beyond. */
1536
- reset(position) {
1537
- this.#position = position;
1538
- while ((this.comments.at(-1)?.start ?? -1) >= position)
1539
- this.comments.pop();
1540
- }
1541
- next() {
1542
- const newline = this.#trivia();
1543
- const start = this.#position;
1544
- const char = this.source[start];
1545
- if (char === void 0) return this.#token("eof", start, newline);
1546
- if (_Scanner.#identifierStart(char) || char === "\\")
1547
- return this.#word("name", start, newline);
1548
- if (char === "#") return this.#word("private", start, newline);
1549
- if (/[0-9]/.test(char) || char === "." && /[0-9]/.test(this.source[start + 1] ?? ""))
1550
- return this.#number(start, newline);
1551
- if (char === '"' || char === "'") return this.#string(start, newline);
1552
- if (char === "`") return this.template(start, newline);
1553
- const punctuator = _Scanner.#punctuators.find(
1554
- (value) => this.source.startsWith(value, start) && !(value === "?." && /[0-9]/.test(this.source[start + 2] ?? ""))
1555
- ) ?? char;
1556
- this.#position = start + punctuator.length;
1557
- return this.#token("punct", start, newline);
1558
- }
1559
- /** Rescans a `/` or `/=` token as a regular expression literal. */
1560
- regex(token) {
1561
- let inClass = false;
1562
- for (let index = token.start + 1; ; index++) {
1563
- const char = this.source[index];
1564
- if (char === void 0 || char === "\n" || char === "\r")
1565
- return this.#invalid(token.start, "Unterminated regex");
1566
- if (char === "\\") index++;
1567
- else if (char === "[") inClass = true;
1568
- else if (char === "]") inClass = false;
1569
- else if (char === "/" && !inClass) {
1570
- this.#position = index + 1;
1571
- while (_Scanner.#identifierPart(this.source[this.#position] ?? ""))
1572
- this.#position++;
1573
- return this.#token("regex", token.start, token.newline);
1574
- }
1969
+ if (!this.#indented || container.kind === "List") {
1970
+ const item = this.#listMarker(container);
1971
+ if (item) return 1;
1575
1972
  }
1576
- }
1577
- /** Rescans `>` as the longest of `>>>=`, `>>>`, `>>=`, `>>`, `>=`. */
1578
- greater(token) {
1579
- return this.#operator(token, _Scanner.#greater);
1580
- }
1581
- /** Rescans `<` as the longest of `<<=`, `<<`, `<=`, since types nest `<<T>() => T>`. */
1582
- less(token) {
1583
- return this.#operator(token, _Scanner.#less);
1584
- }
1585
- #operator(token, candidates) {
1586
- const value = candidates.find(
1587
- (value2) => this.source.startsWith(value2, token.start)
1588
- ) ?? token.value;
1589
- this.reset(token.start + value.length);
1590
- return { ...token, value, end: token.start + value.length };
1591
- }
1592
- /** Scans a template part, from a backtick or a closing `}`, up to `${` or a backtick. */
1593
- template(start, newline = false) {
1594
- for (let index = start + 1; index < this.source.length; index++) {
1595
- const char = this.source[index];
1596
- if (char === "\\") index++;
1597
- else if (char === "`" || char === "$" && this.source[index + 1] === "{") {
1598
- this.#position = index + (char === "`" ? 1 : 2);
1599
- return this.#token("template", start, newline);
1600
- }
1973
+ if (this.#indented && this.#tip.kind !== "Paragraph" && !this.#blank) {
1974
+ this.#advance(4, true);
1975
+ this.#closeUnmatched();
1976
+ this.#add("CodeBlock", this.#offset);
1977
+ return 2;
1601
1978
  }
1602
- return this.#invalid(start, "Unterminated template");
1603
- }
1604
- /** Scans JSX text up to `{` or `<`. */
1605
- jsxText(start) {
1606
- let index = start;
1607
- while (index < this.source.length && !"{<".includes(this.source[index] ?? ""))
1608
- index++;
1609
- this.#position = index;
1610
- return this.#token("jsxText", start, false);
1611
- }
1612
- /** Rescans a name token to include JSX's `-` characters. */
1613
- jsxName(token) {
1614
- let index = token.end;
1615
- while (/[-\p{ID_Continue}$]/u.test(this.source[index] ?? "")) index++;
1616
- this.reset(index);
1617
- return {
1618
- ...token,
1619
- value: this.source.slice(token.start, index),
1620
- end: index
1621
- };
1979
+ return 0;
1622
1980
  }
1623
- /** Rescans a quoted JSX attribute value, which has no escapes. */
1624
- jsxString(token) {
1625
- const quote = this.source[token.start] ?? "";
1626
- const end = this.source.indexOf(quote, token.start + 1);
1627
- if (end < 0) return this.#invalid(token.start, "Unterminated string");
1628
- this.reset(end + 1);
1629
- return {
1630
- ...token,
1631
- type: "string",
1632
- value: this.source.slice(token.start, end + 1),
1633
- end: end + 1
1634
- };
1981
+ /** Turns the last line of a paragraph and the delimiter row into a table. */
1982
+ #table(paragraph, delimiters) {
1983
+ const headerLine = paragraph.lines.pop();
1984
+ const headerOffset = paragraph.offsets.pop();
1985
+ paragraph.indents.pop();
1986
+ paragraph.prefixes.pop();
1987
+ const parent = paragraph.parent;
1988
+ const table = this.#create(
1989
+ "Table",
1990
+ parent,
1991
+ headerOffset,
1992
+ paragraph.line + paragraph.lines.length
1993
+ );
1994
+ table.alignments = delimiters.map((cell) => {
1995
+ const left = cell.startsWith(":");
1996
+ const right = cell.endsWith(":");
1997
+ return left && right ? "center" : left ? "left" : right ? "right" : null;
1998
+ });
1999
+ table.lines.push(headerLine, this.#line.slice(this.#nextNonspace));
2000
+ table.offsets.push(headerOffset, this.#lineStart + this.#nextNonspace);
2001
+ table.endLine = this.#lineNumber;
2002
+ if (paragraph.lines.length === 0) {
2003
+ parent.children.splice(
2004
+ parent.children.indexOf(paragraph),
2005
+ 1,
2006
+ table
2007
+ );
2008
+ } else {
2009
+ paragraph.endLine = table.line - 1;
2010
+ paragraph.open = false;
2011
+ this.#takeDefinitions(paragraph);
2012
+ parent.children.push(table);
2013
+ }
2014
+ this.#tip = table;
2015
+ this.#advance(this.#line.length - this.#offset);
2016
+ this.#consumed = true;
1635
2017
  }
1636
- #token(type, start, newline) {
1637
- return {
1638
- type,
1639
- value: this.source.slice(start, this.#position),
1640
- start,
1641
- end: this.#position,
1642
- newline
1643
- };
2018
+ #listMarker(container) {
2019
+ if (this.#indent >= 4) return false;
2020
+ const rest = this.#rest();
2021
+ const bullet = /^[*+-]/.exec(rest);
2022
+ const ordered = bullet ? null : /^(\d{1,9})([.)])/.exec(rest);
2023
+ const match = bullet ?? ordered;
2024
+ if (!match) return false;
2025
+ if (ordered && container.kind === "Paragraph" && Number(ordered[1]) !== 1)
2026
+ return false;
2027
+ const after = this.#line[this.#nextNonspace + match[0].length];
2028
+ if (after !== void 0 && after !== " " && after !== " ")
2029
+ return false;
2030
+ if (container.kind === "Paragraph" && !/[^ \t]/.test(
2031
+ this.#line.slice(this.#nextNonspace + match[0].length)
2032
+ ))
2033
+ return false;
2034
+ const markerOffset = this.#indent;
2035
+ const start = this.#nextNonspace;
2036
+ this.#advanceNextNonspace();
2037
+ this.#advance(match[0].length, true);
2038
+ const spacesColumn = this.#column;
2039
+ const spacesOffset = this.#offset;
2040
+ let next;
2041
+ do {
2042
+ this.#advance(1, true);
2043
+ next = this.#line[this.#offset];
2044
+ } while (this.#column - spacesColumn < 5 && (next === " " || next === " "));
2045
+ const blankItem = this.#line[this.#offset] === void 0;
2046
+ const spaces = this.#column - spacesColumn;
2047
+ const written = /^ */.exec(this.#line.slice(spacesOffset))?.[0].length ?? 0;
2048
+ let padding;
2049
+ if (spaces >= 5 || spaces < 1 || blankItem) {
2050
+ padding = match[0].length + 1;
2051
+ this.#column = spacesColumn;
2052
+ this.#offset = spacesOffset;
2053
+ this.#partialTab = false;
2054
+ const char = this.#line[this.#offset];
2055
+ if (char === " " || char === " ") this.#advance(1, true);
2056
+ } else padding = match[0].length + spaces;
2057
+ this.#closeUnmatched();
2058
+ const delimiter = bullet ? match[0] : ordered?.[2];
2059
+ if (this.#tip.kind !== "List" || this.#tip.ordered !== !!ordered || this.#tip.delimiter !== delimiter) {
2060
+ const list = this.#add("List", start);
2061
+ list.ordered = !!ordered;
2062
+ list.delimiter = delimiter;
2063
+ }
2064
+ const item = this.#add("ListItem", start);
2065
+ item.markerOffset = markerOffset;
2066
+ item.padding = padding;
2067
+ item.marker = match[0];
2068
+ item.number = ordered ? Number(ordered[1]) : null;
2069
+ item.spaces = blankItem ? 0 : spaces >= 5 || written < 1 ? 1 : Math.min(written, spaces);
2070
+ return true;
1644
2071
  }
1645
- #invalid(start, message) {
1646
- this.#position = this.source.length;
1647
- return {
1648
- type: "invalid",
1649
- value: message,
1650
- start,
1651
- end: start,
1652
- newline: false
1653
- };
2072
+ /** Splits a table row into its cells, on pipes that are not escaped. */
2073
+ static cells(row) {
2074
+ let text = row.trim();
2075
+ if (text.startsWith("|")) text = text.slice(1);
2076
+ if (text.endsWith("|") && !text.endsWith("\\|"))
2077
+ text = text.slice(0, -1);
2078
+ const cells = [];
2079
+ let cell = "";
2080
+ for (let index = 0; index < text.length; index++) {
2081
+ const char = text[index];
2082
+ if (char === "\\" && text[index + 1] === "|") {
2083
+ cell += "\\|";
2084
+ index++;
2085
+ } else if (char === "|") {
2086
+ cells.push(cell.trim());
2087
+ cell = "";
2088
+ } else cell += char;
2089
+ }
2090
+ cells.push(cell.trim());
2091
+ return cells;
1654
2092
  }
1655
- /** Skips whitespace and comments, returns whether a line break was crossed. */
1656
- #trivia() {
1657
- let newline = false;
1658
- if (this.#position === 0 && this.source.startsWith("#!")) {
1659
- const end = this.#lineEnd(0);
1660
- this.comments.push({
1661
- type: "Line",
1662
- value: this.source.slice(0, end),
1663
- start: 0,
1664
- end
2093
+ /** Link reference definitions at the start of a paragraph become blocks of their own. */
2094
+ #takeDefinitions(paragraph) {
2095
+ if (paragraph.lines.length === 0) return;
2096
+ const text = paragraph.lines.map((line) => line.trimStart()).join("\n");
2097
+ let position = 0;
2098
+ for (; ; ) {
2099
+ if (text[position] !== "[") break;
2100
+ const definition = _Blocks.#definition(text, position);
2101
+ if (!definition) break;
2102
+ const [end, label, destination, title] = definition;
2103
+ const first = _Blocks.#countLines(text, 0, position);
2104
+ const last = _Blocks.#countLines(
2105
+ text,
2106
+ 0,
2107
+ Math.max(end - 1, position)
2108
+ );
2109
+ const line = paragraph.lines[first] ?? "";
2110
+ paragraph.definitions.push({
2111
+ type: "Definition",
2112
+ indent: `${paragraph.indents[first] ?? ""}${/^[ \t]*/.exec(line)?.[0] ?? ""}`,
2113
+ start: paragraph.offsets[first] ?? paragraph.start,
2114
+ end: (paragraph.offsets[last] ?? paragraph.start) + (paragraph.lines[last]?.length ?? 0),
2115
+ line: paragraph.line + first,
2116
+ endLine: paragraph.line + last,
2117
+ label,
2118
+ destination,
2119
+ title
1665
2120
  });
1666
- this.#position = end;
2121
+ position = end;
1667
2122
  }
1668
- while (this.#position < this.source.length) {
1669
- const char = this.source[this.#position] ?? "";
1670
- const next = this.source[this.#position + 1];
1671
- if (char === "\n" || char === "\r" || char === "\u2028" || char === "\u2029") {
1672
- newline = true;
1673
- this.#position++;
1674
- } else if (/\s/.test(char)) this.#position++;
1675
- else if (char === "/" && next === "/") {
1676
- const end = this.#lineEnd(this.#position);
1677
- this.#comment("Line", end);
1678
- } else if (char === "/" && next === "*") {
1679
- const close = this.source.indexOf("*/", this.#position + 2);
1680
- const end = close < 0 ? this.source.length : close + 2;
1681
- if (/[\r\n\u2028\u2029]/.test(
1682
- this.source.slice(this.#position, end)
1683
- ))
1684
- newline = true;
1685
- this.#comment("Block", end);
1686
- } else break;
2123
+ if (position === 0) return;
2124
+ const consumed = position >= text.length ? paragraph.lines.length : _Blocks.#countLines(text, 0, position);
2125
+ paragraph.indents.splice(0, consumed);
2126
+ paragraph.prefixes.splice(0, consumed);
2127
+ paragraph.lines.splice(0, consumed);
2128
+ paragraph.offsets.splice(0, consumed);
2129
+ paragraph.line += consumed;
2130
+ paragraph.start = paragraph.offsets[0] ?? paragraph.start;
2131
+ }
2132
+ static #countLines(text, from, to) {
2133
+ let count = 0;
2134
+ for (let index = from; index < to; index++)
2135
+ if (text[index] === "\n") count++;
2136
+ return count;
2137
+ }
2138
+ /** Parses a definition at a position, to the position after its line ending. */
2139
+ static #definition(text, from) {
2140
+ const label = _Blocks.#linkLabel(text, from);
2141
+ if (label === void 0 || text[label] !== ":") return;
2142
+ const raw = text.slice(from, label);
2143
+ if (!/[^ \t\r\n[\]]/.test(raw.slice(1, -1))) return;
2144
+ let position = _Blocks.#spaces(text, label + 1, true);
2145
+ const destination = Scan.destination(text, position);
2146
+ if (!destination) return;
2147
+ const [destinationEnd, destinationText] = destination;
2148
+ if (destinationText === "" && text[position] !== "<") return;
2149
+ position = destinationEnd;
2150
+ const beforeTitle = position;
2151
+ const afterSpaces = _Blocks.#spaces(text, position, true);
2152
+ let title = null;
2153
+ let titleEnd = -1;
2154
+ if (afterSpaces !== position) {
2155
+ const parsed = Scan.title(text, afterSpaces);
2156
+ if (parsed !== void 0) {
2157
+ titleEnd = parsed;
2158
+ title = text.slice(afterSpaces, parsed);
2159
+ }
1687
2160
  }
1688
- return newline;
2161
+ const lineEnd = /* @__PURE__ */ __name((at) => {
2162
+ let index = at;
2163
+ while (text[index] === " " || text[index] === " ") index++;
2164
+ if (index >= text.length) return index;
2165
+ if (text[index] === "\n") return index + 1;
2166
+ }, "lineEnd");
2167
+ if (titleEnd >= 0) {
2168
+ const end2 = lineEnd(titleEnd);
2169
+ if (end2 !== void 0) return [end2, raw, destinationText, title];
2170
+ }
2171
+ const end = lineEnd(beforeTitle);
2172
+ if (end === void 0) return;
2173
+ return [end, raw, destinationText, null];
1689
2174
  }
1690
- #comment(type, end) {
1691
- const start = this.#position;
1692
- this.comments.push({
1693
- type,
1694
- value: this.source.slice(start, end),
1695
- start,
1696
- end
1697
- });
1698
- this.#position = end;
2175
+ /** The position after a link label `[...]`, if one starts at a position. */
2176
+ static #linkLabel(text, from) {
2177
+ if (text[from] !== "[") return;
2178
+ for (let index = from + 1; index < text.length && index - from <= 1e3; index++) {
2179
+ const char = text[index];
2180
+ if (char === "\\") index++;
2181
+ else if (char === "[") return;
2182
+ else if (char === "]") return index + 1;
2183
+ }
1699
2184
  }
1700
- #lineEnd(start) {
1701
- const match = /[\r\n\u2028\u2029]/.exec(this.source.slice(start));
1702
- return match ? start + match.index : this.source.length;
2185
+ static #spaces(text, from, newline) {
2186
+ let index = from;
2187
+ let newlines = 0;
2188
+ while (index < text.length) {
2189
+ const char = text[index];
2190
+ if (char === "\n" && newline && newlines === 0) newlines++;
2191
+ else if (char !== " " && char !== " ") break;
2192
+ index++;
2193
+ }
2194
+ return index;
1703
2195
  }
1704
- #word(type, start, newline) {
1705
- let index = start + 1;
1706
- while (index < this.source.length) {
1707
- const char = this.source[index] ?? "";
1708
- if (char === "\\")
1709
- index += /^\\u\{/.test(this.source.slice(index, index + 3)) ? this.source.indexOf("}", index) - index + 1 : 6;
1710
- else if (_Scanner.#identifierPart(char)) index += char.length;
1711
- else {
1712
- const code = this.source.codePointAt(index) ?? 0;
1713
- const symbol = String.fromCodePoint(code);
1714
- if (code > 65535 && _Scanner.#identifierPart(symbol))
1715
- index += 2;
1716
- else break;
2196
+ // finishing
2197
+ #blocks(parent, definitions) {
2198
+ const blocks = [];
2199
+ for (const child of parent.children) {
2200
+ for (const definition of child.definitions) {
2201
+ blocks.push(definition);
2202
+ const key = Scan.label(definition.label);
2203
+ if (!definitions.has(key)) definitions.set(key, definition);
1717
2204
  }
2205
+ const block = this.#block(child, definitions);
2206
+ if (block) blocks.push(block);
1718
2207
  }
1719
- this.#position = index;
1720
- return this.#token(type, start, newline);
2208
+ return blocks;
1721
2209
  }
1722
- #number(start, newline) {
1723
- const pattern = /0[xX][\da-fA-F_]+n?|0[oO][0-7_]+n?|0[bB][01_]+n?|(?:\d[\d_]*\.?[\d_]*|\.\d[\d_]*)(?:[eE][+-]?\d[\d_]*)?n?/y;
1724
- pattern.lastIndex = start;
1725
- pattern.exec(this.source);
1726
- this.#position = pattern.lastIndex;
1727
- return this.#token("number", start, newline);
2210
+ #end(block) {
2211
+ const line = this.#lines[block.endLine - 1];
2212
+ return line ? line.start + line.text.length : this.#source.length;
1728
2213
  }
1729
- #string(start, newline) {
1730
- const quote = this.source[start];
1731
- for (let index = start + 1; index < this.source.length; index++) {
1732
- const char = this.source[index];
1733
- if (char === "\\")
1734
- index += this.source.startsWith("\r\n", index + 1) ? 2 : 1;
1735
- else if (char === quote) {
1736
- this.#position = index + 1;
1737
- return this.#token("string", start, newline);
1738
- } else if (char === "\n" || char === "\r") break;
2214
+ #block(open, definitions) {
2215
+ const base = {
2216
+ start: open.start,
2217
+ end: Math.max(this.#end(open), open.start),
2218
+ line: open.line,
2219
+ endLine: open.endLine
2220
+ };
2221
+ switch (open.kind) {
2222
+ case "Paragraph": {
2223
+ if (open.lines.length === 0) return;
2224
+ const text = open.lines.map((line) => line.trimStart()).join("\n");
2225
+ return {
2226
+ type: "Paragraph",
2227
+ ...base,
2228
+ text: text.replace(/[ \t]+$/, ""),
2229
+ indents: open.lines.map(
2230
+ (line, index) => `${open.indents[index] ?? ""}${/^[ \t]*/.exec(line)?.[0] ?? ""}`
2231
+ ),
2232
+ prefixes: open.prefixes,
2233
+ lazy: open.lines.map(
2234
+ (_, index) => open.lazy.has(open.line + index)
2235
+ ),
2236
+ children: []
2237
+ };
2238
+ }
2239
+ case "Heading": {
2240
+ const text = open.setext ? open.lines.map((line) => line.trim()).join("\n") : (open.lines[0] ?? "").trimStart();
2241
+ return {
2242
+ type: "Heading",
2243
+ ...base,
2244
+ level: open.level,
2245
+ setext: open.setext,
2246
+ text,
2247
+ underline: open.setext ? open.info : "",
2248
+ children: []
2249
+ };
2250
+ }
2251
+ case "ThematicBreak":
2252
+ return {
2253
+ type: "ThematicBreak",
2254
+ ...base,
2255
+ raw: open.lines[0] ?? ""
2256
+ };
2257
+ case "CodeBlock":
2258
+ return {
2259
+ type: "CodeBlock",
2260
+ ...base,
2261
+ fence: open.fence,
2262
+ indent: open.fenceOffset,
2263
+ info: open.info,
2264
+ lines: open.lines,
2265
+ prefixes: open.prefixes,
2266
+ closed: open.closed
2267
+ };
2268
+ case "HtmlBlock":
2269
+ return {
2270
+ type: "HtmlBlock",
2271
+ ...base,
2272
+ kind: open.htmlKind,
2273
+ lines: open.lines
2274
+ };
2275
+ case "BlockQuote":
2276
+ return {
2277
+ type: "BlockQuote",
2278
+ ...base,
2279
+ children: this.#blocks(open, definitions)
2280
+ };
2281
+ case "List": {
2282
+ const items = open.children.map(
2283
+ (item) => this.#block(item, definitions)
2284
+ );
2285
+ return {
2286
+ type: "List",
2287
+ ...base,
2288
+ ordered: open.ordered,
2289
+ delimiter: open.delimiter,
2290
+ tight: _Blocks.#tight(items),
2291
+ children: items
2292
+ };
2293
+ }
2294
+ case "ListItem": {
2295
+ const children = this.#blocks(open, definitions);
2296
+ let checked = null;
2297
+ const first = children[0];
2298
+ const task = this.#gfm && first?.type === "Paragraph" ? /^\[([ xX])\](?=[ \t]|$)/.exec(first.text) : null;
2299
+ if (task && first?.type === "Paragraph" && first.line === open.line) {
2300
+ checked = task[1] !== " ";
2301
+ }
2302
+ return {
2303
+ type: "ListItem",
2304
+ ...base,
2305
+ marker: open.marker,
2306
+ number: open.number,
2307
+ indent: open.markerOffset,
2308
+ spaces: open.spaces,
2309
+ checked,
2310
+ children
2311
+ };
2312
+ }
2313
+ case "Table": {
2314
+ const rows = open.lines.map((line, index) => ({ line, index })).filter(({ index }) => index !== 1).map(({ line, index }) => {
2315
+ const start = open.offsets[index] ?? open.start;
2316
+ return {
2317
+ type: "TableRow",
2318
+ start,
2319
+ end: start + line.length,
2320
+ line: open.line + index,
2321
+ endLine: open.line + index,
2322
+ cells: _Blocks.cells(line).map((text) => ({
2323
+ type: "TableCell",
2324
+ start,
2325
+ end: start + line.length,
2326
+ text,
2327
+ children: []
2328
+ }))
2329
+ };
2330
+ });
2331
+ return {
2332
+ type: "Table",
2333
+ ...base,
2334
+ alignments: open.alignments,
2335
+ rows
2336
+ };
2337
+ }
1739
2338
  }
1740
- return this.#invalid(start, "Unterminated string");
1741
2339
  }
1742
- static #identifierStart(char) {
1743
- return /[\p{ID_Start}$_]/u.test(char) || /[\ud800-\udbff]/.test(char);
2340
+ /** Lists are loose when blank lines separate items, or blocks of an item. */
2341
+ static #tight(items) {
2342
+ for (const [index, item] of items.entries()) {
2343
+ const next = items[index + 1];
2344
+ if (next && next.line > item.endLine + 1) return false;
2345
+ for (const [position, child] of item.children.entries()) {
2346
+ const sibling = item.children[position + 1];
2347
+ if (sibling && sibling.line > child.endLine + 1) return false;
2348
+ }
2349
+ }
2350
+ return true;
1744
2351
  }
1745
- static #identifierPart(char) {
1746
- return /[\p{ID_Continue}$‌‍]/u.test(char);
2352
+ static #inlines(blocks, inlines) {
2353
+ for (const block of blocks) {
2354
+ switch (block.type) {
2355
+ case "Paragraph":
2356
+ case "Heading":
2357
+ block.children = inlines.parse(block.text);
2358
+ break;
2359
+ case "Table":
2360
+ for (const row of block.rows)
2361
+ for (const cell of row.cells)
2362
+ cell.children = inlines.parse(cell.text);
2363
+ break;
2364
+ case "BlockQuote":
2365
+ _Blocks.#inlines(block.children, inlines);
2366
+ break;
2367
+ case "List":
2368
+ for (const item of block.children)
2369
+ _Blocks.#inlines(item.children, inlines);
2370
+ break;
2371
+ }
2372
+ }
1747
2373
  }
1748
2374
  };
1749
2375
 
@@ -3671,7 +4297,7 @@ var ExpressionParser = class _ExpressionParser extends TypeParser {
3671
4297
  };
3672
4298
 
3673
4299
  // shared/syntax/src/parser/statements.ts
3674
- var Parser = class extends ExpressionParser {
4300
+ var Parser2 = class extends ExpressionParser {
3675
4301
  static {
3676
4302
  __name(this, "Parser");
3677
4303
  }
@@ -4571,18 +5197,26 @@ function isMarkdown(file) {
4571
5197
  return /\.(?:md|markdown)$/i.test(file);
4572
5198
  }
4573
5199
  __name(isMarkdown, "isMarkdown");
5200
+ function isHtml(file) {
5201
+ return /\.html?$/i.test(file);
5202
+ }
5203
+ __name(isHtml, "isHtml");
5204
+ function parseHtml(source) {
5205
+ return new Parser(source).parse();
5206
+ }
5207
+ __name(parseHtml, "parseHtml");
4574
5208
  function parseMarkdown(source, options = {}) {
4575
5209
  return new Blocks(source, options).parse(options.frontmatter);
4576
5210
  }
4577
5211
  __name(parseMarkdown, "parseMarkdown");
4578
5212
  function parse(source, options) {
4579
- const parser = new Parser(source, options);
5213
+ const parser = new Parser2(source, options);
4580
5214
  const program = parser.parseProgram();
4581
5215
  return { program, comments: parser.comments };
4582
5216
  }
4583
5217
  __name(parse, "parse");
4584
5218
  function parseExpression(source) {
4585
- const parser = new Parser(source, { typescript: false, jsx: false });
5219
+ const parser = new Parser2(source, { typescript: false, jsx: false });
4586
5220
  const expression = parser.parseDocument();
4587
5221
  const statement = {
4588
5222
  type: "ExpressionStatement",
@@ -4604,9 +5238,12 @@ export {
4604
5238
  Lines,
4605
5239
  ParseError,
4606
5240
  Walker,
5241
+ isHtml,
4607
5242
  isMarkdown,
4608
5243
  language,
4609
5244
  parse,
4610
5245
  parseExpression,
4611
- parseMarkdown
5246
+ parseHtml,
5247
+ parseMarkdown,
5248
+ tagOf
4612
5249
  };