html-to-markdown-rs 3.23.1

High-performance HTML to Markdown converter using the astral-html tokenizer. Part of the Xberg ecosystem.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
/// Apply `text::backslash_needs_escape` to the run `buffer[from..]`.
///
/// `run_ends_at_last_byte` selects where the run ends, which genuinely differs between
/// the two Tier-2 call sites this mirrors:
///
/// - Prose (`text_node.rs`'s normalized branch) escapes `chomp()`'s *core*, the text
///   node with its boundary whitespace stripped — so a trailing `\` counts as
///   end-of-run even when spaces or a newline follow it in the emitted bytes. Pass
///   `false`.
/// - A table cell (`text_node.rs`'s `in_table_cell` branch) escapes the whole
///   normalized text node with no chomp, so the run ends at the last byte whatever it
///   is. Pass `true`.
///
/// Getting this backwards flips `<p>a\ </p>` or `<td>a\ </td>` by one byte against
/// Tier-2. ~keep
fn escape_backslash_run(buffer: &mut String, from: usize, run_ends_at_last_byte: bool) {
    if memchr::memchr(b'\\', &buffer.as_bytes()[from..]).is_none() {
        return;
    }
    let run = buffer[from..].to_owned();
    let run_end = if run_ends_at_last_byte {
        run.len()
    } else {
        run.trim_end().len()
    };
    let bytes = &run.as_bytes()[..run_end];

    let mut rewritten = String::with_capacity(run.len() + 4);
    let mut copied_to = 0usize;
    for i in memchr::memchr_iter(b'\\', bytes) {
        if crate::text::backslash_needs_escape(bytes, i) {
            rewritten.push_str(&run[copied_to..i]);
            rewritten.push_str(r"\\");
            copied_to = i + 1;
        }
    }
    rewritten.push_str(&run[copied_to..]);

    buffer.truncate(from);
    buffer.push_str(&rewritten);
}

/// Decode HTML entities directly into `out` (no intermediate allocation).
///
/// `base_offset` is the byte offset of `s` within the original HTML input and
/// is used to report the position of any unrecognised entity in the bail reason.
///
/// Uses memchr to quickly find the next `&` and bulk-copies non-entity runs.
///
/// Returns `Err(BailReason::UnknownEntity)` when an entity cannot be decoded.
fn decode_entities_into(
    out: &mut String,
    s: &str,
    base_offset: usize,
    context: ReferenceContext,
) -> Result<(), BailReason> {
    let bytes = s.as_bytes();
    let mut i = 0;
    while i < bytes.len() {
        if let Some(pos) = memchr::memchr(b'&', &bytes[i..]) {
            let amp_pos = i + pos;
            if amp_pos > i {
                out.push_str(&s[i..amp_pos]);
            }
            i = decode_entity_at(bytes, s, amp_pos, out, base_offset, context)?;
        } else {
            if i < bytes.len() {
                out.push_str(&s[i..]);
            }
            break;
        }
    }
    Ok(())
}

/// Decode entities AND collapse spaces/tabs in one pass, directly into `out`.
///
/// `base_offset` is the byte offset of `s` within the original HTML input and
/// is used to report the position of any unrecognised entity in the bail reason.
///
/// Uses memchr3 to quickly find the next special byte (space/tab/&), then
/// bulk-copies the run in one `push_str` to avoid per-byte overhead.
///
/// Returns `Err(BailReason::UnknownEntity)` when an entity cannot be decoded.
fn decode_and_collapse_into(
    out: &mut String,
    s: &str,
    has_entities: bool,
    base_offset: usize,
) -> Result<(), BailReason> {
    decode_and_collapse_into_inner(out, s, has_entities, base_offset, false)
}

/// Collapse like `decode_and_collapse_into` but treat `\n`/`\r` as collapsible
/// whitespace too.  Used for text inside `<a>`/`<strong>`/`<em>` frames where
/// Tier-2's `normalize_link_label` first replaces newlines with spaces, then
/// runs whitespace normalization.
fn decode_and_collapse_into_inline(
    out: &mut String,
    s: &str,
    has_entities: bool,
    base_offset: usize,
) -> Result<(), BailReason> {
    decode_and_collapse_into_inner(out, s, has_entities, base_offset, true)
}

