Skip to content

Commit 67cd8ae

Browse files
committed
Handle all the elements id to add a path prefix
Also make path id to all be the lower case Signed-off-by: Hollow Man <[email protected]>
1 parent 2bca624 commit 67cd8ae

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

src/renderer/html_handlebars/hbs_renderer.rs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,20 @@ impl HtmlHandlebars {
7272
if base.ends_with(".md") {
7373
base.replace_range(base.len() - 3.., "");
7474
}
75-
&base.replace("/", "-").replace("\\", "-")
75+
&base
76+
.replace("/", "-")
77+
.replace("\\", "-")
78+
.to_ascii_lowercase()
7679
};
7780

7881
// We have to build header links in advance so that we can know the ranges
7982
// for the headers in one page.
8083
// Insert a dummy div to make sure that we can locate the specific page.
8184
print_content.push_str(&(format!(r#"<div id="{}"></div>"#, &path_id)));
82-
print_content.push_str(&build_header_links(&fixed_content, Some(path_id)));
85+
print_content.push_str(&build_header_links(
86+
&build_print_element_id(&fixed_content, &path_id),
87+
Some(path_id),
88+
));
8389

8490
// Update the context with data for this file
8591
let ctx_path = path
@@ -772,6 +778,26 @@ fn make_data(
772778
Ok(data)
773779
}
774780

781+
/// Goes through part of the rendered print page HTML,
782+
/// add path id prefix to all the elements id.
783+
fn build_print_element_id(html: &str, path_id: &str) -> String {
784+
let regex = Regex::new(r#"(<[^>]*?id=")([^"]+?)""#).unwrap();
785+
786+
if path_id.is_empty() {
787+
return html.to_string();
788+
}
789+
790+
regex
791+
.replace_all(html, |caps: &Captures<'_>| {
792+
let mut fixed = String::new();
793+
fixed.push_str(&path_id);
794+
fixed.push_str("-");
795+
fixed.push_str(&caps[2]);
796+
format!("{}{}\"", &caps[1], fixed)
797+
})
798+
.into_owned()
799+
}
800+
775801
/// Goes through the rendered HTML, making sure all header tags have
776802
/// an anchor respectively so people can link to sections directly.
777803
fn build_header_links(html: &str, path_id: Option<&str>) -> String {

src/utils/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,8 @@ pub fn normalize_path_id(mut path: String) -> String {
100100
path = path
101101
.replace("/", "-")
102102
.replace(".html#", "-")
103-
.replace("#", "-");
103+
.replace("#", "-")
104+
.to_ascii_lowercase();
104105
if path.ends_with(".html") {
105106
path.replace_range(path.len() - 5.., "");
106107
}

0 commit comments

Comments
 (0)