html-to-markdown-rs 3.23.5

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
use std::collections::{BTreeMap, HashSet};
use std::sync::OnceLock;

use crate::options::ConversionOptions;

/// Format metadata as YAML frontmatter.
///
/// ~keep The `title` key is present whenever a title element was seen, even an empty one, so
/// ~keep `extract_document_metadata` can tell "no title element" from "an empty one" (#527). An
/// ~keep empty title carries no frontmatter line either way, so it is the one key skipped here.
///
/// Keys and values come from the page, so each is written as one YAML scalar (#544): a
/// newline, `: ` or a leading indicator would otherwise end the line or change what YAML reads.
///
/// `None` when no key writes a line: a page whose only metadata is an empty title gets no
/// frontmatter block.
pub fn format_metadata_frontmatter(metadata: &BTreeMap<String, String>) -> Option<String> {
    const OPEN: &str = "---\n";
    let mut result = String::from(OPEN);
    for (key, value) in metadata {
        if key == "title" && value.is_empty() {
            continue;
        }
        push_yaml_scalar(&mut result, key);
        result.push_str(": ");
        push_yaml_scalar(&mut result, value);
        result.push('\n');
    }
    if result.len() == OPEN.len() {
        return None;
    }
    result.push_str("---\n");
    Some(result)
}

/// Append `value` as a plain YAML scalar when it reads back unchanged, else as a double-quoted
/// scalar with every non-printable character escaped.
fn push_yaml_scalar(out: &mut String, value: &str) {
    if is_plain_yaml_scalar(value) {
        out.push_str(value);
        return;
    }
    out.push('"');
    for ch in value.chars() {
        match ch {
            '"' => out.push_str("\\\""),
            '\\' => out.push_str("\\\\"),
            '\n' => out.push_str("\\n"),
            '\r' => out.push_str("\\r"),
            '\t' => out.push_str("\\t"),
            c if is_yaml_printable(c) => out.push(c),
            c => {
                use std::fmt::Write as _;
                let _ = write!(out, "\\u{:04X}", u32::from(c));
            }
        }
    }
    out.push('"');
}

/// A conservative subset of the YAML plain scalar in block context: no leading indicator, no
/// `: ` or ` #`, no leading or trailing space, only printable characters, and a value a YAML
/// reader resolves to a string (#552).
fn is_plain_yaml_scalar(value: &str) -> bool {
    let Some(first) = value.chars().next() else {
        return false;
    };
    !matches!(
        first,
        '-' | '?'
            | ':'
            | ','
            | '['
            | ']'
            | '{'
            | '}'
            | '#'
            | '&'
            | '*'
            | '!'
            | '|'
            | '>'
            | '\''
            | '"'
            | '%'
            | '@'
            | '`'
    ) && !value.starts_with(' ')
        && !value.ends_with([' ', ':'])
        && !value.contains(": ")
        && !value.contains(" #")
        && value.chars().all(|c| c != '\t' && is_yaml_printable(c))
        && !yaml_non_string_scalar().is_match(value)
}

/// Matches a plain scalar that a YAML reader resolves to null, a boolean, a number or a timestamp:
/// the YAML 1.2 core schema, plus the YAML 1.1 forms that readers such as PyYAML still apply
/// (`yes`/`no`/`on`/`off`, `0b`, leading-zero octal, `_` separators, base 60, dates, `=`, `<<`).
fn yaml_non_string_scalar() -> &'static regex::Regex {
    static RE: OnceLock<regex::Regex> = OnceLock::new();
    RE.get_or_init(|| {
        regex::Regex::new(concat!(
            r"^(?:~|null|Null|NULL",
            r"|true|True|TRUE|false|False|FALSE|yes|Yes|YES|no|No|NO|on|On|ON|off|Off|OFF|y|Y|n|N",
            r"|[-+]?(?:0b[01_]+|0o[0-7]+|0x[0-9a-fA-F_]+",
            r"|[0-9][0-9_]*(?::[0-5]?[0-9])*(?:\.[0-9_]*)?(?:[eE][-+]?[0-9]+)?",
            r"|\.[0-9][0-9_]*(?:[eE][-+]?[0-9]+)?|\.(?:inf|Inf|INF))",
            r"|\.(?:nan|NaN|NAN)",
            r"|[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}",
            r"(?:(?:[Tt]|[ \t]+)[0-9]{1,2}:[0-9]{2}:[0-9]{2}(?:\.[0-9]*)?(?:[ \t]*(?:Z|[-+][0-9]{1,2}(?::[0-9]{2})?))?)?",
            r"|=|<<)$",
        ))
        .expect("YAML scalar type regex is well-formed")
    })
}

