From 1b0faddfa8b1ffc3d764ad46b078ffb92a5352fa Mon Sep 17 00:00:00 2001 From: divyanshu2074 <43274510+divyanshu2074@users.noreply.github.com> Date: Sun, 25 Oct 2026 20:01:01 +0530 Subject: [PATCH] fix(formats): ignore block and inline HTML comments in markdown import (#2225) --- crates/formats/src/markdown.rs | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/crates/formats/src/markdown.rs b/crates/formats/src/markdown.rs index bf1987701..da6843307 100644 --- a/crates/formats/src/markdown.rs +++ b/crates/formats/src/markdown.rs @@ -5,7 +5,7 @@ //! bottom-border paragraph), bullet and ordered lists (nested by indentation), GFM pipe tables, //! and inlines: emphasis/strong (the CommonMark delimiter algorithm), `~~strike~~`, code spans, //! inline links, autolinks, images (`data:` URIs become pictures, others their alt text), -//! backslash escapes, entities, `
` and `` anchors. +//! backslash escapes, entities, HTML comments (``), `
` and `
` anchors. //! //! Export writes the same constructs; pictures become `![alt](media-name)` placeholders. @@ -351,6 +351,7 @@ fn interrupts(l: &str) -> bool { || thematic_break(t) || fence(t).is_some() || t.starts_with('>') + || t.starts_with("") { + break; + } + } + continue; + } if t.starts_with('>') { flush_para(&mut para, &mut out, depth, refs); let mut inner: Vec = Vec::new(); @@ -1016,6 +1031,12 @@ fn tokenize(s: &str, base: &Fmt, depth: usize, refs: &Refs) -> Vec { } } b'<' => { + if let Some(pos) = s.get(i..).and_then(|r| r.strip_prefix("")) { + flush!(); + i += 4 + pos + 3; + text_start = i; + continue; + } let rest = s.get(i + 1..).unwrap_or(""); let end = within(rest, MAX_TAG).find('>'); let inner = end.and_then(|e| rest.get(..e)).unwrap_or(""); @@ -2052,4 +2073,13 @@ mod tests { assert_eq!(p3.text(), "one item"); assert!(p3.list.unwrap().ordered); } + + #[test] + fn html_comments_are_ignored_in_blocks_and_inlines() { + let md = "\n\nHello world!\n\n\n\nAfter comment."; + let flow = parse(md); + assert_eq!(flow.blocks.len(), 2); + let texts: Vec = flow.blocks.iter().filter_map(|b| if let FBlock::Para(p) = b { Some(p.text()) } else { None }).collect(); + assert_eq!(texts, ["Hello world!", "After comment."]); + } }