html-to-markdown-rs 3.17.0

High-performance HTML to Markdown converter using the astral-tl parser. 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
//! Link element handler for HTML to Markdown conversion.
//!
//! Handles `<a>` elements including:
//! - Basic link markdown output `[text](href "title")`
//! - Autolinks when text matches href
//! - Links containing heading elements
//! - Complex link content with mixed block/inline elements
//! - Visitor callback integration
//! - Link metadata collection

#[cfg(feature = "metadata")]
use std::collections::BTreeMap;

use crate::converter::Context;
use crate::converter::block::heading::{find_single_heading_child, heading_allows_inline_images, push_heading};
use crate::converter::dom_context::DomContext;
use crate::converter::inline::HandlerContext;
use crate::converter::inline::link::{MarkdownLink, append_markdown_link_in_context, has_uri_scheme};
use crate::converter::main::walk_node;
use crate::converter::media::inline_data_treatment;
use crate::converter::utility::content::{
    collect_link_label_text, get_text_content, node_is_block_level, normalize_link_label, normalized_tag_name,
};
use crate::converter::utility::escaping::escape_link_label;
use crate::options::{ConversionOptions, InlineDataMedia};
use crate::text;
use std::borrow::Cow;

#[cfg(feature = "visitor")]
use crate::converter::utility::serialization::serialize_node;

fn indent_hard_break_continuations(label: &mut String, ctx: &Context, options: &ConversionOptions) {
    if !ctx.in_list_item || !label.contains('\n') {
        return;
    }
    let Some(indent) = crate::converter::list::utils::continuation_indent_string(ctx.list_indent_columns, options)
    else {
        return;
    };
    let original = std::mem::take(label);
    let mut lines = original.split('\n');
    let mut indented = lines.next().unwrap_or_default().to_string();
    for line in lines {
        indented.push('\n');
        if !line.is_empty() {
            // ~keep Link-label normalization collapses the content-column whitespace after a
            // ~keep hard break to one space. Restore the enclosing item's actual column so the
            // ~keep continuation cannot become a new block and split the link (issue #678).
            indented.push_str(&indent);
            indented.push_str(line.trim_start_matches([' ', '\t']));
        }
    }
    *label = indented;
}

/// Handle an `<a>` (link) element and convert to Markdown.
///
/// This handler processes link elements including:
/// - Extracting href and title attributes
/// - Detecting autolinks (where text equals href)
/// - Handling links that contain heading elements
/// - Processing complex link content (mixed block/inline)
/// - Invoking visitor callbacks when the visitor feature is enabled
/// - Collecting link metadata when the metadata feature is enabled
/// - Generating appropriate markdown link output
pub fn handle_link(tag: &tl::HTMLTag, mut handler: HandlerContext<'_>) {
    let Some(data) = LinkData::new(tag, &handler) else {
        walk_handles_to_output(tag.children().top().iter().copied(), &mut handler);
        return;
    };
    if emit_autolink(&data, &mut handler) || emit_heading_link(&data, &mut handler) {
        return;
    }

    handler.context.inline_data_replaced.set(false);
    let mut label = build_label(&data, &handler);
    apply_label_fallbacks(&data, &mut label, &handler);
    indent_hard_break_continuations(&mut label, handler.context, handler.options);
    let drop_link = label.is_empty() && handler.context.inline_data_replaced.get();
    let emit_deferred = emit_link(tag, &data, &label, drop_link, &mut handler);
    #[cfg(feature = "metadata")]
    record_link_metadata(tag, &data, &label, handler.context);
    if data.emit_blocks_separately && emit_deferred {
        walk_handles_to_output(data.deferred.iter().copied(), &mut handler);
    }
}

struct LinkData<'a> {
    href: String,
    title: Option<Cow<'a, str>>,
    children: Vec<tl::NodeHandle>,
    inline_label: String,
    raw_text: String,
    inline_children: Vec<tl::NodeHandle>,
    deferred: Vec<tl::NodeHandle>,
    emit_blocks_separately: bool,
    href_addr_dropped: bool,
    link_allow_inline_images: bool,
    saw_block: bool,
}

