html-to-markdown-rs 3.22.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
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
//! Table cell conversion utilities.
//!
//! Handles conversion of table cell (td/th) elements to Markdown format,
//! including colspan support and content normalization.

use crate::converter::block::container::HandlerContext;

/// Maximum allowed table columns to prevent unbounded memory usage.
const MAX_TABLE_COLS: usize = 1000;

/// Get colspan attribute value from an element.
///
/// Reads the colspan attribute from a table cell, with bounds checking
/// to prevent memory exhaustion attacks.
///
/// # Arguments
/// * `node_handle` - Handle to the cell element
/// * `parser` - HTML parser instance
///
/// # Returns
/// The colspan value (minimum 1, maximum `MAX_TABLE_COLS`)
#[allow(clippy::trivially_copy_pass_by_ref)]
pub fn get_colspan(node_handle: &crate::dom::NodeHandle, parser: &crate::dom::Parser) -> usize {
    if let Some(crate::dom::Node::Tag(tag)) = node_handle.get(parser) {
        if let Some(Some(bytes)) = tag.attributes().get("colspan") {
            if let Ok(colspan) = bytes.as_utf8_str().parse::<usize>() {
                return clamp_table_span(colspan);
            }
        }
    }
    1
}

/// Get both colspan and rowspan in a single lookup.
///
/// More efficient than calling `get_colspan` and a separate rowspan lookup.
///
/// # Arguments
/// * `node_handle` - Handle to the cell element
/// * `parser` - HTML parser instance
///
/// # Returns
/// A tuple of (colspan, rowspan), both minimum 1 and maximum `MAX_TABLE_COLS`
#[allow(clippy::trivially_copy_pass_by_ref)]
pub fn get_colspan_rowspan(node_handle: &crate::dom::NodeHandle, parser: &crate::dom::Parser) -> (usize, usize) {
    if let Some(crate::dom::Node::Tag(tag)) = node_handle.get(parser) {
        let attrs = tag.attributes();
        let colspan = attrs
            .get("colspan")
            .flatten()
            .and_then(|v| v.as_utf8_str().parse::<usize>().ok())
            .map_or(1, clamp_table_span);
        let rowspan = attrs
            .get("rowspan")
            .flatten()
            .and_then(|v| v.as_utf8_str().parse::<usize>().ok())
            .map_or(1, clamp_table_span);
        (colspan, rowspan)
    } else {
        (1, 1)
    }
}

/// Clamp a table span value to safe bounds.
///
/// Prevents memory exhaustion by clamping colspan/rowspan values.
fn clamp_table_span(value: usize) -> usize {
    if value == 0 { 1 } else { value.min(MAX_TABLE_COLS) }
}

/// Collect table cells (td/th) from a row element.
///
/// Extracts only the direct cell children of a row, filtering by tag name.
///
/// # Arguments
/// * `node_handle` - Handle to the row element
/// * `parser` - HTML parser instance
/// * `dom_ctx` - DOM context for tag name resolution
/// * `cells` - Mutable vector to populate with cell handles
#[allow(clippy::trivially_copy_pass_by_ref)]
pub fn collect_table_cells(
    node_handle: &crate::dom::NodeHandle,
    parser: &crate::dom::Parser,
    dom_ctx: &super::super::super::DomContext,
    ctx: &super::super::super::Context,
    cells: &mut Vec<crate::dom::NodeHandle>,
) {
    cells.clear();
    if ctx.excluded_node_ids.contains(&node_handle.get_inner()) {
        return;
    }
    if let Some(crate::dom::Node::Tag(tag)) = node_handle.get(parser) {
        let children = tag.children();
        for child_handle in children.top() {
            if ctx.excluded_node_ids.contains(&child_handle.get_inner()) {
                continue;
            }
            if let Some(cell_name) = dom_ctx.tag_name_for(*child_handle, parser) {
                if matches!(cell_name.as_ref(), "th" | "td" | "cell") {
                    cells.push(*child_handle);
                }
            }
        }
    }
}

