switchroom 0.21.9 → 0.21.10

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.
@@ -12,7 +12,11 @@
12
12
  // `source.slice(node.start, node.end)` round-trips to the source text.
13
13
  // - Never lose text. Any mdast node type outside the supported palette
14
14
  // degrades to a `plain` inline (or a paragraph wrapping one) carrying the
15
- // raw source slice, rather than being dropped.
15
+ // raw source slice, rather than being dropped. The ONE deliberate
16
+ // exception is raw-HTML MARKUP: an unrecognised tag has its angle-bracket
17
+ // token dropped while its content is kept and rendered (see the raw-HTML
18
+ // note below and `html-fold.ts`). Content is never lost; unguaranteeable
19
+ // markup is.
16
20
  //
17
21
  // Underline vs bold (`__…__` vs `**…**`):
18
22
  // Telegram's Bot API 10.1 rich markdown reads a `__…__` double-underscore run
@@ -56,6 +60,25 @@
56
60
  // marker characters differ, and they are never part of quoted content. The
57
61
  // set of line-start offsets that carried the marker is threaded into
58
62
  // `foldBlock` so the matching blockquote nodes get `expandable: true`.
63
+ //
64
+ // The rewrite is FENCE-AWARE. Every other fold reads the ORIGINAL `markdown`
65
+ // through `slice()`, so the rewritten bytes never escape — except for the
66
+ // `code` fold, which must use mdast's `node.value` (the dedented, fence-
67
+ // stripped content, which offsets alone cannot reconstruct without
68
+ // re-implementing micromark's fence parser). That made a fenced block
69
+ // containing a line starting `**>` ship silently corrupted as ` >` —
70
+ // anyone documenting the legacy syntax got their code altered. Skipping
71
+ // fenced regions in the pre-pass fixes it at the one place the rewrite is
72
+ // decided, keeps `code.value` trustworthy for every consumer, and preserves
73
+ // the length invariant trivially (a skipped line is copied verbatim).
74
+ //
75
+ // Raw HTML handling:
76
+ // mdast `html` nodes (inline `<b>`/`<a href>` runs and whole HTML blocks)
77
+ // used to fall through to `plain`, which the renderer `escapeMarkdown`s —
78
+ // escaping `=` and shipping `<a href\="…">`. They now go through
79
+ // `html-fold.ts`'s three-bucket policy: fold to the native IR construct,
80
+ // pass through raw (wire-verified allowlist), or drop the markup and keep
81
+ // the content. See that module's header for the rationale.
59
82
 
60
83
  import { fromMarkdown } from "mdast-util-from-markdown";
61
84
  import { gfm } from "micromark-extension-gfm";
@@ -78,6 +101,19 @@ import type {
78
101
  TableCell,
79
102
  TableRow,
80
103
  } from "./ir.js";
104
+ import {
105
+ HTML_FOLD_TAGS,
106
+ escapeHtmlLiteral,
107
+ hrefOf,
108
+ isBlockLevelTag,
109
+ isPassthroughTag,
110
+ isVoidTag,
111
+ languageOf,
112
+ matchedTagOffsets,
113
+ tokenizeHtml,
114
+ type HtmlFoldTarget,
115
+ type HtmlTagInfo,
116
+ } from "./html-fold.js";
81
117
 