impl<'a> LinkData<'a> {
    fn new(tag: &'a tl::HTMLTag<'a>, handler: &HandlerContext<'_>) -> Option<Self> {
        let href = tag
            .attributes()
            .get("href")
            .flatten()
            .map(|value| text::decode_attribute_value_cow(&value.as_utf8_str()).into_owned())
            .map(|href| {
                handler
                    .context
                    .resolve_url(&href, handler.node_handle, handler.parser, handler.dom_context)
                    .unwrap_or(href)
            })?;
        // ~keep Empty titles are absent because Markdown serializers drop `""` on reparse.
        let title =
            crate::converter::utility::attributes::decoded_attribute(tag, "title").filter(|value| !value.is_empty());
        let children = handler
            .dom_context
            .children_of(handler.node_handle.get_inner())
            .map_or_else(|| tag.children().top().iter().copied().collect(), ToOwned::to_owned);
        let (inline_label, _, saw_block) = collect_link_label_text(&children, handler.parser, handler.dom_context);
        let text_source = if saw_block {
            get_text_content(handler.node_handle, handler.parser, handler.dom_context)
        } else {
            inline_label.clone()
        };
        let raw_text = text::normalize_whitespace_cow(&text_source).trim().to_string();
        let (inline_children, deferred) = partition_link_children(&children, handler.parser, handler.dom_context);
        Some(Self {
            href_addr_dropped: matches!(
                inline_data_treatment(handler.options.inline_data_media, &href),
                InlineDataMedia::AltTextOnly | InlineDataMedia::DropElement
            ),
            emit_blocks_separately: should_defer_table_blocks(
                handler.context,
                &deferred,
                handler.parser,
                handler.dom_context,
            ),
            link_allow_inline_images: handler.context.keep_inline_images_in.contains("a"),
            href,
            title,
            children,
            inline_label,
            raw_text,
            inline_children,
            deferred,
            saw_block,
        })
    }
}

fn emit_autolink(data: &LinkData<'_>, handler: &mut HandlerContext<'_>) -> bool {
    // ~keep Deferred tables and dropped data addresses can never use the visible `<href>` form (#120, #490).
    let autolink = handler.options.autolinks
        && !handler.options.default_title
        && !data.emit_blocks_separately
        && !data.href.is_empty()
        && !data.href_addr_dropped
        && has_uri_scheme(&data.href)
        && (data.raw_text == data.href || (data.href.starts_with("mailto:") && data.raw_text == data.href[7..]));
    if !autolink {
        return false;
    }
    handler.output.push('<');
    if data.href.starts_with("mailto:") && data.raw_text == data.href[7..] {
        handler.output.push_str(&data.raw_text);
    } else {
        handler.output.push_str(&data.href);
    }
    handler.output.push('>');
    true
}

fn emit_heading_link(data: &LinkData<'_>, handler: &mut HandlerContext<'_>) -> bool {
    if data.href_addr_dropped {
        return false;
    }
    let Some((level, heading_handle)) = find_single_heading_child(*handler.node_handle, handler.parser) else {
        return false;
    };
    let Some(tl::Node::Tag(heading_tag)) = heading_handle.get(handler.parser) else {
        return false;
    };
    let heading_name = normalized_tag_name(heading_tag.name().as_utf8_str());
    let heading_context = Context {
        in_heading: true,
        convert_as_inline: true,
        heading_allow_inline_images: heading_allows_inline_images(
            &heading_name,
            &handler.context.keep_inline_images_in,
        ),
        ..handler.context.clone()
    };
    let mut heading_text = String::new();
    walk_node(
        &heading_handle,
        handler.parser,
        &mut heading_text,
        crate::converter::block::container::HandlerContext::new(
            handler.options,
            &heading_context,
            handler.depth + 1,
            handler.dom_context,
        ),
    );
    let heading_text = heading_text.trim();
    if heading_text.is_empty() {
        return false;
    }
    let mut link = String::new();
    append_link(
        &mut link,
        data,
        &escape_link_label(heading_text),
        &data.raw_text,
        handler.options,
        handler.context,
    );
    push_heading(handler.output, handler.context, handler.options, level, &link);
    true
}

