html-to-markdown-rs 3.20.0

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
//! Plain text extraction from parsed HTML DOM.
//!
//! Provides a fast-path text extractor that walks the DOM tree collecting only
//! visible text content with structural whitespace, bypassing the full
//! Markdown/Djot conversion pipeline.

mod output;
#[cfg(test)]
mod stack_tests;
mod walker;

use std::collections::HashSet;

use crate::options::ConversionOptions;
use crate::text;
use output::PlainOutput;

#[cfg(feature = "visitor")]
use crate::converter::utility::content::is_block_level_element;
#[cfg(feature = "visitor")]
use crate::visitor::EMPTY_ATTRS;
#[cfg(feature = "visitor")]
use crate::visitor::{NodeContext, NodeType, VisitResult, VisitorHandle};
#[cfg(feature = "visitor")]
use output::Checkpoint;
#[cfg(feature = "visitor")]
use std::borrow::Cow;

/// Tracks list context for proper marker emission on `<li>` elements.
#[derive(Clone, Copy, Debug)]
enum ListContext {
    /// Not inside any list.
    None,
    /// Inside `<ul>` — each `<li>` gets a `- ` prefix.
    Unordered,
    /// Inside `<ol>` — each `<li>` gets a sequential `N. ` prefix.
    /// The `next_index` is incremented after each `<li>`.
    Ordered { next_index: u32 },
}

/// Tags whose content should be skipped entirely.
const SKIP_TAGS: &[&str] = &["script", "style", "head", "template", "noscript", "math"];

/// Block-level tags that should be separated by blank lines.
const BLOCK_TAGS: &[&str] = &[
    "p",
    "div",
    "h1",
    "h2",
    "h3",
    "h4",
    "h5",
    "h6",
    "blockquote",
    "section",
    "article",
    "aside",
    "main",
    "nav",
    "header",
    "footer",
    "figure",
    "figcaption",
    "details",
    "summary",
    "address",
    "hgroup",
    "search",
    "center",
    "dialog",
    "menu",
    "legend",
    // ~keep The terms and the descriptions are the blocks of a definition list; the list
    // ~keep itself writes nothing of its own, so an empty list stays out of the line.
    "dt",
    "dd",
];

/// State carried by each scheduled DOM action.
///
/// Holds the options, visitor (feature-gated), and current DOM depth.
/// Using a struct avoids feature-gated function parameters at call sites.
#[derive(Clone, Copy)]
struct WalkState<'a> {
    options: &'a ConversionOptions,
    accessible_names: crate::convert_api::AccessibleNamePolicy,
    excluded_node_ids: &'a HashSet<u32>,
    /// Inside a list item, the item's buffer length right after its marker, so a block that
    /// follows the marker directly starts on its line. A table cell starts its own buffer with `None`.
    item_marker_end: Option<usize>,
    in_pre: bool,
    /// Inside the element that writes an option list: the outermost `<select>`, `<datalist>`
    /// or `<option>`. An option there is a word of that list.
    in_option_list: bool,
    in_document_body: bool,
    in_content_header_scope: bool,
    depth: usize,
    #[cfg(feature = "visitor")]
    visitor: Option<&'a VisitorHandle>,
}

impl WalkState<'_> {
    const fn descend(&self) -> Self {
        WalkState {
            options: self.options,
            accessible_names: self.accessible_names,
            excluded_node_ids: self.excluded_node_ids,
            item_marker_end: self.item_marker_end,
            in_pre: self.in_pre,
            in_option_list: self.in_option_list,
            in_document_body: self.in_document_body,
            in_content_header_scope: self.in_content_header_scope,
            depth: self.depth + 1,
            #[cfg(feature = "visitor")]
            visitor: self.visitor,
        }
    }

    /// The state for text that a control writes to a buffer of its own. The marker end of a
    /// list item is a length of the item's buffer, so this state holds none.
    const fn for_own_buffer(&self) -> Self {
        WalkState {
            item_marker_end: None,
            ..*self
        }
    }

    /// Whether a selector of `exclude_selectors` names the node.
    fn excludes(&self, node_handle: &crate::dom::NodeHandle) -> bool {
        self.excluded_node_ids.contains(&node_handle.get_inner())
    }
}

