@aljazmc/syntax 0.3.0 → 0.4.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,6 +1,1482 @@
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 {
10
+ static {
11
+ __name(this, "Scan");
12
+ }
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();
16
+ }
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;
36
+ }
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
+ }
44
+ if (depth !== 0) return;
45
+ return [index, text.slice(from, index)];
46
+ }
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];
54
+ 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;
58
+ }
59
+ }
60
+ };
61
+
62
+ // shared/syntax/src/markdown/inlines.ts
63
+ var Item = class {
64
+ static {
65
+ __name(this, "Item");
66
+ }
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;
85
+ }
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;
94
+ }
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;
103
+ }
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;
112
+ }
113
+ };
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 {
124
+ static {
125
+ __name(this, "Inlines");
126
+ }
127
+ #labels;
128
+ #gfm;
129
+ #text = "";
130
+ #position = 0;
131
+ #delimiters = null;
132
+ #brackets = null;
133
+ constructor(labels, gfm) {
134
+ this.#labels = labels;
135
+ this.#gfm = gfm;
136
+ }
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);
146
+ }
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
187
+ );
188
+ return;
189
+ }
190
+ break;
191
+ }
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;
199
+ }
200
+ }
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
+ }
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();
228
+ }
229
+ const item = new Item(
230
+ hard ? "hardbreak" : "softbreak",
231
+ 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);
246
+ }
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
258
+ );
259
+ return;
260
+ }
261
+ this.#add(block, "text", run.length);
262
+ }
263
+ #delimiter(block, char) {
264
+ const text = this.#text;
265
+ 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;
285
+ }
286
+ if (char === "~" && count > 2) {
287
+ canOpen = false;
288
+ canClose = false;
289
+ }
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
300
+ };
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
+ }
313
+ #openBracket(block, image) {
314
+ 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
325
+ };
326
+ }
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] === "(") {
348
+ 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
+ }
389
+ if (!matched) {
390
+ this.#brackets = opener.previous;
391
+ block.append(new Item("text", close, close + 1));
392
+ return;
393
+ }
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;
417
+ }
418
+ }
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++) {
424
+ const char = text[index];
425
+ 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
+ }
435
+ }
436
+ return 0;
437
+ }
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
+ }
446
+ }
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;
451
+ }
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);
528
+ }
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;
590
+ }
591
+ };
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 {
615
+ static {
616
+ __name(this, "Blocks");
617
+ }
618
+ #source;
619
+ #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;
758
+ }
759
+ #advanceNextNonspace() {
760
+ this.#offset = this.#nextNonspace;
761
+ this.#column = this.#nextNonspaceColumn;
762
+ this.#partialTab = false;
763
+ }
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;
782
+ }
783
+ } else {
784
+ this.#partialTab = false;
785
+ this.#offset += 1;
786
+ this.#column += 1;
787
+ remaining -= 1;
788
+ }
789
+ char = line[this.#offset];
790
+ }
791
+ }
792
+ #rest() {
793
+ return this.#line.slice(this.#nextNonspace);
794
+ }
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);
802
+ }
803
+ this.#tip.indents.push(
804
+ this.#line.slice(
805
+ this.#contentStart,
806
+ Math.max(this.#offset, this.#contentStart)
807
+ )
808
+ );
809
+ this.#tip.lines.push(text);
810
+ this.#tip.offsets.push(this.#lineStart + this.#offset);
811
+ this.#tip.endLine = this.#lineNumber;
812
+ }
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;
825
+ }
826
+ static #canContain(parent, child) {
827
+ if (parent === "List") return child === "ListItem";
828
+ return (parent === "Root" || parent === "BlockQuote" || parent === "ListItem") && child !== "ListItem";
829
+ }
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;
836
+ }
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);
850
+ }
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;
857
+ }
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--;
896
+ }
897
+ return 0;
898
+ }
899
+ if (this.#indent >= 4) {
900
+ this.#advance(4, true);
901
+ return 0;
902
+ }
903
+ if (this.#blank) {
904
+ this.#advanceNextNonspace();
905
+ return 0;
906
+ }
907
+ return 1;
908
+ }
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;
919
+ }
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;
961
+ }
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;
968
+ return;
969
+ }
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();
987
+ }
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;
1002
+ }
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;
1018
+ }
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;
1031
+ }
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;
1039
+ }
1040
+ }
1041
+ }
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;
1049
+ }
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;
1062
+ }
1063
+ }
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;
1071
+ }
1072
+ }
1073
+ if (!this.#indented || container.kind === "List") {
1074
+ const item = this.#listMarker(container);
1075
+ if (item) return 1;
1076
+ }
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;
1082
+ }
1083
+ return 0;
1084
+ }
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",
1094
+ 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);
1117
+ }
1118
+ this.#tip = table;
1119
+ this.#advance(this.#line.length - this.#offset);
1120
+ this.#consumed = true;
1121
+ }
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;
1167
+ }
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
+ }
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;
1193
+ }
1194
+ cells.push(cell.trim());
1195
+ return cells;
1196
+ }
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)
1212
+ );
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;
1226
+ }
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
+ }
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);
1263
+ }
1264
+ }
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];
1274
+ }
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;
1287
+ }
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++;
1297
+ }
1298
+ return index;
1299
+ }
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);
1311
+ }
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
+ };
1397
+ }
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] !== " ";
1405
+ }
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
+ }
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
+ };
1441
+ }
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;
1452
+ }
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;
1475
+ }
1476
+ }
1477
+ }
1478
+ };
1479
+
4
1480
  // shared/syntax/src/scanner.ts