fn build_label(data: &LinkData<'_>, handler: &HandlerContext<'_>) -> String {
    if data.emit_blocks_separately {
        return walk_label(&data.inline_children, false, data, handler);
    }
    if data.saw_block {
        let content = walk_label_content(&data.children, true, data, handler);
        return if content.trim().is_empty() {
            normalize_link_label(&data.inline_label)
        } else {
            normalize_link_label(&content)
        };
    }
    walk_label(&data.children, false, data, handler)
}

fn walk_label(
    children: &[tl::NodeHandle],
    convert_as_inline: bool,
    data: &LinkData<'_>,
    handler: &HandlerContext<'_>,
) -> String {
    normalize_link_label(&walk_label_content(children, convert_as_inline, data, handler))
}

fn walk_label_content(
    children: &[tl::NodeHandle],
    merge_child_spacing: bool,
    data: &LinkData<'_>,
    handler: &HandlerContext<'_>,
) -> String {
    let link_context = Context {
        inline_depth: handler.context.inline_depth + 1,
        in_link: true,
        convert_as_inline: handler.context.convert_as_inline || merge_child_spacing,
        link_allow_inline_images: data.link_allow_inline_images,
        ..handler.context.clone()
    };
    let mut content = String::new();
    if !merge_child_spacing {
        for child in children {
            walk_node(
                child,
                handler.parser,
                &mut content,
                crate::converter::block::container::HandlerContext::new(
                    handler.options,
                    &link_context,
                    handler.depth + 1,
                    handler.dom_context,
                ),
            );
        }
        return content;
    }
    for child in children {
        let mut child_output = String::new();
        walk_node(
            child,
            handler.parser,
            &mut child_output,
            crate::converter::block::container::HandlerContext::new(
                handler.options,
                &link_context,
                handler.depth + 1,
                handler.dom_context,
            ),
        );
        if merge_child_spacing && needs_label_space(&content, &child_output) {
            content.push(' ');
        }
        content.push_str(&child_output);
    }
    content
}

fn needs_label_space(content: &str, child: &str) -> bool {
    !child.trim().is_empty()
        && !content.is_empty()
        && !content.chars().last().is_none_or(char::is_whitespace)
        && !child.chars().next().is_none_or(char::is_whitespace)
}

fn apply_label_fallbacks(data: &LinkData<'_>, label: &mut String, handler: &HandlerContext<'_>) {
    // ~keep Deferred table text must not be duplicated into the label (#490).
    if !data.emit_blocks_separately && label.is_empty() && !data.raw_text.is_empty() {
        *label = normalize_link_label(&data.raw_text);
    }
    let drop_link = label.is_empty() && handler.context.inline_data_replaced.get();
    if label.is_empty() && !data.href.is_empty() && !data.children.is_empty() && !drop_link && !data.href_addr_dropped {
        *label = text::escape(
            &data.href,
            handler.options.escape_misc,
            handler.options.escape_asterisks,
            handler.options.escape_underscores,
            handler.options.escape_ascii,
        )
        .into_owned();
    }
    if label == "^" && data.href.starts_with('#') {
        *label = "↑".to_string();
    }
}

#[cfg_attr(not(feature = "visitor"), allow(unused_variables))]
fn emit_link(
    tag: &tl::HTMLTag<'_>,
    data: &LinkData<'_>,
    label: &str,
    drop_link: bool,
    handler: &mut HandlerContext<'_>,
) -> bool {
    if drop_link {
        return true;
    }
    #[cfg(feature = "visitor")]
    if let Some(visitor) = handler.context.visitor.clone() {
        return visit_link(tag, data, label, &visitor, handler);
    }
    write_link(handler.output, data, label, handler.options, handler.context);
    true
}

