@aljazmc/syntax 0.3.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/README.md +33 -1
- package/bin/index.js +2155 -5
- package/bin/types/html/ast.d.ts +85 -0
- package/bin/types/html/parser.d.ts +12 -0
- package/bin/types/html/tags.d.ts +2 -0
- package/bin/types/index.d.ts +15 -1
- package/bin/types/markdown/ast.d.ts +156 -0
- package/bin/types/markdown/blocks.d.ts +15 -0
- package/bin/types/markdown/inlines.d.ts +7 -0
- package/bin/types/markdown/scan.d.ts +10 -0
- package/package.json +3 -3
package/bin/index.js
CHANGED
|
@@ -271,6 +271,2108 @@ var Scanner = class _Scanner {
|
|
|
271
271
|
}
|
|
272
272
|
};
|
|
273
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 {
|
|
526
|
+
static {
|
|
527
|
+
__name(this, "Parser");
|
|
528
|
+
}
|
|
529
|
+
#source;
|
|
530
|
+
#position = 0;
|
|
531
|
+
#comments = [];
|
|
532
|
+
constructor(source) {
|
|
533
|
+
this.#source = source;
|
|
534
|
+
}
|
|
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
|
+
};
|
|
556
|
+
}
|
|
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."
|
|
587
|
+
);
|
|
588
|
+
return children;
|
|
589
|
+
}
|
|
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;
|
|
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());
|
|
639
|
+
}
|
|
640
|
+
}
|
|
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++;
|
|
663
|
+
}
|
|
664
|
+
if (whitespace !== void 0) this.#position = whitespace;
|
|
665
|
+
return {
|
|
666
|
+
type: "Text",
|
|
667
|
+
start,
|
|
668
|
+
end: this.#position,
|
|
669
|
+
value: source.slice(start, this.#position)
|
|
670
|
+
};
|
|
671
|
+
}
|
|
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."
|
|
726
|
+
);
|
|
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++;
|
|
781
|
+
}
|
|
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
|
+
}
|
|
801
|
+
}
|
|
802
|
+
#value() {
|
|
803
|
+
const source = this.#source;
|
|
804
|
+
const start = this.#position;
|
|
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
|
+
};
|
|
818
|
+
}
|
|
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++;
|
|
825
|
+
}
|
|
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: ""
|
|
836
|
+
};
|
|
837
|
+
}
|
|
838
|
+
/** `<!DOCTYPE html>`, or with a literal and quoted identifiers. */
|
|
839
|
+
#doctype() {
|
|
840
|
+
const source = this.#source;
|
|
841
|
+
const start = this.#position;
|
|
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
|
|
873
|
+
};
|
|
874
|
+
}
|
|
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;
|
|
883
|
+
this.#position++;
|
|
884
|
+
}
|
|
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
|
+
}
|
|
923
|
+
return;
|
|
924
|
+
}
|
|
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++;
|
|
939
|
+
}
|
|
940
|
+
if (depth !== 0) return;
|
|
941
|
+
return [index, text.slice(from, index)];
|
|
942
|
+
}
|
|
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++) {
|
|
949
|
+
const char = text[index];
|
|
950
|
+
if (char === "\\") index++;
|
|
951
|
+
else if (char === close) return index + 1;
|
|
952
|
+
else if (open === "(" && char === "(") return;
|
|
953
|
+
else if (char === "\n" && text[index + 1] === "\n") return;
|
|
954
|
+
}
|
|
955
|
+
}
|
|
956
|
+
};
|
|
957
|
+
|
|
958
|
+
// shared/syntax/src/markdown/inlines.ts
|
|
959
|
+
var Item = class {
|
|
960
|
+
static {
|
|
961
|
+
__name(this, "Item");
|
|
962
|
+
}
|
|
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;
|
|
981
|
+
}
|
|
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;
|
|
990
|
+
}
|
|
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;
|
|
1008
|
+
}
|
|
1009
|
+
};
|
|
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 {
|
|
1020
|
+
static {
|
|
1021
|
+
__name(this, "Inlines");
|
|
1022
|
+
}
|
|
1023
|
+
#labels;
|
|
1024
|
+
#gfm;
|
|
1025
|
+
#text = "";
|
|
1026
|
+
#position = 0;
|
|
1027
|
+
#delimiters = null;
|
|
1028
|
+
#brackets = null;
|
|
1029
|
+
constructor(labels, gfm) {
|
|
1030
|
+
this.#labels = labels;
|
|
1031
|
+
this.#gfm = gfm;
|
|
1032
|
+
}
|
|
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);
|
|
1042
|
+
}
|
|
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;
|
|
1085
|
+
}
|
|
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;
|
|
1095
|
+
}
|
|
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
|
+
);
|
|
1105
|
+
}
|
|
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;
|
|
1111
|
+
}
|
|
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();
|
|
1124
|
+
}
|
|
1125
|
+
const item = new Item(
|
|
1126
|
+
hard ? "hardbreak" : "softbreak",
|
|
1127
|
+
start,
|
|
1128
|
+
this.#position + 1
|
|
1129
|
+
);
|
|
1130
|
+
block.append(item);
|
|
1131
|
+
this.#position++;
|
|
1132
|
+
while (this.#text[this.#position] === " ") this.#position++;
|
|
1133
|
+
}
|
|
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);
|
|
1142
|
+
}
|
|
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);
|
|
1158
|
+
}
|
|
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;
|
|
1181
|
+
}
|
|
1182
|
+
if (char === "~" && count > 2) {
|
|
1183
|
+
canOpen = false;
|
|
1184
|
+
canClose = false;
|
|
1185
|
+
}
|
|
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;
|
|
1199
|
+
}
|
|
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();
|
|
1258
|
+
}
|
|
1259
|
+
}
|
|
1260
|
+
if (text[this.#position] === ")") {
|
|
1261
|
+
this.#position++;
|
|
1262
|
+
matched = true;
|
|
1263
|
+
}
|
|
1264
|
+
}
|
|
1265
|
+
if (!matched) {
|
|
1266
|
+
this.#position = save;
|
|
1267
|
+
destination = "";
|
|
1268
|
+
title = null;
|
|
1269
|
+
}
|
|
1270
|
+
}
|
|
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;
|
|
1284
|
+
}
|
|
1285
|
+
if (!matched) {
|
|
1286
|
+
this.#brackets = opener.previous;
|
|
1287
|
+
block.append(new Item("text", close, close + 1));
|
|
1288
|
+
return;
|
|
1289
|
+
}
|
|
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;
|
|
1305
|
+
}
|
|
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;
|
|
1313
|
+
}
|
|
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;
|
|
1330
|
+
}
|
|
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;
|
|
1356
|
+
}
|
|
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
|
+
}
|
|
1374
|
+
}
|
|
1375
|
+
}
|
|
1376
|
+
opener = opener.previous;
|
|
1377
|
+
}
|
|
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;
|
|
1398
|
+
}
|
|
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;
|
|
1415
|
+
}
|
|
1416
|
+
} else {
|
|
1417
|
+
closer = closer.next;
|
|
1418
|
+
openersBottom.set(key, old.previous);
|
|
1419
|
+
if (!old.canOpen) this.#remove(old);
|
|
1420
|
+
}
|
|
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;
|
|
1483
|
+
}
|
|
1484
|
+
}
|
|
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;
|
|
1552
|
+
}
|
|
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
|
+
}
|
|
1585
|
+
}
|
|
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
|
+
};
|
|
1606
|
+
}
|
|
1607
|
+
#create(kind, parent, start, line) {
|
|
1608
|
+
return {
|
|
1609
|
+
kind,
|
|
1610
|
+
parent,
|
|
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];
|
|
1648
|
+
}
|
|
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;
|
|
1654
|
+
}
|
|
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];
|
|
1686
|
+
}
|
|
1687
|
+
}
|
|
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);
|
|
1698
|
+
}
|
|
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;
|
|
1708
|
+
}
|
|
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)
|
|
1751
|
+
);
|
|
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;
|
|
1815
|
+
}
|
|
1816
|
+
}
|
|
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;
|
|
1837
|
+
}
|
|
1838
|
+
}
|
|
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;
|
|
1857
|
+
}
|
|
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;
|
|
1865
|
+
}
|
|
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();
|
|
1883
|
+
}
|
|
1884
|
+
}
|
|
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;
|
|
1898
|
+
}
|
|
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;
|
|
1914
|
+
}
|
|
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
|
+
}
|
|
1936
|
+
}
|
|
1937
|
+
}
|
|
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
|
+
}
|
|
1946
|
+
}
|
|
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
|
+
}
|
|
1959
|
+
}
|
|
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;
|
|
1967
|
+
}
|
|
1968
|
+
}
|
|
1969
|
+
if (!this.#indented || container.kind === "List") {
|
|
1970
|
+
const item = this.#listMarker(container);
|
|
1971
|
+
if (item) return 1;
|
|
1972
|
+
}
|
|
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;
|
|
1978
|
+
}
|
|
1979
|
+
return 0;
|
|
1980
|
+
}
|
|
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;
|
|
2017
|
+
}
|
|
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;
|
|
2071
|
+
}
|
|
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;
|
|
2092
|
+
}
|
|
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
|
|
2120
|
+
});
|
|
2121
|
+
position = end;
|
|
2122
|
+
}
|
|
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
|
+
}
|
|
2160
|
+
}
|
|
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];
|
|
2174
|
+
}
|
|
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
|
+
}
|
|
2184
|
+
}
|
|
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;
|
|
2195
|
+
}
|
|
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);
|
|
2204
|
+
}
|
|
2205
|
+
const block = this.#block(child, definitions);
|
|
2206
|
+
if (block) blocks.push(block);
|
|
2207
|
+
}
|
|
2208
|
+
return blocks;
|
|
2209
|
+
}
|
|
2210
|
+
#end(block) {
|
|
2211
|
+
const line = this.#lines[block.endLine - 1];
|
|
2212
|
+
return line ? line.start + line.text.length : this.#source.length;
|
|
2213
|
+
}
|
|
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
|
+
}
|
|
2338
|
+
}
|
|
2339
|
+
}
|
|
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;
|
|
2351
|
+
}
|
|
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
|
+
}
|
|
2373
|
+
}
|
|
2374
|
+
};
|
|
2375
|
+
|
|
274
2376
|
// shared/syntax/src/parser/base.ts
|
|
275
2377
|
var BaseParser = class {
|
|
276
2378
|
static {
|
|
@@ -979,7 +3081,34 @@ var ExpressionParser = class _ExpressionParser extends TypeParser {
|
|
|
979
3081
|
"?",
|
|
980
3082
|
"{",
|
|
981
3083
|
"extends",
|
|
982
|
-
"implements"
|
|
3084
|
+
"implements",
|
|
3085
|
+
// like TypeScript, binary operators that cannot start an expression,
|
|
3086
|
+
// `<`, `>`, `+` and `-` would read as comparisons or unary operators
|
|
3087
|
+
"*",
|
|
3088
|
+
"/",
|
|
3089
|
+
"%",
|
|
3090
|
+
"**",
|
|
3091
|
+
"|",
|
|
3092
|
+
"^",
|
|
3093
|
+
"&",
|
|
3094
|
+
"<<",
|
|
3095
|
+
"<=",
|
|
3096
|
+
"as",
|
|
3097
|
+
"satisfies",
|
|
3098
|
+
"instanceof",
|
|
3099
|
+
"+=",
|
|
3100
|
+
"-=",
|
|
3101
|
+
"*=",
|
|
3102
|
+
"/=",
|
|
3103
|
+
"%=",
|
|
3104
|
+
"**=",
|
|
3105
|
+
"<<=",
|
|
3106
|
+
"&=",
|
|
3107
|
+
"|=",
|
|
3108
|
+
"^=",
|
|
3109
|
+
"&&=",
|
|
3110
|
+
"||=",
|
|
3111
|
+
"??="
|
|
983
3112
|
]);
|
|
984
3113
|
parseExpression() {
|
|
985
3114
|
const start = this.token.start;
|
|
@@ -2168,7 +4297,7 @@ var ExpressionParser = class _ExpressionParser extends TypeParser {
|
|
|
2168
4297
|
};
|
|
2169
4298
|
|
|
2170
4299
|
// shared/syntax/src/parser/statements.ts
|
|
2171
|
-
var
|
|
4300
|
+
var Parser2 = class extends ExpressionParser {
|
|
2172
4301
|
static {
|
|
2173
4302
|
__name(this, "Parser");
|
|
2174
4303
|
}
|
|
@@ -3064,14 +5193,30 @@ function language(file) {
|
|
|
3064
5193
|
};
|
|
3065
5194
|
}
|
|
3066
5195
|
__name(language, "language");
|
|
5196
|
+
function isMarkdown(file) {
|
|
5197
|
+
return /\.(?:md|markdown)$/i.test(file);
|
|
5198
|
+
}
|
|
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");
|
|
5208
|
+
function parseMarkdown(source, options = {}) {
|
|
5209
|
+
return new Blocks(source, options).parse(options.frontmatter);
|
|
5210
|
+
}
|
|
5211
|
+
__name(parseMarkdown, "parseMarkdown");
|
|
3067
5212
|
function parse(source, options) {
|
|
3068
|
-
const parser = new
|
|
5213
|
+
const parser = new Parser2(source, options);
|
|
3069
5214
|
const program = parser.parseProgram();
|
|
3070
5215
|
return { program, comments: parser.comments };
|
|
3071
5216
|
}
|
|
3072
5217
|
__name(parse, "parse");
|
|
3073
5218
|
function parseExpression(source) {
|
|
3074
|
-
const parser = new
|
|
5219
|
+
const parser = new Parser2(source, { typescript: false, jsx: false });
|
|
3075
5220
|
const expression = parser.parseDocument();
|
|
3076
5221
|
const statement = {
|
|
3077
5222
|
type: "ExpressionStatement",
|
|
@@ -3093,7 +5238,12 @@ export {
|
|
|
3093
5238
|
Lines,
|
|
3094
5239
|
ParseError,
|
|
3095
5240
|
Walker,
|
|
5241
|
+
isHtml,
|
|
5242
|
+
isMarkdown,
|
|
3096
5243
|
language,
|
|
3097
5244
|
parse,
|
|
3098
|
-
parseExpression
|
|
5245
|
+
parseExpression,
|
|
5246
|
+
parseHtml,
|
|
5247
|
+
parseMarkdown,
|
|
5248
|
+
tagOf
|
|
3099
5249
|
};
|