fn decode_and_collapse_into_inner(
    out: &mut String,
    s: &str,
    has_entities: bool,
    base_offset: usize,
    collapse_newlines: bool,
) -> Result<(), BailReason> {
    let bytes = s.as_bytes();
    let mut i = 0;
    // ~keep Rule 4 of the module: white space at the start of the text is the run that `out`
    // ~keep ends with, so after a space or a line end it writes nothing. `<td>one <span>two&#10;
    // ~keep </span> three</td>` is `one two three`, as the full converter writes it.
    let mut prev_was_space = !crate::converter::utility::white_space::space_is_owed(out);
    // ~keep Mirrors Tier-2's `normalize_block_whitespace_cow` (text.rs): when a literal
    // `\n` survives into the output (only possible here when `collapse_newlines`
    // is false -- the `true` variant folds `\n` straight into a space below and
    // never leaves one in `out`), a run of spaces/tabs immediately after it is a
    // Markdown continuation line's leading indentation. A compliant parser drops
    // that entirely on reparse regardless of width (CommonMark spec 4.9), so
    // collapsing it to one space here was not a fixed point -- see the CommonMark
    // spec fixpoint oracle, example 182. Zero is. `s` has already had its own
    // leading/trailing whitespace resolved into a synthetic prefix/suffix by the
    // caller's Phase Y step, so every `\n` this sees with more content after it is
    // a genuine mid-text line break, never the text node's own edge.
    let mut at_line_start = false;
    while i < bytes.len() {
        let Some(pos) = next_special_byte(bytes, i, has_entities, collapse_newlines) else {
            out.push_str(&s[i..]);
            break;
        };
        if pos > i {
            out.push_str(&s[i..pos]);
            prev_was_space = false;
            at_line_start = !collapse_newlines && bytes[pos - 1] == b'\n';
        }
        // ~keep A character reference that decodes to a space, a tab or a line end (`&#10;`,
        // ~keep `&#32;`, `&Tab;`) is that white space: the module decides on the decoded text.
        // ~keep `<td><span>one&#10;</span><span>&#10;two</span></td>` is `one two`, as the full
        // ~keep converter writes it.
        let (special, next) = match bytes[pos] {
            b'&' => match crate::text::decode_character_reference(s, pos, ReferenceContext::Text) {
                Some((end, decoded @ (' ' | '\t' | '\n'), None))
                    if bytes[end - 1] == b';' && (decoded != '\n' || collapse_newlines) =>
                {
                    (decoded as u8, end)
                }
                _ => (b'&', pos),
            },
            byte => (byte, pos + 1),
        };
        match special {
            b' ' | b'\t' if at_line_start => {}
            b' ' | b'\t' | b'\n' => {
                if !prev_was_space {
                    out.push(' ');
                }
                prev_was_space = true;
            }
            b'&' => {
                prev_was_space = false;
                at_line_start = false;
                i = decode_entity_at(bytes, s, pos, out, base_offset, ReferenceContext::Text)?;
                continue;
            }
            _ => unreachable!(),
        }
        i = next;
    }
    Ok(())
}

/// The position of the next byte of `bytes` from `from` that the collapse decides on: a space,
/// a tab, a line end when `collapse_newlines`, and `&` when `has_entities`.
fn next_special_byte(bytes: &[u8], from: usize, has_entities: bool, collapse_newlines: bool) -> Option<usize> {
    let rest = &bytes[from..];
    let pos = match (has_entities, collapse_newlines) {
        (true, true) => {
            let space = memchr3(b' ', b'\t', b'\n', rest);
            let entity = memchr::memchr(b'&', rest);
            match (space, entity) {
                (Some(a), Some(b)) => Some(a.min(b)),
                (Some(a), None) | (None, Some(a)) => Some(a),
                (None, None) => None,
            }
        }
        (true, false) => memchr3(b' ', b'\t', b'&', rest),
        (false, true) => memchr3(b' ', b'\t', b'\n', rest),
        (false, false) => memchr::memchr2(b' ', b'\t', rest),
    };
    pos.map(|pos| from + pos)
}

