diff --git a/src/renderer/html_handlebars/hbs_renderer.rs b/src/renderer/html_handlebars/hbs_renderer.rs index 0945b5ea..a8bfa8eb 100644 --- a/src/renderer/html_handlebars/hbs_renderer.rs +++ b/src/renderer/html_handlebars/hbs_renderer.rs @@ -214,36 +214,33 @@ fn make_data(book: &MDBook) -> Result Ok(data) } -fn build_header_links(mut html: String) -> String { - for header in &["h1", "h2", "h3", "h4", "h5"] { - let regex = Regex::new(&format!("<{h}>(.*?)", h=header)).unwrap(); +fn build_header_links(html: String) -> String { + let regex = Regex::new(r"(.*?)").unwrap(); - html = regex.replace_all(&html, |caps: &Captures| { - let text = &caps[1]; - let mut id = text.to_string(); - let repl_sub = vec!["", "", "", "", - "", "", - "<", ">", "&", "'", """]; - for sub in repl_sub { - id = id.replace(sub, ""); - } - let id = id.chars().filter_map(|c| { - if c.is_alphanumeric() || c == '-' || c == '_' { - if c.is_ascii() { - Some(c.to_ascii_lowercase()) - } else { - Some(c) - } - } else if c.is_whitespace() && c.is_ascii() { - Some('-') + regex.replace_all(&html, |caps: &Captures| { + let level = &caps[1]; + let text = &caps[2]; + let mut id = text.to_string(); + let repl_sub = vec!["", "", "", "", + "", "", + "<", ">", "&", "'", """]; + for sub in repl_sub { + id = id.replace(sub, ""); + } + let id = id.chars().filter_map(|c| { + if c.is_alphanumeric() || c == '-' || c == '_' { + if c.is_ascii() { + Some(c.to_ascii_lowercase()) } else { - None + Some(c) } - }).collect::(); + } else if c.is_whitespace() && c.is_ascii() { + Some('-') + } else { + None + } + }).collect::(); - format!("<{h}>{text}", h=header, id=id, text=text) - }).into_owned(); - } - - html + format!("{text}", level=level, id=id, text=text) + }).into_owned() }