/// Extract the text content of a table cell for column width calculation.
///
/// Returns the same text that would appear in the rendered cell, without
/// the surrounding pipe delimiters. Used in the first pass to compute
/// maximum column widths before rendering with padding.
///
/// # Arguments
/// * `node_handle` - Handle to the cell element
/// * `parser` - HTML parser instance
/// * `options` - Conversion options
/// * `ctx` - Conversion context
/// * `dom_ctx` - DOM context
/// * `depth` - Current recursion depth (the cell's own depth; children are walked at `depth + 1`)
#[allow(clippy::trivially_copy_pass_by_ref)]
pub fn cell_text_content(
    node_handle: &crate::dom::NodeHandle,
    parser: &crate::dom::Parser,
    options: &crate::options::ConversionOptions,
    ctx: &super::super::super::Context,
    dom_ctx: &super::super::super::DomContext,
    depth: usize,
) -> String {
    let cell_ctx = super::super::super::Context {
        in_table_cell: true,
        ..ctx.clone()
    };

    // ~keep Width measurement never defers a nested table out to a separate block: the
    // ~keep result is discarded once its length is measured (capped at 200 chars anyway),
    // ~keep and deferring here as well as in the render pass would queue it twice (issue #484).
    render_cell_text(
        node_handle,
        parser,
        HandlerContext::new(options, &cell_ctx, depth, dom_ctx),
        None,
    )
}

/// Initial buffer capacity for a rendered cell's markdown.
const CELL_TEXT_CAPACITY: usize = 128;

/// Render a cell's content to the exact text that appears between its pipe delimiters.
///
/// `cell_ctx` must already carry `in_table_cell = true`; the caller owns that decision so
/// the per-row context can be built once instead of cloned per cell.
///
/// `depth` is the cell's own recursion depth; children are walked at `depth + 1`.
///
/// `deferred_tables`, when `Some`, receives a child that is, or wraps (e.g. a `<div>`), a
/// nested `<table>`, rendered as that child's markdown verbatim (trimmed, unescaped,
/// un-flattened) instead of folding it into this cell's single line. The caller passes `Some`
/// only when the enclosing row holds no other cell — GFM has no way to express a real nested
/// table, but a lone cell's content can be lifted out and rendered as its own separate table
/// after the enclosing one, which keeps the inner table usable instead of flattening it into a
/// line of cell content (issue #484). `None` (the default, and always the case for a cell
/// sharing its row with a sibling) flattens nested rows without table syntax (#760).
#[allow(clippy::trivially_copy_pass_by_ref)]
pub fn render_cell_text(
    node_handle: &crate::dom::NodeHandle,
    parser: &crate::dom::Parser,
    handler: HandlerContext<'_>,
    mut deferred_tables: Option<&mut Vec<String>>,
) -> String {
    let mut text = String::with_capacity(CELL_TEXT_CAPACITY);
    render_cell_content(node_handle, parser, &mut text, handler, &mut deferred_tables);
    finalize_cell_text(text, handler.options)
}