/// `raw` without its leading whitespace, where a character reference that decodes to whitespace
/// (`&#10;`, `&#32;`, `&Tab;`) counts as whitespace too.
///
/// ~keep Tier-2 decodes a text node before it trims the node's leading whitespace, so a newline
/// ~keep written as `&#10;` after a hard break is dropped there; trimmed before decoding, it
/// ~keep survived here as a second line end and made a blank line.
fn trim_leading_text_whitespace(raw: &str) -> &str {
    const WHITESPACE: [char; 4] = [' ', '\t', '\n', '\r'];
    let mut rest = raw.trim_start_matches(WHITESPACE);
    while rest.starts_with('&') {
        match crate::text::decode_character_reference(rest, 0, ReferenceContext::Text) {
            Some((end, first, None)) if rest.as_bytes()[end - 1] == b';' && WHITESPACE.contains(&first) => {
                rest = rest[end..].trim_start_matches(WHITESPACE);
            }
            _ => break,
        }
    }
    rest
}

/// Whether the white space that the source text `raw` starts with holds a Unicode space, as a
/// character or as a character reference (`&nbsp;`, `&#160;`, `&#xA0;`).
///
/// ~keep The source form of `white_space::starts_with_unicode_space`, which reads decoded text.
fn source_starts_with_unicode_space(raw: &str) -> bool {
    let mut rest = raw;
    loop {
        let Some(first) = rest.chars().next() else {
            return false;
        };
        let (character, end) = if first == '&' {
            match crate::text::decode_character_reference(rest, 0, ReferenceContext::Text) {
                Some((end, decoded, None)) if rest.as_bytes()[end - 1] == b';' => (decoded, end),
                _ => return false,
            }
        } else {
            (first, first.len_utf8())
        };
        if !character.is_whitespace() {
            return false;
        }
        if crate::text::is_unicode_space(character) {
            return true;
        }
        rest = &rest[end..];
    }
}

/// Hand the page to Tier-2 at a text that starts with a Unicode space after a written space,
/// unless the tag before the text tells that the space joins the written one.
///
/// ~keep Tier-2 writes that space (`a <span></span>&nbsp;b` is `a  b`) by what stands before
/// ~keep the text in the tree. The scanner knows that only for a text that starts where an
/// ~keep opening tag ends, or the closing tag of an element that holds text and is no `span`
/// ~keep (`emit_close`): there the two converters join the spaces, and the scanner answers.
/// ~keep Any other text has an unknown place, and Tier-2 takes the page: a text after a `<br>`
/// ~keep in a table cell is one, and Tier-2 keeps the Unicode space there. A text of only white
/// ~keep space after a closing tag is another (`is_blank_after_text`).
/// ~keep In `pre` and in code the white space rule does not apply, and nothing is handed over.
/// ~keep A heading has one line. Tier-2 writes a `<br>` there as a space, and this scanner writes
/// ~keep a hard break that the close of the heading folds to a space: that hard break is a
/// ~keep written space here (`<h2>a<br><span></span>&nbsp;b</h2>` is `## a  b`). The text right
/// ~keep after the `<br>` has a known place and joins.
fn hand_over_unicode_space_after_space(
    state: &mut Tier1State,
    raw: &str,
    base_offset: usize,
) -> Result<(), BailReason> {
    if state.escape_ctx.intersects(EscapeCtx::PRE | EscapeCtx::CODE) || !source_starts_with_unicode_space(raw) {
        return Ok(());
    }
    let in_label = state.link_stack.iter().any(|(href, ..)| href.is_some());
    let in_empty_address_link = state.link_stack.iter().any(|(href, ..)| href.as_deref() == Some(""));
    let place = state
        .text_place
        .and_then(|(offset, place)| (offset == base_offset).then_some(place));
    let in_heading = state.escape_ctx.contains(EscapeCtx::HEADING);
    let in_cell = state.in_table_cell();
    let written = state.cell_or_output_mut();
    let after_heading_break = in_heading && (written.ends_with("  \n") || written.ends_with("\\\n"));
    // ~keep A text with a word that starts a line after a hard break keeps its Unicode spaces as
    // ~keep the indent of that line in Tier-2 (#839). This scanner has no such rule, in the label
    // ~keep of a link either, and Tier-2 takes the page.
    // ~keep A link with an empty address is running text in Tier-2: a `<br>` in it is a break of
    // ~keep the block there, and this scanner writes the break of a label. What a text of only
    // ~keep Unicode spaces writes after that break is decided in Tier-2 (issue #845).
    let after_hard_break = !in_heading
        && !in_cell
        && (written.ends_with("  \n") || written.ends_with("\\\n"))
        && (in_empty_address_link
            || !crate::converter::utility::white_space::is_blank(crate::text::decode_html_entities_cow(raw).as_ref()));
    if after_hard_break {
        return Err(BailReason::UnicodeSpaceAfterSpace);
    }
    // ~keep In the label of a link every run of white space is one space in both converters
    // ~keep (`normalize_link_label`), and a Unicode space at a label edge is the one space of that
    // ~keep edge: the place of the text decides nothing there. A link with no address is running
    // ~keep text, not a label.
    if in_label {
        return Ok(());
    }
    if (written.ends_with(' ') || after_heading_break)
        && place.is_none_or(|place| unicode_space_is_kept(written, place) || is_blank_after_text(place, raw))
    {
        return Err(BailReason::UnicodeSpaceAfterSpace);
    }
    Ok(())
}

