Skip to content

Commit 857acb9

Browse files
authored
Merge pull request #1683 from abdnh/strip-html-from-anchor-ids
Strip HTML tags from anchor IDs
2 parents 2ddcb43 + 58bc92d commit 857acb9

File tree

1 file changed

+9
-13
lines changed

1 file changed

+9
-13
lines changed

src/utils/mod.rs

+9-13
Original file line numberDiff line numberDiff line change
@@ -48,19 +48,11 @@ pub fn id_from_content(content: &str) -> String {
4848
let mut content = content.to_string();
4949

5050
// Skip any tags or html-encoded stuff
51-
const REPL_SUB: &[&str] = &[
52-
"<em>",
53-
"</em>",
54-
"<code>",
55-
"</code>",
56-
"<strong>",
57-
"</strong>",
58-
"&lt;",
59-
"&gt;",
60-
"&amp;",
61-
"&#39;",
62-
"&quot;",
63-
];
51+
lazy_static! {
52+
static ref HTML: Regex = Regex::new(r"(<.*?>)").unwrap();
53+
}
54+
content = HTML.replace_all(&content, "").into();
55+
const REPL_SUB: &[&str] = &["&lt;", "&gt;", "&amp;", "&#39;", "&quot;"];
6456
for sub in REPL_SUB {
6557
content = content.replace(sub, "");
6658
}
@@ -351,6 +343,10 @@ more text with spaces
351343
);
352344
assert_eq!(id_from_content("## **Bold** title"), "bold-title");
353345
assert_eq!(id_from_content("## `Code` title"), "code-title");
346+
assert_eq!(
347+
id_from_content("## title <span dir=rtl>foo</span>"),
348+
"title-foo"
349+
);
354350
}
355351

356352
#[test]

0 commit comments

Comments
 (0)