fn write_link(output: &mut String, data: &LinkData<'_>, label: &str, options: &ConversionOptions, context: &Context) {
    if data.href_addr_dropped {
        output.push_str(label);
        return;
    }
    append_link(output, data, &escape_link_label(label), label, options, context);
}

fn append_link(
    output: &mut String,
    data: &LinkData<'_>,
    escaped_label: &str,
    raw_text: &str,
    options: &ConversionOptions,
    context: &Context,
) {
    append_markdown_link_in_context(
        output,
        &MarkdownLink {
            label: escaped_label,
            href: &data.href,
            title: data.title.as_deref(),
            raw_text,
        },
        options,
        context.reference_collector.as_ref(),
        context.in_table_cell,
    );
}

#[cfg(feature = "visitor")]
fn visit_link(
    tag: &tl::HTMLTag<'_>,
    data: &LinkData<'_>,
    label: &str,
    visitor: &crate::visitor::VisitorHandle,
    handler: &mut HandlerContext<'_>,
) -> bool {
    use crate::visitor::{NodeContext, NodeType, VisitResult};

    let node_id = handler.node_handle.get_inner();
    let node_context = NodeContext::with_lazy_attributes(
        NodeType::Link,
        Cow::Borrowed("a"),
        tag,
        handler.depth,
        handler.dom_context.get_sibling_index(node_id).unwrap_or(0),
        handler
            .dom_context
            .parent_tag_name(node_id, handler.parser)
            .map(Cow::Borrowed),
        true,
    );
    let result = visitor.lock().expect("visitor mutex poisoned").visit_link(
        &node_context,
        &data.href,
        label,
        data.title.as_deref(),
    );
    match result {
        VisitResult::Continue => write_link(handler.output, data, label, handler.options, handler.context),
        VisitResult::Custom(custom) => {
            if let Some(collector) = handler.context.structure_collector.as_ref() {
                collector.borrow_mut().replace_current_element(Some(&custom));
            }
            handler.output.push_str(&custom);
        }
        VisitResult::Skip => {
            if let Some(collector) = handler.context.structure_collector.as_ref() {
                collector.borrow_mut().replace_current_element(None);
            }
            return false;
        }
        VisitResult::Error(error) => {
            if let Some(collector) = handler.context.structure_collector.as_ref() {
                collector.borrow_mut().replace_current_element(None);
            }
            if handler.context.visitor_error.borrow().is_none() {
                *handler.context.visitor_error.borrow_mut() = Some(error);
            }
        }
        VisitResult::PreserveHtml => {
            let html = serialize_node(handler.node_handle, handler.parser);
            if let Some(collector) = handler.context.structure_collector.as_ref() {
                collector.borrow_mut().replace_current_element(Some(&html));
            }
            handler.output.push_str(&html);
            return false;
        }
    }
    true
}

#[cfg(feature = "metadata")]
fn record_link_metadata(tag: &tl::HTMLTag<'_>, data: &LinkData<'_>, label: &str, context: &Context) {
    if !context.metadata_wants_links {
        return;
    }
    let Some(collector) = context.metadata_collector.as_ref() else {
        return;
    };
    let rel = tag
        .attributes()
        .get("rel")
        .flatten()
        .map(|value| value.as_utf8_str().to_string());
    let attributes = tag
        .attributes()
        .iter()
        .filter(|(key, _)| key.as_ref() != "href")
        .map(|(key, value)| {
            (
                key.to_string(),
                value.map(|value| value.to_string()).unwrap_or_default(),
            )
        })
        .collect::<BTreeMap<_, _>>();
    collector.borrow_mut().add_link(
        data.href.clone(),
        label.to_string(),
        data.title.as_deref().map(str::to_string),
        rel,
        attributes,
    );
}