/// Whether the source text `raw` stands after text and holds no word: every character of it is
/// white space, a Unicode space included.
///
/// ~keep The place after text (`emit_close`) answers for a text that holds a word: the Unicode
/// ~keep space that `</a>&nbsp;now` starts with joins the written space in both converters. A
/// ~keep text of only white space is no word to join: Tier-2 writes an em space (U+2003)
/// ~keep between `<b>Go </b>` and `<i>now</i>` with no space before it, by rules this scanner
/// ~keep does not have. The place of such a text counts as unknown, and Tier-2 takes the page.
fn is_blank_after_text(place: TextPlace, raw: &str) -> bool {
    place == TextPlace::AfterText
        && crate::converter::utility::white_space::is_blank(&crate::text::decode_html_entities_cow(raw))
}

/// Scan and decode a single HTML entity starting at `amp_pos` (the `&` byte).
///
/// Tries the hot subset in `decode_entity_into` for an alphanumeric name closed by `;`; every
/// other reference goes through Tier-2's decoder, `text::decode_character_reference`, so both
/// tiers read one table. When nothing decodes, writes the `&` alone and resumes at the next byte,
/// as Tier-2 does, so a reference after an unknown name still decodes (#554).
///
/// Returns the position immediately after the reference, or after the `&`.
///
/// Emits `Err(BailReason::UnknownEntity)` when Tier-2's decoder would decode a
/// reference this function does not: one without its `;`.
fn decode_entity_at(
    bytes: &[u8],
    s: &str,
    amp_pos: usize,
    out: &mut String,
    base_offset: usize,
    context: ReferenceContext,
) -> Result<usize, BailReason> {
    let amp = amp_pos;
    let name_len = bytes[amp + 1..]
        .iter()
        .take_while(|b| b.is_ascii_alphanumeric())
        .count();
    let name_end = amp + 1 + name_len;
    if bytes.get(name_end) == Some(&b';') && decode_entity_into(out, &s[amp + 1..name_end]) {
        return Ok(name_end + 1);
    }
    match crate::text::decode_character_reference(s, amp, context) {
        Some((reference_end, first, second)) if bytes[reference_end - 1] == b';' => {
            out.push(first);
            if let Some(second) = second {
                out.push(second);
            }
            Ok(reference_end)
        }
        // ~keep A legacy named reference (#545) or a numeric one (#553) without its `;` is
        // ~keep rare; Tier-2 owns the longest-name rule and the attribute exception.
        Some((reference_end, ..)) => Err(BailReason::UnknownEntity {
            name: s[amp + 1..reference_end].into(),
            offset: base_offset + amp,
        }),
        None => {
            out.push('&');
            Ok(amp + 1)
        }
    }
}

/// Apply the escape-context bits for an opening tag.
///
/// The close path restores `state.escape_ctx` directly from `frame.prev_escape_ctx`
/// so a symmetric `remove_open_escape_ctx` is not needed.
#[inline]
fn apply_open_escape_ctx(state: &mut Tier1State, spec: &TagSpec) {
    if spec.kind == TagKind::Pre {
        state.escape_ctx |= EscapeCtx::PRE | EscapeCtx::CODE;
        return;
    }

    let bit = match spec.kind {
        TagKind::Code => EscapeCtx::CODE,
        TagKind::Link => EscapeCtx::LINK,
        TagKind::Blockquote => EscapeCtx::BLOCKQUOTE,
        TagKind::Heading(_) => EscapeCtx::HEADING,
        TagKind::Strong => EscapeCtx::STRONG,
        _ => return,
    };

    state.escape_ctx |= bit;
}