/// The YAML 1.2 printable set, minus the Unicode line and paragraph separators and the
/// byte-order mark, which YAML 1.1 readers still treat as a line break or a document marker.
const fn is_yaml_printable(c: char) -> bool {
    matches!(c, '\t' | ' '..='~' | '\u{A0}'..='\u{D7FF}' | '\u{E000}'..='\u{FFFD}' | '\u{10000}'..='\u{10FFFF}')
        && !matches!(c, '\u{2028}' | '\u{2029}' | '\u{FEFF}')
}

/// Record `<meta name>`/`<meta property>` content into `metadata`, honoring `strip_tags`/
/// `preserve_tags` for `"meta"`. The first tag per key wins, comparing keys in any letter case;
/// `seen` holds the lower-cased keys recorded so far.
fn collect_meta_head_metadata(
    child_tag: &crate::dom::HTMLTag,
    options: &ConversionOptions,
    metadata: &mut BTreeMap<String, String>,
    seen: &mut HashSet<String>,
) {
    if !child_tag.name().as_utf8_str().eq_ignore_ascii_case("meta")
        || options.strip_tags.iter().any(|t| t == "meta")
        || options.preserve_tags.iter().any(|t| t == "meta")
    {
        return;
    }

    // ~keep `content` is a user-visible metadata value and carries character references like
    // ~keep any other attribute, so it is decoded before it reaches the frontmatter (#494).
    // ~keep The key (`name`/`property`) is an identifier, not prose, and stays raw.
    if let (Some(name), Some(content)) = (
        child_tag.attributes().get("name").flatten(),
        crate::converter::utility::attributes::decoded_attribute(child_tag, "content"),
    ) {
        let key = format!("meta-{}", name.as_utf8_str());
        if seen.insert(key.to_ascii_lowercase()) {
            metadata.insert(key, content.into_owned());
        }
    }
    if let (Some(property), Some(content)) = (
        child_tag.attributes().get("property").flatten(),
        crate::converter::utility::attributes::decoded_attribute(child_tag, "content"),
    ) {
        let key = format!("meta-{}", property.as_utf8_str());
        if seen.insert(key.to_ascii_lowercase()) {
            metadata.insert(key, content.into_owned());
        }
    }
}

/// Record the trimmed, decoded `<title>` text into `metadata`, honoring `strip_tags`/
/// `preserve_tags` for `"title"`. The first title wins, as in a browser; an empty one still
/// records the `title` key, empty, so the element's presence survives even though its text does
/// not (#527). `seen_title` is whether a title came before.
fn collect_title_head_metadata(
    child_tag: &crate::dom::HTMLTag,
    parser: &crate::dom::Parser,
    options: &ConversionOptions,
    metadata: &mut BTreeMap<String, String>,
    seen_title: &mut bool,
) {
    if !child_tag.name().as_utf8_str().eq_ignore_ascii_case("title") {
        return;
    }
    let later_title = std::mem::replace(seen_title, true);
    if later_title
        || options.strip_tags.iter().any(|t| t == "title")
        || options.preserve_tags.iter().any(|t| t == "title")
    {
        return;
    }

    let mut title_content = String::new();
    let title_children = child_tag.children();
    for title_child in title_children.top() {
        if let Some(crate::dom::Node::Raw(raw)) = title_child.get(parser) {
            title_content.push_str(raw.as_utf8_str().as_ref());
        }
    }
    // ~keep The title is text and carries character references like any other text (#509).
    let title_content = crate::text::decode_html_entities_cow(title_content.trim()).into_owned();
    metadata.insert("title".to_string(), title_content);
}

/// Record the decoded href of the first `<link rel="canonical">` into `metadata`.
fn collect_link_head_metadata(child_tag: &crate::dom::HTMLTag, metadata: &mut BTreeMap<String, String>) {
    if !child_tag.name().as_utf8_str().eq_ignore_ascii_case("link") {
        return;
    }
    let Some(rel_attr) = child_tag.attributes().get("rel").flatten() else {
        return;
    };
    let rel_str = rel_attr.as_utf8_str();
    if !rel_str.contains("canonical") {
        return;
    }
    let Some(href) = crate::converter::utility::attributes::decoded_attribute(child_tag, "href") else {
        return;
    };
    metadata
        .entry("canonical".to_string())
        .or_insert_with(|| href.into_owned());
}

