html-to-markdown-rs 3.21.2

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
//! Table scanning and analysis utilities.
//!
//! Provides the `TableScan` struct and scanning functions for analyzing table structure
//! to determine if it should be rendered as a Markdown table or converted to list format.

use crate::converter::dom_context::TableContentSummary;
use crate::converter::utility::content::normalized_tag_name;
use std::borrow::Cow;

/// Scan results for a table element.
///
/// Contains metadata about table structure to determine optimal rendering:
/// - Row counts for consistency checking
/// - Presence of headers, captions, and nested tables
/// - Presence of colspan/rowspan (spanning cells)
/// - Link and text content counts
#[derive(Default)]
pub struct TableScan {
    /// Number of cells in each row
    pub row_counts: Vec<usize>,
    /// Whether any cells have colspan or rowspan attributes
    pub has_span: bool,
    /// Whether the table has header cells (th elements or role="head")
    pub has_header: bool,
    /// Whether the table has a caption element
    pub has_caption: bool,
    /// Number of nested tables found inside this table
    pub nested_table_count: usize,
    /// Count of anchor elements in the table
    pub link_count: usize,
    /// Whether the table has content to write: text, an image, a rule, or (with
    /// `br_in_tables` on) a line break
    pub has_text: bool,
}

/// Scan a table element for structural metadata.
///
/// Analyzes the table to determine characteristics that influence rendering:
/// - Whether to render as a Markdown table or layout table
/// - If spanning cells are present
/// - If the table has semantic meaning (headers, captions)
///
/// `has_text`/`link_count`/`has_header`/`has_caption` are gathered from the *entire*
/// subtree (including nested tables), since they answer "is there any semantic content
/// here at all"; `has_span`/`nested_table_count`/`row_counts` feed the layout-table
/// heuristic and reflect only *this* table's own direct structure — a straight chain of
/// one-nested-table-per-cell tables is not a layout table (issue #13). The two concerns
/// are computed by separate passes below so neither one re-walks a nested table's subtree
/// that an ancestor table has already accounted for; see [`content_summary`].
///
/// # Arguments
/// * `node_handle` - Handle to the table element
/// * `parser` - HTML parser instance
/// * `dom_ctx` - DOM context for tag name resolution
/// * `br_in_tables` - The conversion option of the same name; a `<br>` counts as content
///   only under this option, since it is what a cell actually writes it as (see
///   [`apply_tag_content`])
#[allow(clippy::trivially_copy_pass_by_ref)]
pub fn scan_table(
    node_handle: &crate::dom::NodeHandle,
    parser: &crate::dom::Parser,
    dom_ctx: &super::super::super::DomContext,
    ctx: &super::super::super::Context,
    br_in_tables: bool,
) -> TableScan {
    let (row_counts, nested_table_count, has_span) = scan_own_structure(node_handle, parser, dom_ctx, ctx);
    let content = content_summary(*node_handle, parser, dom_ctx, ctx, br_in_tables);
    TableScan {
        row_counts,
        has_span,
        has_header: content.has_header,
        has_caption: content.has_caption,
        nested_table_count,
        link_count: content.link_count,
        has_text: content.has_text,
    }
}

/// Resolve a node's normalized tag name via the `DomContext` cache, falling back to
/// parsing it directly when the node was not pre-registered (e.g. not yet visited).
fn tag_name_of<'a>(
    handle: &crate::dom::NodeHandle,
    tag: &'a crate::dom::HTMLTag,
    parser: &crate::dom::Parser,
    dom_ctx: &'a super::super::super::DomContext,
) -> Cow<'a, str> {
    dom_ctx.tag_info(handle.get_inner(), parser).map_or_else(
        || normalized_tag_name(tag.name().as_utf8_str()).into_owned().into(),
        |info| Cow::Borrowed(info.name.as_str()),
    )
}