fn render_cell_content(
    node_handle: &crate::dom::NodeHandle,
    parser: &crate::dom::Parser,
    text: &mut String,
    handler: HandlerContext<'_>,
    deferred_tables: &mut Option<&mut Vec<String>>,
) {
    let Some(crate::dom::Node::Tag(tag)) = node_handle.get(parser) else {
        return;
    };
    let children = tag.children();
    // ~keep A comment splits the text of the cell in two nodes. The walk writes each of them
    // ~keep with the rules of running text, as Tier-1 does; the shortcut is for one text alone.
    let has_tag_or_comment_child = children.top().iter().any(|child_handle| {
        matches!(
            child_handle.get(parser),
            Some(crate::dom::Node::Tag(_) | crate::dom::Node::Comment(_))
        )
    });
    if !has_tag_or_comment_child {
        let raw = handler.dom_ctx.text_content(*node_handle, parser);
        let normalized = if handler.options.whitespace_mode == crate::options::WhitespaceMode::Normalized {
            crate::text::normalize_cell_whitespace_cow(raw.as_str())
        } else {
            crate::text::fold_cell_line_breaks_verbatim_cow(raw.as_str())
        };
        *text = escape_cell_text(normalized.as_ref(), handler.options);
        return;
    }
    let cell_ctx = handler
        .ctx
        .for_cell(*node_handle, parser, handler.dom_ctx, handler.options.br_in_tables);
    let handler = HandlerContext::new(handler.options, &cell_ctx, handler.depth, handler.dom_ctx);
    let mut table_ends = Vec::new();
    for child_handle in children.top() {
        render_cell_child(child_handle, parser, text, handler, deferred_tables);
        if deferred_tables.is_none() && super::utils::is_or_contains_table(child_handle, parser, handler.dom_ctx) {
            table_ends.push(text.len());
        }
    }
    separate_nested_table_following_content(text, &table_ends, handler.options.br_in_tables);
}

/// Separate content after a flattened nested table once the cell's inline markup is complete.
/// ~keep Delaying insertion keeps code/link offsets stable and avoids a trailing synthetic `<br>`.
pub fn separate_nested_table_following_content(text: &mut String, table_ends: &[usize], br_in_tables: bool) {
    if table_ends.is_empty() {
        return;
    }
    let original = std::mem::take(text);
    let mut start = 0;
    let content_end = original.trim_end().len();
    for &end in table_ends {
        if end <= start {
            continue;
        }
        let Some(piece) = original.get(start..end) else {
            continue;
        };
        let Some(following) = original.get(end..) else { continue };
        text.push_str(piece);
        if end < content_end && !following.starts_with(char::is_whitespace) && !following.starts_with("<br>") {
            text.push_str(if br_in_tables { "<br>" } else { " " });
        }
        start = end;
    }
    text.push_str(&original[start..]);
}

/// Text of a nested table for the width pre-pass, in one walk so issue #406 stays linear.
/// ~keep The flat form writes one separator between cells and one between rows; the measured
/// ~keep text has them too, or the rule under the header is short by one character for each.
#[allow(clippy::trivially_copy_pass_by_ref)]
pub fn nested_table_width_text(
    node_handle: &crate::dom::NodeHandle,
    parser: &crate::dom::Parser,
    dom_ctx: &super::super::super::DomContext,
    ctx: &super::super::super::Context,
    br_in_tables: bool,
) -> String {
    let mut text = String::with_capacity(64);
    let mut row_gap = false;
    let mut cell_gap = false;
    let mut stack = vec![*node_handle];
    while let Some(handle) = stack.pop() {
        if ctx.excluded_node_ids.contains(&handle.get_inner()) {
            continue;
        }
        match handle.get(parser) {
            Some(crate::dom::Node::Raw(bytes)) => {
                let raw = bytes.as_utf8_str();
                let decoded = crate::text::decode_html_entities_cow(raw.as_ref());
                if decoded.trim().is_empty() {
                    cell_gap |= !text.is_empty();
                    continue;
                }
                if text.is_empty() {
                } else if row_gap {
                    crate::converter::emit_table_cell_break(&mut text, br_in_tables);
                } else if cell_gap && !text.ends_with(char::is_whitespace) {
                    text.push(' ');
                }
                row_gap = false;
                cell_gap = false;
                text.push_str(decoded.as_ref());
            }
            Some(crate::dom::Node::Tag(tag)) => {
                row_gap |= super::utils::is_tag_name(&handle, parser, dom_ctx, "tr");
                cell_gap |= ["td", "th"]
                    .iter()
                    .any(|name| super::utils::is_tag_name(&handle, parser, dom_ctx, name));
                if let Some(children) = dom_ctx.children_of(handle.get_inner()) {
                    stack.extend(children.iter().rev().copied());
                } else {
                    let first = stack.len();
                    stack.extend(tag.children().top().iter().copied());
                    stack[first..].reverse();
                }
            }
            _ => {}
        }
    }
    text
}