/// Extract metadata from the head element below `roots`, recording `document_base_href` as
/// `base` whether or not the source has a `<head>` tag.
pub fn extract_head_metadata(
    roots: &[crate::dom::NodeHandle],
    parser: &crate::dom::Parser,
    options: &ConversionOptions,
    document_base_href: Option<&str>,
    metadata_end: usize,
) -> BTreeMap<String, String> {
    let mut metadata = head_element_metadata(roots, parser, options, metadata_end);
    if let Some(href) = document_base_href {
        metadata.insert("base".to_string(), href.to_string());
    }
    metadata
}

/// Title, meta and canonical link fields before the body starts, with optional head tags.
fn head_element_metadata(
    roots: &[crate::dom::NodeHandle],
    parser: &crate::dom::Parser,
    options: &ConversionOptions,
    metadata_end: usize,
) -> BTreeMap<String, String> {
    let mut metadata = BTreeMap::new();
    let mut seen_meta = HashSet::new();
    let mut seen_title = false;
    let mut seen_head = false;
    let mut work: Vec<_> = roots.iter().rev().map(|handle| (*handle, false)).collect();
    while let Some((handle, in_head)) = work.pop() {
        match handle.get(parser) {
            Some(crate::dom::Node::Tag(tag)) => {
                if tag.boundaries(parser).0 >= metadata_end {
                    break;
                }
                let name = tag.name().as_bytes();
                if name.eq_ignore_ascii_case(b"head") && std::mem::replace(&mut seen_head, true) {
                    continue;
                }
                if seen_head && name.contains(&b':') {
                    continue;
                }
                if name.eq_ignore_ascii_case(b"html") || name.eq_ignore_ascii_case(b"head") {
                    let first = work.len();
                    let children_in_head = in_head || name.eq_ignore_ascii_case(b"head");
                    work.extend(tag.children().top().iter().map(|child| (*child, children_in_head)));
                    work[first..].reverse();
                } else if starts_body(&name.to_ascii_lowercase()) {
                    break;
                } else {
                    collect_meta_head_metadata(tag, options, &mut metadata, &mut seen_meta);
                    if in_head {
                        collect_title_head_metadata(tag, parser, options, &mut metadata, &mut seen_title);
                    }
                    collect_link_head_metadata(tag, &mut metadata);
                }
            }
            Some(crate::dom::Node::Raw(text)) => {
                let next_is_html = matches!(work.last().and_then(|(next, _)| next.get(parser)),
                    Some(crate::dom::Node::Tag(tag)) if tag.name().as_bytes().eq_ignore_ascii_case(b"html"));
                if !is_ignorable_before_head(&text.as_utf8_str(), next_is_html) {
                    break;
                }
            }
            _ => {}
        }
    }
    if !seen_title {
        if let Some(crate::dom::Node::Tag(tag)) = first_document_title(roots, parser).and_then(|t| t.get(parser)) {
            collect_title_head_metadata(tag, parser, options, &mut metadata, &mut seen_title);
        }
    }
    metadata
}

/// The first `title` element of the body below `roots` in tree order. A browser's
/// `document.title` reads this element when the head has no title, as on pages that stream their
/// metadata after the body has started. The search skips the `title` of a drawing or a formula,
/// the inert content of a template or a `noscript` element, the raw text of a `noembed` or
/// `noframes` element, and a `head` element in the body,
/// whose content the head metadata ignores.
fn first_document_title(
    roots: &[crate::dom::NodeHandle],
    parser: &crate::dom::Parser,
) -> Option<crate::dom::NodeHandle> {
    let mut work: Vec<_> = roots.iter().rev().copied().collect();
    while let Some(handle) = work.pop() {
        let Some(crate::dom::Node::Tag(tag)) = handle.get(parser) else {
            continue;
        };
        let name = tag.name().as_bytes();
        if name.eq_ignore_ascii_case(b"title") {
            return Some(handle);
        }
        if [
            b"head".as_slice(),
            b"svg",
            b"math",
            b"template",
            b"noscript",
            b"noembed",
            b"noframes",
        ]
        .iter()
        .any(|skipped| name.eq_ignore_ascii_case(skipped))
        {
            continue;
        }
        let first = work.len();
        work.extend(tag.children().top().iter().copied());
        work[first..].reverse();
    }
    None
}

