From 58bc92d380c50d215d3bf6801752f7260d5188b4 Mon Sep 17 00:00:00 2001 From: Abdo Date: Wed, 10 Nov 2021 00:41:44 +0300 Subject: [PATCH] Strip HTML tags from anchor IDs --- src/utils/mod.rs | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/src/utils/mod.rs b/src/utils/mod.rs index 2352517a13..8898e480cc 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -48,19 +48,11 @@ pub fn id_from_content(content: &str) -> String { let mut content = content.to_string(); // Skip any tags or html-encoded stuff - const REPL_SUB: &[&str] = &[ - "", - "", - "", - "", - "", - "", - "<", - ">", - "&", - "'", - """, - ]; + lazy_static! { + static ref HTML: Regex = Regex::new(r"(<.*?>)").unwrap(); + } + content = HTML.replace_all(&content, "").into(); + const REPL_SUB: &[&str] = &["<", ">", "&", "'", """]; for sub in REPL_SUB { content = content.replace(sub, ""); } @@ -417,6 +409,10 @@ more text with spaces ); assert_eq!(id_from_content("## **Bold** title"), "bold-title"); assert_eq!(id_from_content("## `Code` title"), "code-title"); + assert_eq!( + id_from_content("## title foo"), + "title-foo" + ); } #[test]