/// Extract plain text from a parsed DOM tree.
///
/// Walks the tree collecting visible text with structural whitespace:
/// - Block elements get blank-line separation
/// - `<br>` becomes a newline, `<hr>` a blank line
/// - `<pre>` preserves internal whitespace
/// - `<img>` outputs alt text, and an inline `<svg>` its text (unless `skip_images` is set)
/// - `<script>`, `<style>`, `<head>`, `<template>`, `<noscript>` are skipped
/// - Tables: cells separated by tab, rows by newline
/// - Inline elements are recursed without markers
/// - A form control that writes text is a word of its own, and so is each option of a list
/// - Nodes matching `excluded_node_ids` (from `exclude_selectors`) are dropped entirely
/// - When a visitor is configured, `visit_element_start`, `visit_element_end`, and
///   `visit_text` callbacks are fired and their results are honoured.
pub fn extract_plain_text(
    dom: &crate::dom::VDom,
    parser: &crate::dom::Parser,
    options: &ConversionOptions,
    accessible_names: crate::convert_api::AccessibleNamePolicy,
) -> String {
    let excluded_node_ids: HashSet<u32> = if options.exclude_selectors.is_empty() {
        HashSet::new()
    } else {
        let mut ids = HashSet::new();
        for selector in &options.exclude_selectors {
            if let Some(iter) = dom.query_selector(selector) {
                for handle in iter {
                    ids.insert(handle.get_inner());
                }
            }
        }
        ids
    };

    let state = WalkState {
        options,
        accessible_names,
        excluded_node_ids: &excluded_node_ids,
        item_marker_end: None,
        in_pre: false,
        in_option_list: false,
        in_document_body: false,
        in_content_header_scope: false,
        depth: 0,
        #[cfg(feature = "visitor")]
        visitor: options.visitor.as_ref(),
    };

    let mut text = walker::extract(dom.children(), parser, state);
    post_process(&mut text);
    text
}

/// Writes a text node. `after_passed_over` says whether the walker passed over the node right
/// before the text.
fn emit_raw_text(bytes: &crate::dom::Bytes<'_>, buf: &mut PlainOutput, state: &WalkState<'_>, after_passed_over: bool) {
    let raw = bytes.as_utf8_str();
    let decoded = text::decode_html_entities_cow(raw.as_ref());
    #[cfg(feature = "visitor")]
    if visit_plain_text(&decoded, buf, state) {
        return;
    }
    if state.in_pre {
        buf.push_text(&decoded);
        return;
    }
    let normalized = text::normalize_whitespace_cow(&decoded);
    // ~keep A node that the walker passed over is no part of the text, so the white space on
    // ~keep its two sides is one run: the space that opens this text is not written after a
    // ~keep space. An element that was walked and wrote nothing keeps both spaces, and so does
    // ~keep a no-break space that opens the text, which `normalized` holds as a space.
    let joins_run = after_passed_over && decoded.starts_with([' ', '\t']) && buf.ends_with_space();
    let words = normalized
        .strip_prefix(' ')
        .filter(|_| joins_run)
        .unwrap_or_else(|| normalized.as_ref());
    if !(words.is_empty() || words == " " && buf.ends_with_line_end()) {
        buf.push_text(words);
    }
}

#[cfg(feature = "visitor")]
fn visit_plain_text(decoded: &str, buf: &mut PlainOutput, state: &WalkState<'_>) -> bool {
    let Some(visitor_handle) = state.visitor else {
        return false;
    };
    let node_ctx = NodeContext::with_borrowed_attributes(
        NodeType::Text,
        Cow::Borrowed(""),
        &EMPTY_ATTRS,
        state.depth,
        0,
        None,
        true,
    );
    let visit_result = visitor_handle
        .lock()
        .expect("visitor mutex poisoned")
        .visit_text(&node_ctx, decoded);
    match visit_result {
        VisitResult::Skip => true,
        VisitResult::Custom(custom) => {
            buf.push_text(&custom);
            true
        }
        _ => false,
    }
}

#[cfg(feature = "visitor")]
fn visit_plain_element_start(
    tag_name: &str,
    tag: &crate::dom::HTMLTag<'_>,
    buf: &mut PlainOutput,
    state: &WalkState<'_>,
) -> bool {
    let Some(visitor_handle) = state.visitor else {
        return false;
    };
    let node_ctx = plain_element_context(tag_name, tag, state.depth);
    let result = visitor_handle
        .lock()
        .expect("visitor mutex poisoned")
        .visit_element_start(&node_ctx);
    let VisitResult::Custom(custom) = result else {
        return matches!(result, VisitResult::Skip);
    };
    let start = buf.open();
    buf.push_text(&custom);
    let end_result = visitor_handle
        .lock()
        .expect("visitor mutex poisoned")
        .visit_element_end(&node_ctx, &custom);
    close_plain_checkpoint(buf, start, end_result);
    true
}