/// Report whether an attribute is present, with or without a value.
///
/// [`find_attr`] returns the attribute's *value* and so cannot tell an absent
/// attribute from a valueless one (`<td colspan>`).  Tier-2's spanning-cell test
/// (`block/table/scanner.rs`: `attrs.get("colspan").is_some()`) keys off presence
/// alone, so mirroring it needs this distinction.
fn has_attr(attrs: &[(&[u8], Option<&[u8]>)], key: &[u8]) -> bool {
    attrs.iter().any(|(k, _)| k.eq_ignore_ascii_case(key))
}

/// Find an attribute value by (lowercase) key name.
fn find_attr<'a>(attrs: &[(&'a [u8], Option<&'a [u8]>)], key: &[u8]) -> Option<&'a [u8]> {
    for (k, v) in attrs {
        if k.eq_ignore_ascii_case(key) {
            return *v;
        }
    }
    None
}

/// Returns true when `name_lower` is a tag that *may* need preprocessing-skip
/// evaluation.  All other tags skip the more expensive `should_skip_preprocessing`
/// check entirely.
const fn is_preprocessing_skip_candidate(name_lower: &[u8]) -> bool {
    matches!(name_lower, b"nav" | b"header" | b"footer" | b"aside" | b"form")
}

/// Mirrors `should_drop_for_preprocessing` (preprocessing_helpers.rs) for
/// the Tier-1 byte scanner.
///
/// Called only for tags that passed [`is_preprocessing_skip_candidate`].
/// Uses the raw attribute byte slices collected by [`parse::collect_attrs`]
/// instead of the Tier-2 `dom::HTMLTag` DOM node.
fn should_skip_preprocessing(
    name_lower: &[u8],
    attrs: &[(&[u8], Option<&[u8]>)],
    options: &ConversionOptions,
    is_page_header: bool,
) -> bool {
    use crate::options::PreprocessingPreset;

    if !options.preprocessing.enabled {
        return false;
    }

    if options.preprocessing.preset == PreprocessingPreset::Minimal {
        return false;
    }

    if options.preprocessing.remove_forms && name_lower == b"form" {
        return true;
    }

    if !options.preprocessing.remove_navigation {
        return false;
    }

    if name_lower == b"nav" {
        return true;
    }

    // ~keep <header> / <footer> / <aside> — drop only when navigation hints present.
    // (Aggressive would drop footer/aside unconditionally, but Aggressive routes
    // through Tier-2 via the existing router gate so Tier-1 only needs the
    // Standard-preset behaviour: nav-hint check.)
    if name_lower == b"header" && is_page_header {
        return true;
    }

    if matches!(name_lower, b"header" | b"footer" | b"aside") {
        return byte_attrs_have_navigation_hint(attrs);
    }

    false
}

fn header_is_page_level(state: &Tier1State, html: &str) -> bool {
    let mut in_document_body = false;
    for frame in &state.stack {
        let Some(name) = html.as_bytes().get(frame.name_range.clone()) else {
            continue;
        };
        if matches_ignore_ascii_case(name, &[b"article", b"section", b"main"]) {
            return false;
        }
        in_document_body |= name.eq_ignore_ascii_case(b"body");
    }
    in_document_body
}

fn matches_ignore_ascii_case(value: &[u8], candidates: &[&[u8]]) -> bool {
    candidates.iter().any(|candidate| value.eq_ignore_ascii_case(candidate))
}