82
118
  /** Copy UTF-16 offsets off an mdast node. Falls back to 0-length when a
83
119
  * synthesized node lacks a position (from-markdown always sets one, but the
@@ -125,13 +161,42 @@ function isTgInlineEntityHref(href: string): boolean {
125
161
  return TG_INLINE_ENTITY_HREFS.some((base) => h === base || h.startsWith(`${base}?`));
126
162
  }
127
163
 
164
+ /** A top-level fenced-code delimiter: 3+ backticks or tildes, indented 0–3
165
+ * spaces. Deeper indentation is not a fence, and a fence nested inside a
166
+ * blockquote / list item is prefixed by its container's markup, so this only
167
+ * ever tracks TOP-LEVEL fences.
168
+ *
169
+ * KNOWN LIMITATION (pre-existing, not a safety property): tracking only
170
+ * top-level fences does NOT establish that every column-0 `**>` line outside
171
+ * one is a real blockquote candidate. Counterexample:
172
+ *
173
+ * > ```
174
+ * **> lazy
175
+ * > ```
176
+ *
177
+ * The `> ``` ` opener is inside a blockquote so it never matches here; the
178
+ * column-0 `**> lazy` line is rewritten to ` > lazy` and the `**` is eaten.
179
+ * CommonMark forbids lazy continuation into fenced code, so that line should
180
+ * CLOSE the blockquote and stay literal. Fixing it needs real container
181
+ * tracking (blockquote/list prefixes), not a wider fence regex. The case is
182
+ * pinned by a test in `tests/render/html-dialect.test.ts` so any future
183
+ * container-aware rewrite has to decide it deliberately. */
184
+ const FENCE_DELIM_RE = /^ {0,3}(`{3,}|~{3,})/;
185
+
128
186
  /** Pre-transform expandable-blockquote markers so mdast can parse them as
129
187
  * ordinary blockquotes, WITHOUT shifting any source offset. Each line that
130
- * opens with `**>` has its two `*` characters replaced by two spaces
131
- * (`**>` → ` >`), which micromark reads as a normal (optionally
132
- * 1–3-space-indented) blockquote line. Returns the rewritten text plus the
133
- * set of line-start offsets that carried the marker — `foldBlock` uses that
134
- * set to flip `expandable: true` on the produced blockquote nodes. */
188
+ * opens with `**>` — and is NOT inside a fenced code block — has its two `*`
189
+ * characters replaced by two spaces (`**>` → ` >`), which micromark reads as
190
+ * a normal (optionally 1–3-space-indented) blockquote line. Returns the
191
+ * rewritten text plus the set of line-start offsets that carried the marker —
192
+ * `foldBlock` uses that set to flip `expandable: true` on the produced
193
+ * blockquote nodes.
194
+ *
195
+ * Fence tracking mirrors CommonMark: an opener is 3+ backticks/tildes at
196
+ * 0–3 spaces of indent; the block closes on a delimiter of the SAME character
197
+ * that is at least as long and carries nothing but whitespace after it, or at
198
+ * end of document. Inside such a block every line is copied verbatim, so a
199
+ * documented `**> …` example survives byte-identical into `code.value`. */
135
200
  function markExpandableQuotes(markdown: string): {
136
201
  text: string;
137
202
  expandableLineStarts: Set<number>;
@@ -139,8 +204,38 @@ function markExpandableQuotes(markdown: string): {
139
204
  const expandableLineStarts = new Set<number>();
140
205
  let out = "";
141
206
  let offset = 0;
207
+ /** The open fence's delimiter run, or null outside a fenced block. */
208
+ let openFence: string | null = null;
142
209
  // Split keeping the trailing newline on each line so offsets are exact.
143
210
  for (const line of markdown.split(/(?<=\n)/)) {
211
+ const body = line.replace(/\r?\n$/, "");
212
+ const fence = FENCE_DELIM_RE.exec(body)?.[1] ?? null;
213
+ if (openFence !== null) {
214
+ // Inside a fence: copy verbatim, and close on a matching delimiter.
215
+ out += line;
216
+ if (
217
+ fence !== null &&
218
+ fence[0] === openFence[0] &&
219
+ fence.length >= openFence.length &&
220
+ body.slice(body.indexOf(fence) + fence.length).trim() === ""
221
+ ) {
222
+ openFence = null;
223
+ }
224
+ offset += line.length;
225
+ continue;
226
+ }
227
+ if (fence !== null) {
228
+ // A backtick info string may not contain a backtick; such a line is not
229
+ // a fence opener at all (CommonMark), so only treat it as one when the
230
+ // rest of the line is clean.
231
+ const rest = body.slice(body.indexOf(fence) + fence.length);
232
+ if (!(fence[0] === "`" && rest.includes("`"))) {
233
+ openFence = fence;
234
+ out += line;
235
+ offset += line.length;
236
+ continue;
237
+ }
238
+ }
144
239
  if (EXPANDABLE_MARKER_RE.test(line)) {
145
240
  expandableLineStarts.add(offset);
146
241
  // Replace the leading two `*` chars with two spaces; the `>` and
@@ -168,7 +263,7 @@ function foldAlign(a: AlignType | undefined): "left" | "center" | "right" | null
168
263
  return a ?? null;
169
264
  }
170
265
 
171
- function foldInline(node: PhrasingContent, source: string): Inline {
266
+ function foldInline(node: PhrasingContent, source: string, ctx: FoldCtx): Inline {
172
267
  switch (node.type) {
173
268
  case "text":
174
269
  return { type: "plain", text: node.value, ...pos(node) };
@@ -179,21 +274,21 @@ function foldInline(node: PhrasingContent, source: string): Inline {
179
274
  const underline = source.slice(p.start, p.start + 2) === "__";
180
275
  return {
181
276
  type: underline ? "underline" : "bold",
182
- children: foldInlineChildren(node, source),
277
+ children: foldInlineChildren(node, source, ctx),
183
278
  ...p,
184
279
  };
185
280
  }
186
281
  case "emphasis":
187
- return { type: "italic", children: foldInlineChildren(node, source), ...pos(node) };
282
+ return { type: "italic", children: foldInlineChildren(node, source, ctx), ...pos(node) };
188
283
  case "delete":
189
- return { type: "strike", children: foldInlineChildren(node, source), ...pos(node) };
284
+ return { type: "strike", children: foldInlineChildren(node, source, ctx), ...pos(node) };
190
285
  case "inlineCode":
191
286
  return { type: "code", text: node.value, ...pos(node) };
192
287
  case "link":
193
288
  return {
194
289
  type: "link",
195
290
  href: node.url,
196
- children: foldInlineChildren(node, source),
291
+ children: foldInlineChildren(node, source, ctx),
197
292
  ...pos(node),
198
293
  };
199
294
  case "image": {
@@ -290,42 +385,444 @@ function expandPlainNode(node: PlainNode, source: string): Inline[] {
290
385
  return out;
291
386
  }
292
387
 
388
+ // ---------------------------------------------------------------------------
389
+ // Raw-HTML tag folding (see html-fold.ts for the policy and its rationale)
390
+ // ---------------------------------------------------------------------------
391
+
392
+ /** One element of the stream the HTML matcher walks: either a classified HTML
393
+ * token or an already-folded IR inline. */
394
+ type HtmlFoldItem =
395
+ | { kind: "tag"; tag: HtmlTagInfo; start: number; end: number }
396
+ | { kind: "inline"; node: Inline };
397
+
398
+ /** Index of the token that closes `name`, honouring same-name nesting, or -1. */
399
+ function findClosingTag(items: HtmlFoldItem[], from: number, name: string): number {
400
+ let depth = 0;
401
+ for (let i = from; i < items.length; i++) {
402
+ const it = items[i];
403
+ if (it.kind !== "tag" || it.tag.name !== name) continue;
404
+ if (it.tag.kind === "open") depth++;
405
+ else if (it.tag.kind === "close") {
406
+ if (depth === 0) return i;
407
+ depth--;
408
+ }
409
+ }
410
+ return -1;
411
+ }
412
+
413
+ /** Flatten a folded run to literal text, or null when any child carries
414
+ * structure a code span cannot represent. Used for `<code>`. Deliberately
415
+ * accepts ONLY `plain`: an inner `code` node's own backticks are not in its
416
+ * `text`, so folding it in would silently DELETE them
417
+ * (`<code>a `b` c</code>` -> `` `a b c` ``), and a `raw` node's wire bytes
418
+ * would be swallowed into a literal span. Both degrade to the children
419
+ * instead, which preserves every byte. */
420
+ function inlineLiteralText(nodes: Inline[]): string | null {
421
+ let out = "";
422
+ for (const n of nodes) {
423
+ if (n.type !== "plain") return null;
424
+ out += n.text;
425
+ }
426
+ return out;
427
+ }
428
+
429
+ /** Where a folded run is being emitted. `noBreaks` marks a container whose
430
+ * content must stay on ONE line — a heading or a table cell — so a `<br>` or
431
+ * a structural separator has to degrade to a space instead of a line break,
432
+ * and a `<pre>` to an inline code span instead of a fence. Without this a
433
+ * `# a<br>b` heading emitted `# a \nb`, which ends the heading block and
434
+ * drops `b` out of it. */
435
+ interface FoldCtx {
436
+ noBreaks?: boolean;
437
+ /** Source offsets of every HTML token the DOCUMENT-level pass found a
438
+ * partner for (`matchedTagOffsets`). A token missing from this set is
439
+ * unmatched — prose, not markup. Empty means "no HTML in this document",
440
+ * in which case nothing consults it. */
441
+ matched: ReadonlySet<number>;
442
+ }
443
+
444
+ /** Separator nodes this module synthesized while degrading structural markup.
445
+ * Tracked by identity so `normalizeSeparators` can collapse and trim OUR
446
+ * separators without touching a hard break the author actually wrote with
447
+ * `<br>` (`a<br><br>b` must keep both). */
448
+ const structuralSeparators = new WeakSet<Inline>();
449
+
450
+ /** The whitespace a degraded block-level tag leaves behind. A GFM HARD break
451
+ * (two spaces + newline), not a bare `\n`: the renderer runs AFTER
452
+ * `normalizeParagraphBreaks`, so a lone `\n` would never be promoted and
453
+ * Telegram collapses it to a space. */
454
+ function mkSeparator(ctx: FoldCtx, start: number, end: number): Inline {
455
+ const node: Inline = {
456
+ type: "raw",
457
+ text: ctx.noBreaks === true ? " " : " \n",
458
+ start,
459
+ end,
460
+ };
461
+ structuralSeparators.add(node);
462
+ return node;
463
+ }
464
+
465
+ /** Drop leading/trailing synthesized separators and collapse runs of them, so
466
+ * a degrade never contributes stray whitespace at the edges of a block
467
+ * (`<div></div>` must still render to nothing, and a `<pre>` fence must not
468
+ * leave a dangling newline). */
469
+ function normalizeSeparators(nodes: Inline[]): Inline[] {
470
+ const out: Inline[] = [];
471
+ for (const node of nodes) {
472
+ if (!structuralSeparators.has(node)) {
473
+ out.push(node);
474
+ continue;
475
+ }
476
+ if (out.length === 0) continue;
477
+ if (structuralSeparators.has(out[out.length - 1])) continue;
478
+ out.push(node);
479
+ }
480
+ while (out.length > 0 && structuralSeparators.has(out[out.length - 1])) out.pop();
481
+ return out;
482
+ }
483
+
484
+ /** An unmatched marker or unclassifiable token kept as LITERAL text, with its
485
+ * angle brackets HTML-entity-escaped so the wire sees prose, not a tag. */
486
+ function literalTag(tag: HtmlTagInfo, start: number, end: number): Inline {
487
+ return { type: "plain", text: escapeHtmlLiteral(tag.raw), start, end };
488
+ }
489
+
490
+ /**
491
+ * Apply the HTML dialect policy over a mixed tag/inline stream.
492
+ *
493
+ * `active` carries the constructs already open in an ANCESTOR fold. Markdown
494
+ * cannot nest same-kind emphasis — `**a **b** c**` is asterisk soup on the
495
+ * reader's screen, not nested bold — so an emphasis tag whose target is
496
+ * already active degrades to its children. Combined with the single-child
497
+ * unwrap in `buildFoldedTag`, `<b>a <b>b</b> c</b>` and `<b>**already**</b>`
498
+ * both come out as ONE bold run. A nested `<a>` is NOT unwrapped that way: its
499
+ * href is content, so it degrades to escaped literal markup instead of being
500
+ * silently deleted.
501
+ *
502
+ * See `html-fold.ts` for the bucket policy and the matched-vs-unmatched
503
+ * discriminator this implements.
504
+ */
505
+ function foldHtmlItems(
506
+ items: HtmlFoldItem[],
507
+ active: ReadonlySet<HtmlFoldTarget>,
508
+ ctx: FoldCtx,
509
+ ): Inline[] {
510
+ const out: Inline[] = [];
511
+ let i = 0;
512
+ while (i < items.length) {
513
+ const it = items[i];
514
+ if (it.kind === "inline") {
515
+ out.push(it.node);
516
+ i++;
517
+ continue;
518
+ }
519
+ const tag = it.tag;
520
+
521
+ // A comment renders nothing anywhere: dropping it loses no content.
522
+ if (tag.kind === "comment") {
523
+ i++;
524
+ continue;
525
+ }
526
+
527
+ // `<br>` / `<br/>` — a real line break (a space where breaks are illegal).
528
+ if (HTML_FOLD_TAGS[tag.name] === "break" && tag.kind !== "close") {
529
+ out.push(
530
+ ctx.noBreaks === true
531
+ ? { type: "raw", text: " ", start: it.start, end: it.end }
532
+ : { type: "raw", text: " \n", start: it.start, end: it.end },
533
+ );
534
+ i++;
535
+ continue;
536
+ }
537
+
538
+ // Void / self-closing tags delimit no content, so dropping their markup
539
+ // cannot lose text. Block-level ones still leave a separator.
540
+ if (tag.kind === "selfclose" || (tag.kind === "open" && isVoidTag(tag.name))) {
541
+ if (isPassthroughTag(tag)) {
542
+ out.push({ type: "raw", text: tag.raw, start: it.start, end: it.end });
543
+ } else if (isBlockLevelTag(tag.name)) {
544
+ out.push(mkSeparator(ctx, it.start, it.end));
545
+ }
546
+ i++;
547
+ continue;
548
+ }
549
+
550
+ // Bucket 2: the wire-verified allowlist, now BALANCE-CHECKED. Emitting an
551
+ // unmatched `<u>` raw is precisely the `unclosed start tag` 400 the fold
552
+ // policy exists to prevent — and the fallback resends the whole message as
553
+ // PLAIN TEXT, so one stray marker in relayed content degrades the entire
554
+ // reply. The check is document-level because a `<details>` open and close
555
+ // legitimately land in different mdast blocks.
556
+ if (isPassthroughTag(tag) && (tag.kind === "open" || tag.kind === "close")) {
557
+ out.push(
558
+ ctx.matched.has(it.start)
559
+ ? { type: "raw", text: tag.raw, start: it.start, end: it.end }
560
+ : literalTag(tag, it.start, it.end),
561
+ );
562
+ i++;
563
+ continue;
564
+ }
565
+
566
+ // Everything else must be BALANCED to count as markup.
567
+ if (tag.kind === "open") {
568
+ const close = findClosingTag(items, i + 1, tag.name);
569
+ const closeItem = close >= 0 ? items[close] : undefined;
570
+ if (closeItem !== undefined && closeItem.kind === "tag") {
571
+ out.push(
572
+ ...foldMatchedTag(tag, closeItem, items.slice(i + 1, close), active, ctx, {
573
+ start: it.start,
574
+ end: closeItem.end,
575
+ }),
576
+ );
577
+ i = close + 1;
578
+ continue;
579
+ }
580
+ }
581
+
582
+ // Matched somewhere ELSE in the document (a `<div>` whose `</div>` is a
583
+ // separate mdast block) — markup, so drop it, but leave a separator for a
584
+ // block-level name so the neighbouring blocks do not glue.
585
+ if (ctx.matched.has(it.start) && tag.kind !== "other") {
586
+ if (isBlockLevelTag(tag.name)) out.push(mkSeparator(ctx, it.start, it.end));
587
+ i++;
588
+ continue;
589
+ }
590
+
591
+ // Unmatched open, stray close, or an unclassifiable token: PROSE.
592
+ out.push(literalTag(tag, it.start, it.end));
593
+ i++;
594
+ }
595
+ return normalizeSeparators(out);
596
+ }
597
+
598
+ /** Emit a tag whose closing partner was found. */
599
+ function foldMatchedTag(
600
+ tag: HtmlTagInfo,
601
+ closeTag: HtmlFoldItem & { kind: "tag" },
602
+ innerItems: HtmlFoldItem[],
603
+ active: ReadonlySet<HtmlFoldTarget>,
604
+ ctx: FoldCtx,
605
+ span: Pos,
606
+ ): Inline[] {
607
+ const target = HTML_FOLD_TAGS[tag.name];
608
+
609
+ // `<pre>` — the one BLOCK-level fold (see html-fold.ts).
610
+ if (target === "pre") {
611
+ const folded = buildPreBlock(innerItems, ctx, span);
612
+ if (folded !== null) return folded;
613
+ }
614
+
615
+ if (target !== undefined && target !== "break" && target !== "pre") {
616
+ const nested = new Set(active);
617
+ nested.add(target);
618
+ const inner = foldHtmlItems(innerItems, nested, ctx);
619
+ // A LINK cannot nest in markdown, and its href is content — degrading to
620
+ // the children would silently delete the inner URL. Keep the markup as
621
+ // escaped literal text instead.
622
+ if (active.has(target)) {
623
+ return target === "link"
624
+ ? [
625
+ literalTag(tag, span.start, span.start + tag.raw.length),
626
+ ...inner,
627
+ literalTag(closeTag.tag, closeTag.start, closeTag.end),
628
+ ]
629
+ : inner;
630
+ }
631
+ const folded = buildFoldedTag(target, tag, inner, span.start, span.end);
632
+ // A tag we cannot represent faithfully (an `<a>` with no href, a `<code>`
633
+ // wrapping structure) degrades to its CONTENT — nothing is lost.
634
+ return folded !== null ? [folded] : inner;
635
+ }
636
+
637
+ // Unknown but BALANCED: markup dropped, content kept. A block-level name
638
+ // leaves a separator so its neighbours cannot glue together.
639
+ const inner = foldHtmlItems(innerItems, active, ctx);
640
+ if (!isBlockLevelTag(tag.name)) return inner;
641
+ return [
642
+ mkSeparator(ctx, span.start, span.start + tag.raw.length),
643
+ ...inner,
644
+ mkSeparator(ctx, closeTag.start, closeTag.end),
645
+ ];
646
+ }
647
+
648
+ /** Build the fenced-code equivalent of a matched `<pre>…</pre>`, or null when
649
+ * the content is not literal text a fence can carry (in which case the caller
650
+ * falls back to the ordinary degrade). An optional single `<code …>` wrapper
651
+ * is unwrapped, and its `class="language-…"` becomes the fence info string —
652
+ * exactly the shape Telegram's own Rich HTML reference documents. */
653
+ function buildPreBlock(
654
+ innerItems: HtmlFoldItem[],
655
+ ctx: FoldCtx,
656
+ span: Pos,
657
+ ): Inline[] | null {
658
+ let body = innerItems;
659
+ let language: string | null = null;
660
+ const first = body[0];
661
+ const last = body[body.length - 1];
662
+ if (
663
+ body.length >= 2 &&
664
+ first?.kind === "tag" &&
665
+ first.tag.kind === "open" &&
666
+ first.tag.name === "code" &&
667
+ last?.kind === "tag" &&
668
+ last.tag.kind === "close" &&
669
+ last.tag.name === "code"
670
+ ) {
671
+ language = languageOf(first.tag);
672
+ body = body.slice(1, -1);
673
+ }
674
+ let text = "";
675
+ for (const item of body) {
676
+ if (item.kind !== "inline" || item.node.type !== "plain") return null;
677
+ text += item.node.text;
678
+ }
679
+ // `<pre>` content routinely starts on the line after the tag and ends on the
680
+ // line before the close; those layout newlines are not content.
681
+ text = text.replace(/^\r?\n/, "").replace(/[ \t]*\r?\n[ \t]*$/, "");
682
+ // A fence cannot survive inside a heading or a table cell — degrade to an
683
+ // inline code span there, with the newlines flattened.
684
+ if (ctx.noBreaks === true) {
685
+ return [{ type: "code", text: text.replace(/\s*\r?\n\s*/g, " "), ...span }];
686
+ }
687
+ const fence = "```";
688
+ return [
689
+ mkSeparator(ctx, span.start, span.start),
690
+ {
691
+ type: "raw",
692
+ text: `${fence}${language ?? ""}\n${text}\n${fence}`,
693
+ ...span,
694
+ },
695
+ mkSeparator(ctx, span.end, span.end),
696
+ ];
697
+ }
698
+
699
+ function buildFoldedTag(
700
+ target: HtmlFoldTarget,
701
+ tag: HtmlTagInfo,
702
+ children: Inline[],
703
+ start: number,
704
+ end: number,
705
+ ): Inline | null {
706
+ switch (target) {
707
+ case "bold":
708
+ case "italic":
709
+ case "strike":
710
+ // Already exactly that construct (`<b>**already**</b>`) — re-wrapping
711
+ // would emit `****already****`, which reads as literal asterisks.
712
+ if (children.length === 1 && children[0].type === target) return children[0];
713
+ return { type: target, children, start, end };
714
+ case "code": {
715
+ const text = inlineLiteralText(children);
716
+ return text === null ? null : { type: "code", text, start, end };
717
+ }
718
+ case "link": {
719
+ const href = hrefOf(tag);
720
+ return href === null ? null : { type: "link", href, children, start, end };
721
+ }
722
+ default:
723
+ return null;
724
+ }
725
+ }
726
+
727
+ /** Turn a raw HTML string into fold items: tokens stay tokens, the text
728
+ * between them becomes `plain` inlines (with spoiler/highlight recognition,
729
+ * matching how prose is treated everywhere else). */
730
+ function htmlStringToItems(raw: string, base: number, source: string): HtmlFoldItem[] {
731
+ return tokenizeHtml(raw, base).flatMap<HtmlFoldItem>((piece) =>
732
+ piece.kind === "tag"
733
+ ? [{ kind: "tag", tag: piece.tag, start: piece.start, end: piece.end }]
734
+ : expandPlainNode(mkPlain(piece.text, piece.start, piece.end), source).map(
735
+ (node) => ({ kind: "inline", node }) as HtmlFoldItem,
736
+ ),
737
+ );
738
+ }
739
+
293
740
  /** Fold a run of mdast phrasing children into IR inlines, then run the
294
- * post-parse spoiler/highlight recognition over the produced `plain` nodes. */
295
- function buildInlines(children: ReadonlyArray<MdastNode>, source: string): Inline[] {
296
- return children
297
- .map((c) => foldInline(c as PhrasingContent, source))
298
- .flatMap((n) => (n.type === "plain" ? expandPlainNode(n, source) : [n]));
741
+ * post-parse spoiler/highlight recognition over the produced `plain` nodes,
742
+ * then apply the raw-HTML tag policy across the whole run (open/close markers
743
+ * are SIBLING mdast `html` nodes, never a parent, so matching has to happen
744
+ * at this level). */
745
+ function buildInlines(
746
+ children: ReadonlyArray<MdastNode>,
747
+ source: string,
748
+ ctx: FoldCtx,
749
+ ): Inline[] {
750
+ const items: HtmlFoldItem[] = [];
751
+ let sawHtml = false;
752
+ for (const child of children) {
753
+ if ((child as PhrasingContent).type === "html") {
754
+ sawHtml = true;
755
+ const p = pos(child);
756
+ items.push(...htmlStringToItems(slice(source, child), p.start, source));
757
+ continue;
758
+ }
759
+ const folded = foldInline(child as PhrasingContent, source, ctx);
760
+ const expanded = folded.type === "plain" ? expandPlainNode(folded, source) : [folded];
761
+ for (const node of expanded) items.push({ kind: "inline", node });
762
+ }
763
+ if (!sawHtml) return items.map((it) => (it as { node: Inline }).node);
764
+ return foldHtmlItems(items, new Set(), ctx);
299
765
  }
300
766
 
301
- function foldInlineChildren(node: MdastParent, source: string): Inline[] {
302
- return buildInlines(node.children, source);
767
+ function foldInlineChildren(
768
+ node: MdastParent,
769
+ source: string,
770
+ ctx: FoldCtx,
771
+ ): Inline[] {
772
+ return buildInlines(node.children, source, ctx);
303
773
  }
304
774
 
305
775
  function foldTableRow(
306
776
  row: Extract<RootContent, { type: "tableRow" }>,
307
777
  source: string,
778
+ ctx: FoldCtx,
308
779
  ): TableRow {
309
780
  const cells: TableCell[] = row.children.map((cell) => ({
310
- children: buildInlines(cell.children, source),
781
+ // A table cell is one line on the wire — a `<br>` or a structural degrade
782
+ // inside it must not emit a newline (it would end the row).
783
+ children: buildInlines(cell.children, source, { ...ctx, noBreaks: true }),
311
784
  ...pos(cell),
312
785
  }));
313
786
  return { cells, ...pos(row) };
314
787
  }
315
788
 
789
+ /** True for a block that would render to the empty string. Only the raw-HTML
790
+ * fold can produce one — a block that is nothing but markup we dropped
791
+ * (`<!-- comment -->`, `<hr/>`, `<div></div>` on their own line). Left in,
792
+ * each would contribute a spurious `\n\n` separator to the rendered document,
793
+ * so they are filtered out at the point blocks are collected. */
794
+ function isEmptyBlock(block: Block): boolean {
795
+ return block.type === "paragraph" && block.children.length === 0;
796
+ }
797
+
798
+ /** Fold a run of mdast block nodes, dropping any that render to nothing. */
799
+ function foldBlocks(
800
+ nodes: ReadonlyArray<RootContent>,
801
+ source: string,
802
+ expandableLineStarts: Set<number>,
803
+ ctx: FoldCtx,
804
+ ): Block[] {
805
+ return nodes
806
+ .map((n) => foldBlock(n, source, expandableLineStarts, ctx))
807
+ .filter((b) => !isEmptyBlock(b));
808
+ }
809
+
316
810
  function foldBlock(
317
811
  node: RootContent,
318
812
  source: string,
319
813
  expandableLineStarts: Set<number>,
814
+ ctx: FoldCtx,
320
815
  ): Block {
321
816
  switch (node.type) {
322
817
  case "paragraph":
323
- return { type: "paragraph", children: foldInlineChildren(node, source), ...pos(node) };
818
+ return { type: "paragraph", children: foldInlineChildren(node, source, ctx), ...pos(node) };
324
819
  case "heading":
325
820
  return {
326
821
  type: "heading",
327
822
  level: node.depth,
328
- children: foldInlineChildren(node, source),
823
+ // A heading is a single `#…` LINE: an embedded newline would end the
824
+ // block and orphan the rest of the text (`# a<br>b`).
825
+ children: foldInlineChildren(node, source, { ...ctx, noBreaks: true }),
329
826
  ...pos(node),
330
827
  };
331
828
  case "blockquote": {
@@ -335,7 +832,7 @@ function foldBlock(
335
832
  const expandable = expandableLineStarts.has(lineStart(source, p.start));
336
833
  return {
337
834
  type: "blockquote",
338
- children: node.children.map((c) => foldBlock(c, source, expandableLineStarts)),
835
+ children: foldBlocks(node.children, source, expandableLineStarts, ctx),
339
836
  expandable,
340
837
  ...p,
341
838
  };
@@ -349,7 +846,7 @@ function foldBlock(
349
846
  };
350
847
  case "list": {
351
848
  const items: ListItem[] = node.children.map((li) => ({
352
- children: li.children.map((c) => foldBlock(c, source, expandableLineStarts)),
849
+ children: foldBlocks(li.children, source, expandableLineStarts, ctx),
353
850
  checked: li.checked ?? null,
354
851
  // mdast `listItem.spread`: whether this item's block children are
355
852
  // separated by a blank line. Tight (false) keeps a paragraph and its
@@ -374,8 +871,8 @@ function foldBlock(
374
871
  const [headerRow, ...bodyRows] = rows;
375
872
  return {
376
873
  type: "table",
377
- header: foldTableRow(headerRow, source),
378
- rows: bodyRows.map((r) => foldTableRow(r, source)),
874
+ header: foldTableRow(headerRow, source, ctx),
875
+ rows: bodyRows.map((r) => foldTableRow(r, source, ctx)),
379
876
  align: (node.align ?? []).map(foldAlign),
380
877
  ...pos(node),
381
878
  };
@@ -391,7 +888,26 @@ function foldBlock(
391
888
  children: [{ type: "raw", text: slice(source, node), ...pos(node) }],
392
889
  ...pos(node),
393
890
  };
394
- // Not in the palette (html, definition, …): degrade to a paragraph
891
+ // A raw HTML BLOCK (`<details>…`, `<div>…`, a bare `<b>alone</b>` line).
892
+ // mdast hands the whole block over as one opaque string, so tokenize it
893
+ // and run the same three-bucket policy the inline path uses: the
894
+ // wire-verified allowlist passes through raw (which is also what stops
895
+ // `escapeMarkdown` mangling `<details open="x">` into `open\="x"`),
896
+ // markdown-equivalent tags fold, everything else drops its markup and
897
+ // keeps its content.
898
+ case "html": {
899
+ const p = pos(node);
900
+ return {
901
+ type: "paragraph",
902
+ children: foldHtmlItems(
903
+ htmlStringToItems(slice(source, node), p.start, source),
904
+ new Set(),
905
+ ctx,
906
+ ),
907
+ ...p,
908
+ };
909
+ }
910
+ // Not in the palette (definition, …): degrade to a paragraph
395
911
  // carrying the raw source slice so no content is dropped.
396
912
  default:
397
913
  return {
@@ -415,8 +931,33 @@ export function parse(markdown: string): Document {
415
931
  mdastExtensions: [gfmFromMarkdown()],
416
932
  });
417
933
  return {
418
- blocks: tree.children.map((child) =>
419
- foldBlock(child, markdown, expandableLineStarts),
420
- ),
934
+ blocks: foldBlocks(tree.children, markdown, expandableLineStarts, {
935
+ matched: matchedTagOffsets(collectHtmlTokens(tree.children, markdown)),
936
+ }),
937
+ };
938
+ }
939
+
940
+ /** Every HTML token in the tree, in document order, for the balance pass.
941
+ * Walks mdast `html` nodes only — a tag inside a code fence or a code span is
942
+ * not markup and must not balance anything. */
943
+ function collectHtmlTokens(
944
+ nodes: ReadonlyArray<MdastNode>,
945
+ source: string,
946
+ ): { tag: HtmlTagInfo; start: number }[] {
947
+ const out: { tag: HtmlTagInfo; start: number }[] = [];
948
+ const walk = (list: ReadonlyArray<MdastNode>): void => {
949
+ for (const node of list) {
950
+ if (node.type === "html") {
951
+ const p = pos(node);
952
+ for (const piece of tokenizeHtml(slice(source, node), p.start)) {
953
+ if (piece.kind === "tag") out.push({ tag: piece.tag, start: piece.start });
954
+ }
955
+ continue;
956
+ }
957
+ const children = (node as MdastParent).children;
958
+ if (Array.isArray(children)) walk(children);
959
+ }
421
960
  };
961
+ walk(nodes);
962
+ return out;
422
963
  }