/// Calls the visitor at the end of an element and closes `element_start`, the checkpoint
/// opened before the element wrote.
#[cfg(feature = "visitor")]
fn visit_plain_element_end(
    visitor_handle: &VisitorHandle,
    tag_name: &str,
    tag: &crate::dom::HTMLTag<'_>,
    buf: &mut PlainOutput,
    depth: usize,
    element_start: Checkpoint,
) {
    let node_ctx = plain_element_context(tag_name, tag, depth);
    let result = visitor_handle
        .lock()
        .expect("visitor mutex poisoned")
        .visit_element_end(&node_ctx, buf.since(&element_start));
    close_plain_checkpoint(buf, element_start, result);
}

/// Closes `checkpoint` as the visitor answered at the end of an element: its text takes the
/// place of the text written since, a skip removes that text, and every other answer keeps it.
#[cfg(feature = "visitor")]
fn close_plain_checkpoint(buf: &mut PlainOutput, checkpoint: Checkpoint, end_result: VisitResult) {
    match end_result {
        VisitResult::Custom(custom) => {
            buf.rollback(checkpoint);
            buf.push_text(&custom);
        }
        VisitResult::Skip => buf.rollback(checkpoint),
        _ => buf.commit(checkpoint),
    }
}

#[cfg(feature = "visitor")]
fn plain_element_context<'a>(tag_name: &'a str, tag: &'a crate::dom::HTMLTag<'a>, depth: usize) -> NodeContext<'a> {
    NodeContext::with_lazy_attributes(
        NodeType::Element,
        Cow::Borrowed(tag_name),
        tag,
        depth,
        0,
        None,
        !is_block_level_element(tag_name),
    )
}

fn emit_plain_image(tag: &crate::dom::HTMLTag<'_>, buf: &mut PlainOutput, options: &ConversionOptions) {
    if options.skip_images {
        return;
    }
    if let Some(alt) = crate::converter::utility::attributes::decoded_attribute(tag, "alt")
        && !alt.is_empty()
    {
        buf.push_text(alt.as_ref());
    }
}

/// Collect all descendant `NodeHandle`s matching `target_tag` (by cloning handles). An element
/// that `exclude_selectors` names is not collected, and neither is an element in it.
fn collect_descendant_handles(
    tag: &crate::dom::HTMLTag,
    parser: &crate::dom::Parser,
    target_tag: &str,
    state: &WalkState<'_>,
    result: &mut Vec<crate::dom::NodeHandle>,
) {
    let mut stack: Vec<_> = if tag.name().as_utf8_str().eq_ignore_ascii_case("table") {
        crate::converter::utility::siblings::table_children(tag, parser)
    } else {
        tag.children().top().iter().copied().collect()
    };
    stack.reverse();

    while let Some(handle) = stack.pop() {
        if state.excludes(&handle) {
            continue;
        }
        if let Some(crate::dom::Node::Tag(child_tag)) = handle.get(parser) {
            let name = child_tag.name().as_utf8_str();
            if SKIP_TAGS.iter().any(|skip| name.eq_ignore_ascii_case(skip)) {
                continue;
            }
            if name.eq_ignore_ascii_case(target_tag) {
                result.push(handle);
            } else {
                let child_children = child_tag.children();
                let mut child_handles: Vec<_> = child_children.top().iter().copied().collect();
                child_handles.reverse();
                for child in child_handles {
                    stack.push(child);
                }
            }
        }
    }
}

/// Single-pass post-processor: trims trailing whitespace per line, collapses runs of 3+
/// newlines to exactly 2 (one blank line between paragraphs), and normalizes the trailing
/// newline. Uses `str::lines()` so multibyte UTF-8 codepoints are never split mid-character
/// — the original byte-oriented implementation cast `u8 as char`, mangling anything outside
/// ASCII (see issue #362).
///
/// Empty / whitespace-only input lines are folded into the surrounding newline run, so any
/// number of space-only lines between paragraphs collapses to a single blank line.
///
/// Algorithm contributed by @xitep in
/// <https://github.com/xberg-io/html-to-markdown/issues/362>: 2–7× faster than the
/// previous char-by-char rewrite while preserving identical semantics.
fn normalize_plain_output(buf: &mut String) {
    let input = std::mem::take(buf);
    let mut out = String::with_capacity(input.len());
    let mut last_was_blank = false;
    for line in input.lines().map(str::trim_end) {
        if line.is_empty() {
            if !last_was_blank {
                out.push('\n');
                last_was_blank = true;
            }
        } else {
            out.push_str(line);
            out.push('\n');
            last_was_blank = false;
        }
    }
    let keep = out.trim_end_matches('\n').len();
    out.truncate(keep);
    if !out.is_empty() {
        out.push('\n');
    }
    *buf = out;
}

/// Post-process the accumulated plain-text buffer.
fn post_process(buf: &mut String) {
    normalize_plain_output(buf);
}