/// Byte-level equivalent of `element_has_navigation_hint` for use in the
/// Tier-1 scanner where attributes are raw `&[u8]` slices rather than a
/// parsed `dom::HTMLTag`.
fn byte_attrs_have_navigation_hint(attrs: &[(&[u8], Option<&[u8]>)]) -> bool {
    if let Some(role) = find_attr(attrs, b"role") {
        let role_lc = role.to_ascii_lowercase();
        if matches!(role_lc.as_slice(), b"navigation" | b"menubar" | b"tablist" | b"toolbar") {
            return true;
        }
    }

    if let Some(label) = find_attr(attrs, b"aria-label") {
        let label_lc = label.to_ascii_lowercase();
        const ARIA_SUBSTRINGS: &[&[u8]] = &[b"navigation", b"menu", b"contents", b"table of contents", b"toc"];
        if ARIA_SUBSTRINGS
            .iter()
            .any(|sub| label_lc.windows(sub.len()).any(|w| w == *sub))
        {
            return true;
        }
    }

    for attr_name in [b"class".as_slice(), b"id".as_slice()] {
        if let Some(value) = find_attr(attrs, attr_name) {
            if byte_value_has_nav_keyword(value) {
                return true;
            }
        }
    }

    false
}

/// Tokenize a raw attribute byte value and return true when any token matches
/// a keyword in [`NAV_KEYWORDS`].
///
/// Tokens are split on ASCII whitespace.  Each token is normalised by
/// replacing `_`, `:`, `.`, `/` with `-` and lowercasing before comparison.
fn byte_value_has_nav_keyword(value: &[u8]) -> bool {
    let mut start = 0;
    let len = value.len();
    loop {
        while start < len && value[start].is_ascii_whitespace() {
            start += 1;
        }
        if start >= len {
            break;
        }
        let mut end = start;
        while end < len && !value[end].is_ascii_whitespace() {
            end += 1;
        }
        let token_bytes = &value[start..end];
        let mut buf = [0u8; 64];
        let normalised: &[u8] = if token_bytes.len() <= buf.len() {
            let n = token_bytes.len();
            for (i, &b) in token_bytes.iter().enumerate() {
                buf[i] = match b {
                    b'_' | b':' | b'.' | b'/' => b'-',
                    _ => b.to_ascii_lowercase(),
                };
            }
            &buf[..n]
        } else {
            start = end;
            continue;
        };

        if NAV_KEYWORDS.iter().any(|kw| kw.as_bytes() == normalised) {
            return true;
        }

        start = end;
    }
    false
}

/// Extract `href` and `title` from the attribute list for a link.
fn extract_link_attrs(attrs: &[(&[u8], Option<&[u8]>)]) -> Result<(Option<String>, Option<String>), BailReason> {
    let href = find_attr(attrs, b"href").map(decode_attr).transpose()?;
    // ~keep Full decode, matching Tier-2's `decoded_attribute` (issue #494). This used to be
    // a deliberate half-decode that mirrored Tier-2 leaving named entities intact, so a
    // `title="A&amp;B"` reached the output as the literal text `A&amp;B` in both tiers.
    let title = find_attr(attrs, b"title").map(decode_attr).transpose()?;
    Ok((href, title))
}

/// Extract `start` attribute from `<ol>` (defaults to 1).
fn extract_ol_start(attrs: &[(&[u8], Option<&[u8]>)]) -> u16 {
    find_attr(attrs, b"start")
        .and_then(|b| std::str::from_utf8(b).ok())
        .and_then(|s| s.parse::<u16>().ok())
        .unwrap_or(1)
}

/// Decode an attribute value: entity-decode and convert to a String.
///
/// Returns `Err(BailReason::Classifier)` when the value is not valid UTF-8
/// (malformed bytes in attributes cannot be decoded faithfully).
/// Returns `Err(BailReason::UnknownEntity)` when the value contains an entity
/// that Tier-1 cannot decode (Tier-2 would decode it differently).
fn decode_attr(bytes: &[u8]) -> Result<String, BailReason> {
    let s = std::str::from_utf8(bytes).map_err(|_| BailReason::Classifier)?;
    if !s.contains('&') {
        return Ok(s.to_owned());
    }
    let mut out = String::with_capacity(s.len());
    decode_entities_into(&mut out, s, 0, ReferenceContext::Attribute)?;
    Ok(out)
}

