Skip to content

A fast file manager for Windows and macOS that never freezes: tabs, dual panes, archives as folders, Quick Look-style previews. A File Explorer and Finder alternative written in Rust.

Markdown preview: a heading inside an HTML link cannot cut the text out of range

denfry29abbf2db1e2
diff --git a/crates/rf-app/src/look/viewers/markdown.rs b/crates/rf-app/src/look/viewers/markdown.rs
index 068ecb18..5d3e1bab 100644
--- a/crates/rf-app/src/look/viewers/markdown.rs
+++ b/crates/rf-app/src/look/viewers/markdown.rs
@@ -126,8 +126,10 @@ fn strip_tags(line: &str, in_comment: &mut bool) -> String {
match (name.as_str(), closing) {
("a", false) => link = attr(tag, "href").map(|h| (out.len(), h)),
("a", true) => {
- if let Some((at, href)) = link.take() {
- let text = out[at..].trim().to_string();
+ // (a link whose start is gone, a heading's marker having replaced it, stays plain text)
+ if let Some((at, href)) = link.take()
+ && let Some(text) = out.get(at..).map(|t| t.trim().to_string())
+ {
out.truncate(at);
if !text.is_empty() {
out.push_str(&format!("[{text}]({href})"));
@@ -139,6 +141,8 @@ fn strip_tags(line: &str, in_comment: &mut bool) -> String {
("img", _) => out.push_str(&attr(tag, "alt").unwrap_or_default()),
(h, false) if h.len() == 2 && h.starts_with('h') && h.as_bytes()[1].is_ascii_digit() && out.trim().is_empty() => {
out.clear();
+ // (a link opened before the heading began has lost its start: its text stays plain)
+ link = None;
out.push_str(&"#".repeat(usize::from(h.as_bytes()[1] - b'0')));
out.push(' ');
}
@@ -234,6 +238,17 @@ mod tests {
assert_eq!(tame(md), "```html\n<p>kept</p>\n```\n\nAfter\n\n <div>indented: code</div>\n\na < b and c > d");
}
+ #[test]
+ fn a_heading_inside_a_link_does_not_cut_a_letter_in_two() {
+ // the link's start was at byte 3 of " "; the heading's "# " is 2 bytes, and "Я" is 2 bytes more
+ assert_eq!(tame("<p> <a href=\"x\"><h1>Я</h1></a></p>
+"), "# Я
+");
+ assert_eq!(tame("<div><a href=\"x\"> <h2>t</h2></a></div>
+"), "## t
+");
+ }
+
#[test]
fn a_tag_under_a_paragraph_line_is_part_of_the_paragraph_unless_it_is_a_block_tag() {
assert_eq!(tame("line\n<a href=\"x\">y</a>\n"), "line\n<a href=\"x\">y</a>\n");