diff --git a/src/renderer/html_handlebars/hbs_renderer.rs b/src/renderer/html_handlebars/hbs_renderer.rs index 4165b5447a..ffa2a922f9 100644 --- a/src/renderer/html_handlebars/hbs_renderer.rs +++ b/src/renderer/html_handlebars/hbs_renderer.rs @@ -831,7 +831,7 @@ fn make_data( Ok(data) } -/// Goes through part of the rendered print page HTML, +/// Go through the rendered print page HTML, /// add path id prefix to all the elements id as well as footnote links. fn build_print_element_id(html: &str, path_id: &str) -> String { static ALL_ID: Lazy = Lazy::new(|| Regex::new(r#"(<[^>]*?id=")([^"]+?)""#).unwrap()); @@ -842,27 +842,13 @@ fn build_print_element_id(html: &str, path_id: &str) -> String { .unwrap() }); - if path_id.is_empty() { - return html.to_string(); - } - - let temp_html = ALL_ID - .replace_all(html, |caps: &Captures<'_>| { - let mut fixed = String::new(); - fixed.push_str(&path_id); - fixed.push_str("-"); - fixed.push_str(&caps[2]); - format!("{}{}\"", &caps[1], fixed) - }) - .into_owned(); + let temp_html = ALL_ID.replace_all(html, |caps: &Captures<'_>| { + format!("{}{}-{}\"", &caps[1], path_id, &caps[2]) + }); FOOTNOTE_ID .replace_all(&temp_html, |caps: &Captures<'_>| { - let mut fixed = String::new(); - fixed.push_str(&path_id); - fixed.push_str("-"); - fixed.push_str(&caps[2]); - format!("{}{}\"", &caps[1], fixed) + format!("{}{}-{}\"", &caps[1], path_id, &caps[2]) }) .into_owned() } diff --git a/src/utils/mod.rs b/src/utils/mod.rs index b122e506b8..3fb7d6c8ba 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -152,7 +152,8 @@ fn adjust_links<'a>( static HTML_MD_LINK: Lazy = Lazy::new(|| Regex::new(r"(?P.*)\.(html|md)(?P#.*)?").unwrap()); - fn add_base(mut fixed_link: String, path: Option<&Path>) -> String { + fn add_base(path: Option<&Path>) -> String { + let mut fixed_link = String::new(); if let Some(path) = path { let base = path .parent() @@ -240,8 +241,11 @@ fn adjust_links<'a>( if SCHEME_LINK.is_match(&dest) { return dest; } + + let closure = |path: Option<&Path>| add_base(path); + // This is a relative link, adjust it as necessary. - let mut fixed_link = add_base(String::new(), path); + let mut fixed_link = closure(path); fixed_link.push_str(&dest); CowStr::from(fixed_link) } @@ -276,8 +280,10 @@ fn adjust_links<'a>( return dest; } + let closure = |path: Option<&Path>| add_base(path); + // This is a relative link, adjust it as necessary. - let mut fixed_link = add_base(String::new(), path); + let mut fixed_link = closure(path); if let Some(caps) = HTML_MD_LINK.captures(&dest) { fixed_link.push_str(&caps["link"]); @@ -321,12 +327,10 @@ fn adjust_links<'a>( static IMG_LINK: Lazy = Lazy::new(|| Regex::new(r#"(]*?src=")([^"]+?)""#).unwrap()); - let temp_html = IMG_LINK - .replace_all(&html, |caps: ®ex::Captures<'_>| { - let fixed = fix_resource_links(caps[2].into(), path); - format!("{}{}\"", &caps[1], fixed) - }) - .into_owned(); + let temp_html = IMG_LINK.replace_all(&html, |caps: ®ex::Captures<'_>| { + let fixed = fix_resource_links(caps[2].into(), path); + format!("{}{}\"", &caps[1], fixed) + }); A_LINK .replace_all(&temp_html, |caps: ®ex::Captures<'_>| {