/// Pop the topmost frame whose spec matches `spec`.
/// Tier-2 is lenient about close tags; we are strict in M3c: only pop the
/// Pop the topmost frame whose spec matches `spec`.
///
/// We compare by checking if the `TagKind` on the top frame maps to the same
/// "semantic group" as the spec being closed.  We are strict in M3c: only the
/// top frame is checked to avoid mismatched-close-tag complexity.
fn pop_matching_frame(stack: &mut Vec<OpenTag>, spec: &'static TagSpec) -> Option<OpenTag> {
    let top = stack.last()?;
    if kinds_match(&top.spec.kind, &spec.kind) {
        stack.pop()
    } else {
        None
    }
}

/// Return `true` if two `TagKind` values are the "same" for close-tag matching.
///
/// Uses pointer equality on the `&'static TagSpec` where possible for speed.
/// For kinds with inner data (`List`, `Heading`, `TableCell`) we use a
/// coarser match that still prevents cross-kind confusion:
/// - `List(Ordered)` only matches `List(Ordered)`, etc.
/// - `Heading(n)` matches `Heading(m)` for any n, m (HTML allows `</h3>` to
///   close `<h2>` in some parsers; we are lenient for headings since they
///   do not nest in practice).
fn kinds_match(a: &TagKind, b: &TagKind) -> bool {
    match (a, b) {
        (TagKind::List(la), TagKind::List(lb)) => la == lb,
        (TagKind::Heading(_), TagKind::Heading(_)) => true,
        (TagKind::TableCell { is_header: a_h }, TagKind::TableCell { is_header: b_h }) => a_h == b_h,
        _ => std::mem::discriminant(a) == std::mem::discriminant(b),
    }
}

/// Find the nearest enclosing list kind by walking the stack top-to-bottom.
fn find_parent_list_kind(stack: &[OpenTag]) -> Option<ListKind> {
    for frame in stack.iter().rev() {
        if let TagKind::List(kind) = frame.spec.kind {
            return Some(kind);
        }
    }
    None
}

/// Increment the ordered-list counter on the nearest `List(Ordered)` frame.
/// Returns the new counter value (1-based).
fn increment_ol_counter(stack: &mut [OpenTag]) -> u16 {
    for frame in stack.iter_mut().rev() {
        if frame.spec.kind == TagKind::List(ListKind::Ordered) {
            frame.list_index = frame.list_index.saturating_add(1);
            return frame.list_index;
        }
    }
    1
}

/// Get the `ol_start` value from the nearest `List(Ordered)` frame.
fn find_ol_start(stack: &[OpenTag]) -> u16 {
    for frame in stack.iter().rev() {
        if frame.spec.kind == TagKind::List(ListKind::Ordered) {
            return frame.ol_start;
        }
    }
    1
}

/// Return the ATX heading prefix for level `n` (1–6).
///
/// Uses the `HEADING_PREFIXES` table — no allocation.
fn heading_prefix(n: u8) -> &'static str {
    let idx = (n as usize).saturating_sub(1).min(5);
    HEADING_PREFIXES[idx]
}

/// Push the list-item indentation for `depth` into `out`.
///
/// Depth 0 → no indent; each level adds two spaces (matches the router's
/// `list_indent_width == 2` gate).  Depths 0–7 use the static `LIST_ITEM_INDENTS`
/// table; deeper nesting (rare) falls back to a runtime loop.
fn push_list_item_indent(out: &mut String, depth: u16) {
    let idx = depth as usize;
    if idx < LIST_ITEM_INDENTS.len() {
        out.push_str(LIST_ITEM_INDENTS[idx]);
    } else {
        out.reserve(idx * 2);
        for _ in 0..idx {
            out.push_str("  ");
        }
    }
}

/// Add `> ` prefix to every non-empty line of `content`, and `>` to empty
/// lines that are between non-empty ones (Tier-2 behaviour for multi-paragraph
/// blockquotes).
fn prefix_blockquote_lines(content: &str) -> String {
    let content = content.trim_end_matches('\n');
    if content.is_empty() {
        return String::new();
    }

    let lines: Vec<&str> = content.split('\n').collect();
    let mut result = String::with_capacity(content.len() + lines.len() * 2);

    for (i, line) in lines.iter().enumerate() {
        if line.is_empty() {
            result.push('>');
        } else {
            result.push_str("> ");
            result.push_str(line);
        }
        if i < lines.len() - 1 {
            result.push('\n');
        }
    }
    result.push('\n');
    result
}

// ~keep ── GFM table emission ────────────────────────────────────────────────────────