/// Scan a table's own direct row/cell structure: row cell counts, spanning cells, and a
/// count of directly-nested `<table>` elements. Never descends into a nested `<table>`'s
/// subtree — none of these fields count content past that boundary anyway (issue #13), so
/// stopping there bounds each table's own scan to its own layer instead of everything
/// nested below it.
#[allow(clippy::trivially_copy_pass_by_ref)]
fn scan_own_structure(
    node_handle: &crate::dom::NodeHandle,
    parser: &crate::dom::Parser,
    dom_ctx: &super::super::super::DomContext,
    ctx: &super::super::super::Context,
) -> (Vec<usize>, usize, bool) {
    let mut row_counts = Vec::new();
    let mut nested_table_count = 0usize;
    let mut has_span = false;

    let Some(crate::dom::Node::Tag(root_tag)) = node_handle.get(parser) else {
        return (row_counts, nested_table_count, has_span);
    };

    let mut work: Vec<crate::dom::NodeHandle> = root_tag.children().top().iter().copied().collect();
    while let Some(handle) = work.pop() {
        if ctx.excluded_node_ids.contains(&handle.get_inner()) {
            continue;
        }
        let Some(crate::dom::Node::Tag(tag)) = handle.get(parser) else {
            continue;
        };
        let tag_name = tag_name_of(&handle, tag, parser, dom_ctx);

        match tag_name.as_ref() {
            "table" => nested_table_count += 1,
            "tr" | "row" => {
                let (cell_count, row_has_span) = scan_row_cells(tag, parser, dom_ctx, ctx);
                row_counts.push(cell_count);
                has_span |= row_has_span;
                // ~keep Still descend into the row's own cells (not their counts, already
                // ~keep captured above) so a `<table>` nested inside a `<td>` is found and
                // ~keep counted; only a nested `<table>` tag itself stops this walk.
                work.extend(tag.children().top().iter().copied());
            }
            _ => work.extend(tag.children().top().iter().copied()),
        }
    }

    (row_counts, nested_table_count, has_span)
}

/// Count cells (respecting `colspan`) in a single `<tr>`/`row` element and report whether
/// any cell carries a `colspan`/`rowspan` attribute.
fn scan_row_cells(
    row_tag: &crate::dom::HTMLTag,
    parser: &crate::dom::Parser,
    dom_ctx: &super::super::super::DomContext,
    ctx: &super::super::super::Context,
) -> (usize, bool) {
    let mut cell_count = 0;
    let mut has_span = false;
    for child in row_tag.children().top() {
        if ctx.excluded_node_ids.contains(&child.get_inner()) {
            continue;
        }
        let Some(crate::dom::Node::Tag(cell_tag)) = child.get(parser) else {
            continue;
        };
        let cell_name = tag_name_of(child, cell_tag, parser, dom_ctx);
        if !matches!(cell_name.as_ref(), "td" | "th" | "cell") {
            continue;
        }
        cell_count += super::cell::get_colspan(child, parser);
        let attrs = cell_tag.attributes();
        if attrs.get("colspan").is_some() || attrs.get("rowspan").is_some() {
            has_span = true;
        }
    }
    (cell_count, has_span)
}

/// A pending frame in the [`content_summary`] iterative traversal.
enum ContentFrame {
    /// Visit a node: accumulate its own content into the innermost open table accumulator,
    /// opening a new one first if the node is itself a `<table>`.
    Enter(crate::dom::NodeHandle),
    /// All descendants of the `<table>` node with this id have been visited; finalize and
    /// cache its accumulator, merging it into the now-current parent accumulator (if any).
    ExitTable(u32),
}

/// Compute (and memoize) the full-subtree content summary — text/links/headers/caption,
/// including nested tables — for the `<table>` at `table_handle`.
///
/// A naive per-table full-subtree walk is O(n²) on a chain of n nested tables, since each
/// outer table's walk re-visits every table nested below it. This performs a single
/// iterative post-order traversal that opens a fresh accumulator on every `<table>` boundary
/// it crosses (including the root) and, on leaving each one, caches its result and folds it
/// into the enclosing accumulator. The first call for the outermost table in a chain pays
/// the full O(n) walk once and populates the cache for every table below it; subsequent
/// calls for those nested tables (made later, as the walk renders into them) are O(1) cache
/// hits. Explicit `Vec`-backed frames avoid native recursion, so this does not reintroduce
/// the stack-depth risk `deeply_nested_tables_do_not_overflow_stack` guards against.
#[allow(clippy::trivially_copy_pass_by_ref)]
fn content_summary(
    table_handle: crate::dom::NodeHandle,
    parser: &crate::dom::Parser,
    dom_ctx: &super::super::super::DomContext,
    ctx: &super::super::super::Context,
    br_in_tables: bool,
) -> TableContentSummary {
    let table_id = table_handle.get_inner();
    if let Some(cached) = dom_ctx.cached_table_content_summary(table_id) {
        return cached;
    }

    let mut acc_stack: Vec<(TableContentSummary, bool)> = Vec::new();
    let mut work = vec![ContentFrame::Enter(table_handle)];
    while let Some(frame) = work.pop() {
        match frame {
            ContentFrame::Enter(handle) => {
                if ctx.excluded_node_ids.contains(&handle.get_inner()) {
                    continue;
                }
                visit_content_node(handle, parser, dom_ctx, &mut acc_stack, &mut work, br_in_tables);
            }
            ContentFrame::ExitTable(id) => finish_table_accumulator(id, dom_ctx, &mut acc_stack),
        }
    }

    dom_ctx.cached_table_content_summary(table_id).unwrap_or_default()
}