fn walk_handles_to_output(children: impl Iterator<Item = tl::NodeHandle>, handler: &mut HandlerContext<'_>) {
    for child in children {
        walk_node(
            &child,
            handler.parser,
            handler.output,
            crate::converter::block::container::HandlerContext::new(
                handler.options,
                handler.context,
                handler.depth + 1,
                handler.dom_context,
            ),
        );
    }
}

/// Partition an anchor's DIRECT children into (inline, block) using `node_is_block_level`
/// (issue #490).
///
/// ~keep Deliberately direct children only, not `collect_link_label_text`'s topmost block
/// ~keep *descendants* (which can sit several inline wrappers deep) -- every byte of the
/// ~keep anchor's content must land in exactly one of the two halves, which is also what
/// ~keep catches a table buried under a block wrapper (`<a><div><table>...`).
fn partition_link_children(
    children: &[tl::NodeHandle],
    parser: &tl::Parser,
    dom_ctx: &DomContext,
) -> (Vec<tl::NodeHandle>, Vec<tl::NodeHandle>) {
    children
        .iter()
        .copied()
        .partition(|child| !node_is_block_level(child, parser, dom_ctx))
}

/// Decide whether an anchor's deferred (direct block) children should be rendered as
/// separate blocks after the link, instead of being walked into the inline label
/// (issue #490).
///
/// ~keep A wrapped `<table>` crushed a whole GFM table into the link label: the label would
/// ~keep contain literal `|`/`\n` that either get escaped into noise or, unescaped, corrupt
/// ~keep the OUTER row on reparse. Emitting the table as a separate block after the link is
/// ~keep the only shape that round-trips. Gated on an actual `<table>` inside a deferred
/// ~keep subtree so the common case (a `<p>`/`<div>`-only anchor) pays nothing and behaves
/// ~keep exactly as before. `!ctx.convert_as_inline` and `!ctx.in_heading` are load-bearing,
/// ~keep not polish: a heading's `normalize_heading_text` folds `\n` to spaces, so a block
/// ~keep table inside a heading is no better than the crushed-label bug; an inline context (a
/// ~keep data cell's own label, or this link nested inside an outer link's label) has nowhere
/// ~keep to put a deferred block at all.
///
/// ~keep A layout cell is the exception among inline contexts (issue #503): it converts as
/// ~keep inline only because its row becomes one list item, and that item's text already
/// ~keep holds a bare nested table. Refusing there sent the table through the label, where
/// ~keep `escape_link_label` turned every inner link into `\[One\](/one)` text.
fn should_defer_table_blocks(
    ctx: &Context,
    deferred: &[tl::NodeHandle],
    parser: &tl::Parser,
    dom_ctx: &DomContext,
) -> bool {
    (!ctx.convert_as_inline || ctx.in_layout_cell)
        && !ctx.in_heading
        && deferred.iter().any(|handle| subtree_has_table(handle, parser, dom_ctx))
}

/// Short-circuiting DFS: does `handle`'s subtree (itself or any descendant) contain a
/// `<table>`?
///
/// ~keep Checked only against a deferred block child of an `<a>` (issue #490), so the cost
/// ~keep is paid only when the anchor actually has a block child, and the walk stops at the
/// ~keep first `<table>` found regardless of subtree size.
#[allow(clippy::trivially_copy_pass_by_ref)]
fn subtree_has_table(handle: &tl::NodeHandle, parser: &tl::Parser, dom_ctx: &DomContext) -> bool {
    let Some(tl::Node::Tag(tag)) = handle.get(parser) else {
        return false;
    };
    if normalized_tag_name(tag.name().as_utf8_str()) == "table" {
        return true;
    }
    if let Some(children) = dom_ctx.children_of(handle.get_inner()) {
        children.iter().any(|child| subtree_has_table(child, parser, dom_ctx))
    } else {
        tag.children()
            .top()
            .iter()
            .any(|child| subtree_has_table(child, parser, dom_ctx))
    }
}