/// The first `<head>` element below `roots` before the body starts. A browser's parser ignores
/// a `<head>` tag once the body has started, at text or at a tag that [`starts_body`].
pub fn document_head(roots: &[crate::dom::NodeHandle], parser: &crate::dom::Parser) -> Option<crate::dom::NodeHandle> {
    document_head_search(roots, parser).head
}

/// What [`document_head_search`] finds before the body starts.
pub(super) struct HeadSearch {
    /// The `<head>` element.
    head: Option<crate::dom::NodeHandle>,
    /// True when a browser's parser puts an element in the head that this tree holds outside
    /// one: head content in front of the `<head>` tag, or a `title` before the body that is in
    /// no `<head>` element, with a `<head>` tag on the page or not (issue #816).
    pub(super) content_outside_head: bool,
}

/// Walks from the start of the document to the start of the body. The walk does not stop at
/// the `<head>` element: a `title` between `</head>` and the body is in the head for a browser.
pub(super) fn document_head_search(roots: &[crate::dom::NodeHandle], parser: &crate::dom::Parser) -> HeadSearch {
    let mut head = None;
    let mut content_before_head = false;
    let mut saw_head_content = false;
    let mut saw_title = false;
    let mut misplaced_image = false;
    let mut work: Vec<_> = roots.iter().rev().copied().collect();
    while let Some(handle) = work.pop() {
        match handle.get(parser) {
            Some(crate::dom::Node::Raw(text)) => {
                let next_is_html = matches!(
                    work.last().and_then(|next| next.get(parser)),
                    Some(crate::dom::Node::Tag(tag)) if tag.name().as_bytes().eq_ignore_ascii_case(b"html")
                );
                if !is_ignorable_before_head(&text.as_utf8_str(), next_is_html) {
                    break;
                }
            }
            Some(crate::dom::Node::Tag(tag)) => {
                let name = tag.name().as_bytes().to_ascii_lowercase();
                match name.as_slice() {
                    b"head" => {
                        if head.is_none() {
                            head = Some(handle);
                            content_before_head = saw_head_content;
                            misplaced_image = tag.children().top().iter().any(|child| {
                                matches!(child.get(parser), Some(crate::dom::Node::Tag(child))
                                    if child.name().as_bytes().eq_ignore_ascii_case(b"img"))
                            });
                        }
                    }
                    b"html" => {
                        let first = work.len();
                        work.extend(tag.children().top().iter().copied());
                        work[first..].reverse();
                    }
                    name if starts_body(name) => break,
                    name => {
                        saw_head_content = true;
                        saw_title |= name == b"title";
                    }
                }
            }
            _ => {}
        }
    }
    HeadSearch {
        head,
        content_outside_head: content_before_head || saw_title || misplaced_image,
    }
}

/// Whether a run of text before the head is found should be skipped rather than ending the
/// search: whitespace after character-reference decoding, or any run that sits directly in
/// front of the document's own `<html>`
/// tag. The WHATWG "before html" insertion mode already discards anything ahead of `<html>`
/// itself without letting it block the parser from reaching the real head inside, whether that
/// text is a real byte order mark (stripped earlier, so it never reaches here), one a wrong
/// encoding guess mangled beyond recognition, or ordinary prose: a browser shows the page's
/// title and meta tags either way. Text with nothing named `html` ahead of it, a head-only
/// fragment, still ends the search unchanged; see
/// `should_ignore_a_head_after_implicit_body_content_on_both_tiers`.
pub fn is_ignorable_before_head(text: &str, next_tag_is_html: bool) -> bool {
    next_tag_is_html
        || crate::text::decode_html_entities_cow(text)
            .chars()
            .all(char::is_whitespace)
}

/// Whether a start tag named `name` (lower case) starts the body when no body has started:
/// every tag except the ones the HTML parser's "in head" insertion mode keeps in the head.
pub const fn starts_body(name: &[u8]) -> bool {
    !matches!(
        name,
        b"html"
            | b"head"
            | b"base"
            | b"basefont"
            | b"bgsound"
            | b"link"
            | b"meta"
            | b"noframes"
            | b"noscript"
            | b"script"
            | b"style"
            | b"template"
            | b"title"
    )
}

/// Whether an end tag named `name` (lower case) starts the body when no body has started. The
/// HTML parser's "in head" insertion mode reads these three as the first thing after the head
/// and ignores every other end tag.
pub const fn end_tag_starts_body(name: &[u8]) -> bool {
    matches!(name, b"body" | b"html" | b"br")
}