/// Process one node in the [`content_summary`] traversal: fold raw text and recognized tags
/// into the innermost open accumulator, opening a new one when `handle` is itself a
/// `<table>`, and queue its children for a later `Enter`.
fn visit_content_node(
    handle: crate::dom::NodeHandle,
    parser: &crate::dom::Parser,
    dom_ctx: &super::super::super::DomContext,
    acc_stack: &mut Vec<(TableContentSummary, bool)>,
    work: &mut Vec<ContentFrame>,
    br_in_tables: bool,
) {
    let content = ContentTest {
        br_in_tables: br_in_tables && !acc_stack.last().is_some_and(|(_, presentation)| *presentation),
        checkboxes: true,
    };
    let acc = acc_stack.last_mut().map(|(acc, _)| acc);
    match fold_node_content(handle, parser, dom_ctx, acc, content) {
        Some(Element::Table(tag)) => {
            let id = handle.get_inner();
            let presentation = tag
                .attributes()
                .get("role")
                .flatten()
                .is_some_and(|role| role.as_utf8_str().eq_ignore_ascii_case("presentation"));
            acc_stack.push((TableContentSummary::default(), presentation));
            work.push(ContentFrame::ExitTable(id));
            work.extend(tag.children().top().iter().copied().map(ContentFrame::Enter));
        }
        Some(Element::Other(tag)) => work.extend(tag.children().top().iter().copied().map(ContentFrame::Enter)),
        None => {}
    }
}

/// What the content test of a table counts.
#[derive(Clone, Copy)]
struct ContentTest {
    /// The conversion option of the same name: a line break is content only under it.
    br_in_tables: bool,
    /// Whether a checkbox is content. A table asks with `true`. A cell asks with `false`, to
    /// learn whether it holds something other than checkboxes.
    checkboxes: bool,
}

/// An element that [`fold_node_content`] read.
enum Element<'p> {
    /// A `<table>`. Its content belongs to a summary of its own.
    Table(&'p crate::dom::HTMLTag<'p>),
    /// Any other element.
    Other(&'p crate::dom::HTMLTag<'p>),
}

/// The content test of a table for one node: folds the text of a text node, or what an element
/// that is not a table counts for, into `acc`. Returns the element, for the caller to read
/// its children.
///
/// ~keep The scan of a table and `cell_holds_only_inputs` both read each node here, so "the
/// ~keep table has content" and "the cell holds only inputs" are one decision with one list.
fn fold_node_content<'p>(
    handle: crate::dom::NodeHandle,
    parser: &'p crate::dom::Parser<'p>,
    dom_ctx: &super::super::super::DomContext,
    acc: Option<&mut TableContentSummary>,
    content: ContentTest,
) -> Option<Element<'p>> {
    match handle.get(parser)? {
        crate::dom::Node::Raw(bytes) => {
            // ~keep A character reference for white space is white space.
            if !crate::text::decode_html_entities_cow(bytes.as_utf8_str().as_ref())
                .trim()
                .is_empty()
            {
                if let Some(acc) = acc {
                    acc.has_text = true;
                }
            }
            None
        }
        crate::dom::Node::Tag(tag) => {
            let tag_name = tag_name_of(&handle, tag, parser, dom_ctx);
            if tag_name.as_ref() == "table" {
                return Some(Element::Table(tag));
            }
            apply_tag_content(&tag_name, tag, parser, acc, content);
            Some(Element::Other(tag))
        }
        crate::dom::Node::Comment(_) => None,
    }
}