/// ~keep A nested table's structural pipes bypass text-node escaping. It must either be deferred
/// ~keep from a single-cell row or flattened to cell content with explicit row separators;
/// ~keep otherwise reparsing widens and eventually truncates the outer row (issues #469/#484/#488).
fn render_cell_child(
    child_handle: &crate::dom::NodeHandle,
    parser: &crate::dom::Parser,
    text: &mut String,
    handler: HandlerContext<'_>,
    deferred_tables: &mut Option<&mut Vec<String>>,
) {
    if !super::utils::is_or_contains_table(child_handle, parser, handler.dom_ctx) {
        super::super::super::walk_node(
            child_handle,
            parser,
            text,
            crate::converter::block::container::HandlerContext::new(
                handler.options,
                handler.ctx,
                handler.depth + 1,
                handler.dom_ctx,
            ),
        );
        return;
    }
    let mut nested = String::new();
    let nested_context = crate::converter::Context {
        allow_nested_table_markup: deferred_tables.is_some(),
        ..handler.ctx.clone()
    };
    super::super::super::walk_node(
        child_handle,
        parser,
        &mut nested,
        crate::converter::block::container::HandlerContext::new(
            handler.options,
            &nested_context,
            handler.depth + 1,
            handler.dom_ctx,
        ),
    );
    if let Some(buf) = deferred_tables.as_deref_mut() {
        let trimmed = nested.trim();
        if !trimmed.is_empty() {
            buf.push(trimmed.to_string());
        }
        return;
    }
    if nested.contains('|') {
        nested = crate::converter::utility::escaping::escape_bare_pipes_outside_code_spans(&nested);
    }
    let nested = fold_nested_table_rows(&nested, handler.options.br_in_tables);
    if !nested.is_empty() && !text.trim_end().is_empty() {
        crate::converter::emit_table_cell_break(text, handler.options.br_in_tables);
    }
    text.push_str(&nested);
}

pub(super) fn finalize_cell_text(mut text: String, options: &crate::options::ConversionOptions) -> String {
    trim_in_place(&mut text);
    // ~keep Final invariant: a rendered cell never contains a newline, in any mode. This used
    // ~keep to be gated on `!br_in_tables`, which is what let issues #456 and #457 reach the
    // ~keep output — `br_in_tables` selects how a *line break* is represented (`<br>` vs a
    // ~keep space), it does not make a raw newline legal between two pipes. Handlers that know
    // ~keep they are in a cell still emit `<br>` themselves; this only catches what they miss.
    if text.contains('\n') {
        text = text.replace('\n', " ");
    }
    // ~keep A `|` a handler wrote into the cell (a code span, a link destination or title, an
    // ~keep image description, a nested table) splits the row like one in plain text does.
    // ~keep GFM splits on a pipe in a code span too and reads `\|` there as `|`; Djot does not
    // ~keep split inside a verbatim span, and a backslash there stays literal.
    if text.contains('|') {
        match options.output_format {
            crate::options::OutputFormat::Markdown => {
                text = crate::converter::utility::escaping::escape_cell_pipes(&text).into_owned();
            }
            crate::options::OutputFormat::Djot => {
                text = crate::converter::utility::escaping::escape_bare_pipes_outside_code_spans(&text);
            }
            crate::options::OutputFormat::Plain => {}
        }
    }
    text
}

/// Flatten a nested table's rendered rows onto the single line a Markdown cell allows.
///
/// Each row arrives on its own line. `br_in_tables` selects how the boundary between them is
/// spelled — a literal `<br>` when set, a single space otherwise — matching what every other
/// in-cell handler does with a line break (`emit_table_cell_break`, issues #453/#454). Blank
/// lines the inner table emits around itself carry no content and are dropped.
pub fn fold_nested_table_rows(nested: &str, br_in_tables: bool) -> String {
    let separator = if br_in_tables { "<br>" } else { " " };
    nested
        .lines()
        .map(str::trim)
        .filter(|line| !line.is_empty())
        .collect::<Vec<_>>()
        .join(separator)
}