5
1481
  var ParseError = class extends SyntaxError {
6
1482
  static {
@@ -979,7 +2455,34 @@ var ExpressionParser = class _ExpressionParser extends TypeParser {
979
2455
  "?",
980
2456
  "{",
981
2457
  "extends",
982
- "implements"
2458
+ "implements",
2459
+ // like TypeScript, binary operators that cannot start an expression,
2460
+ // `<`, `>`, `+` and `-` would read as comparisons or unary operators
2461
+ "*",
2462
+ "/",
2463
+ "%",
2464
+ "**",
2465
+ "|",
2466
+ "^",
2467
+ "&",
2468
+ "<<",
2469
+ "<=",
2470
+ "as",
2471
+ "satisfies",
2472
+ "instanceof",
2473
+ "+=",
2474
+ "-=",
2475
+ "*=",
2476
+ "/=",
2477
+ "%=",
2478
+ "**=",
2479
+ "<<=",
2480
+ "&=",
2481
+ "|=",
2482
+ "^=",
2483
+ "&&=",
2484
+ "||=",
2485
+ "??="
983
2486
  ]);
984
2487
  parseExpression() {
985
2488
  const start = this.token.start;
@@ -3064,6 +4567,14 @@ function language(file) {
3064
4567
  };
3065
4568
  }
3066
4569
  __name(language, "language");
4570
+ function isMarkdown(file) {
4571
+ return /\.(?:md|markdown)$/i.test(file);
4572
+ }
4573
+ __name(isMarkdown, "isMarkdown");
4574
+ function parseMarkdown(source, options = {}) {
4575
+ return new Blocks(source, options).parse(options.frontmatter);
4576
+ }
4577
+ __name(parseMarkdown, "parseMarkdown");
3067
4578
  function parse(source, options) {
3068
4579
  const parser = new Parser(source, options);
3069
4580
  const program = parser.parseProgram();
@@ -3093,7 +4604,9 @@ export {
3093
4604
  Lines,
3094
4605
  ParseError,
3095
4606
  Walker,
4607
+ isMarkdown,
3096
4608
  language,
3097
4609
  parse,
3098
- parseExpression
4610
+ parseExpression,
4611
+ parseMarkdown
3099
4612
  };