/// Whether the table cell `cell_handle` holds only inputs: the content test of a table, with
/// checkboxes left out, finds nothing in the cell. A table inside the cell is content.
///
/// ~keep This reads the subtree of the cell, so a cell asks it one time for each walk of its
/// ~keep children, where the walk opens the cell: `Context::for_cell`, and the two walks that
/// ~keep build the context of a cell themselves (the table grid and a layout cell). A control
/// ~keep reads the answer from its context. The walk stops at the first content and at a table
/// ~keep inside the cell, so nested tables are not read one time for each level.
pub fn cell_holds_only_inputs(
    cell_handle: crate::dom::NodeHandle,
    parser: &crate::dom::Parser,
    dom_ctx: &super::super::super::DomContext,
    br_in_tables: bool,
) -> bool {
    let content = ContentTest {
        br_in_tables,
        checkboxes: false,
    };
    let Some(crate::dom::Node::Tag(cell_tag)) = cell_handle.get(parser) else {
        return false;
    };
    let mut found = TableContentSummary::default();
    let mut work: Vec<crate::dom::NodeHandle> = cell_tag.children().top().iter().copied().collect();
    while let Some(handle) = work.pop() {
        match fold_node_content(handle, parser, dom_ctx, Some(&mut found), content) {
            Some(Element::Table(_)) => return false,
            Some(Element::Other(tag)) => work.extend(tag.children().top().iter().copied()),
            None => {}
        }
        if !found.is_empty() {
            return false;
        }
    }
    true
}

/// Fold a single non-table tag's contribution (link/header/caption/image/rule/line break)
/// into the current accumulator, if one is open.
fn apply_tag_content(
    tag_name: &str,
    tag: &crate::dom::HTMLTag,
    parser: &crate::dom::Parser,
    acc: Option<&mut TableContentSummary>,
    content: ContentTest,
) {
    let Some(acc) = acc else { return };
    let br_in_tables = content.br_in_tables;
    match tag_name {
        "a" => acc.link_count += 1,
        "caption" => acc.has_caption = true,
        "th" => acc.has_header = true,
        "img" | "graphic" if tag.attributes().get("src").is_some() || tag.attributes().get("alt").is_some() => {
            acc.has_text = true;
        }
        "svg" => acc.has_text = true,
        "iframe" if !crate::converter::media::embedded::extract_media_src(tag).is_empty() => acc.has_text = true,
        "audio" | "video"
            if !crate::converter::media::embedded::media_element_src(
                tag,
                parser,
                crate::options::InlineDataMedia::Keep,
            )
            .is_empty() =>
        {
            acc.has_text = true;
        }
        "input" if content.checkboxes && crate::converter::form::elements::checkbox_state(tag).is_some() => {
            acc.has_text = true;
        }
        // ~keep A rule is content without text: a table whose cells hold only rules is not a blank
        // ~keep spacer, and dropping it lost the whole table (issue #628).
        "hr" => acc.has_text = true,
        // ~keep A cell writes a literal `<br>` only when `br_in_tables` is on (see
        // ~keep `emit_table_cell_break`); with it off the break collapses to a space a cell
        // ~keep trims away, so only the `br_in_tables` case is content (issue #646).
        "br" if br_in_tables => acc.has_text = true,
        "cell" => {
            if let Some(Some(role)) = tag.attributes().get("role") {
                if role.as_utf8_str() == "head" {
                    acc.has_header = true;
                }
            }
        }
        _ => {}
    }
}

/// Pop the accumulator for `id`, cache it, and fold it into the new top-of-stack
/// accumulator (the enclosing table), if any.
fn finish_table_accumulator(
    id: u32,
    dom_ctx: &super::super::super::DomContext,
    acc_stack: &mut Vec<(TableContentSummary, bool)>,
) {
    let Some((finished, _)) = acc_stack.pop() else {
        return;
    };
    dom_ctx.cache_table_content_summary(id, finished);
    if let Some((parent, _)) = acc_stack.last_mut() {
        parent.merge(finished);
    }
}