/// Trim leading and trailing whitespace without reallocating.
fn trim_in_place(text: &mut String) {
    let end = text.trim_end().len();
    text.truncate(end);
    let start = text.len() - text.trim_start().len();
    text.drain(..start);
}

/// Escape text for use inside a table cell.
///
/// Applies every `escape_*` option exactly as outside a table (issue #638), and
/// escapes `|` (pipe) when neither `escape_misc` nor `escape_ascii` already does. Raw
/// backticks are always escaped for Djot because they otherwise consume the table syntax.
fn escape_cell_text(text: &str, options: &crate::options::ConversionOptions) -> String {
    let escaped = crate::text::escape(
        text,
        options.escape_misc,
        options.escape_asterisks,
        options.escape_underscores,
        options.escape_ascii,
    );
    let escaped = if options.escape_misc || options.escape_ascii {
        escaped.into_owned()
    } else {
        escaped.replace('|', r"\|")
    };
    crate::converter::utility::escaping::escape_djot_table_cell_literal(&escaped, options.output_format, true)
        .into_owned()
}

/// Convert a table cell (td or th) to Markdown format.
///
/// Processes cell content and renders it with pipe delimiters for Markdown tables.
/// Handles colspan by adding extra pipes, and escapes pipes in cell content.
///
/// # Arguments
/// * `node_handle` - Handle to the cell element
/// * `parser` - HTML parser instance
/// * `output` - Mutable string to append cell content
/// * `options` - Conversion options (escape settings, `br_in_tables`)
/// * `ctx` - Conversion context (visitor, etc)
/// * `_tag_name` - Tag name (for consistency, not used)
/// * `dom_ctx` - DOM context for content extraction
/// * `col_width` - Optional target width for padding (None = no padding)
/// * `depth` - Current recursion depth (the cell's own depth; children are walked at `depth + 1`)
/// * `deferred_tables` - See [`render_cell_text`]; forwarded unchanged.
#[allow(clippy::trivially_copy_pass_by_ref)]
pub fn convert_table_cell(
    node_handle: &crate::dom::NodeHandle,
    parser: &crate::dom::Parser,
    output: &mut String,
    handler: HandlerContext<'_>,
    col_width: Option<usize>,
    deferred_tables: Option<&mut Vec<String>>,
) {
    let text = render_cell_text(node_handle, parser, handler, deferred_tables);
    emit_cell_text(node_handle, parser, output, &text, col_width);
}

/// Emit already-rendered cell text with its padding and colspan pipe delimiters.
///
/// Split out from [`convert_table_cell`] so a cell rendered during the column-width
/// pre-pass can be emitted without walking its subtree a second time.
#[allow(clippy::trivially_copy_pass_by_ref)]
pub fn emit_cell_text(
    node_handle: &crate::dom::NodeHandle,
    parser: &crate::dom::Parser,
    output: &mut String,
    text: &str,
    col_width: Option<usize>,
) {
    let colspan = get_colspan(node_handle, parser);

    output.push(' ');
    output.push_str(text);
    if let Some(width) = col_width {
        let text_len = text.chars().count();
        if text_len < width {
            for _ in 0..(width - text_len) {
                output.push(' ');
            }
        }
    }
    for _ in 0..colspan {
        output.push_str(" |");
    }
}

#[cfg(test)]
mod tests {
    #[test]
    fn rich_formatting_preserved_in_cells() {
        let html = "<table><tr><th>H</th></tr><tr><td><strong>Bold</strong> and <em>italic</em></td></tr></table>";
        let result = crate::convert(html, None).unwrap();
        let content = result.content.unwrap_or_default();
        assert!(
            content.contains("**Bold**") || content.contains("__Bold__"),
            "bold should be preserved: {content}"
        );
        assert!(
            content.contains("*italic*") || content.contains("_italic_"),
            "italic should be preserved: {content}"
        );
    }
}