@@ -72,14 +72,20 @@ impl HtmlHandlebars {
72
72
if base. ends_with ( ".md" ) {
73
73
base. replace_range ( base. len ( ) - 3 .., "" ) ;
74
74
}
75
- & base. replace ( "/" , "-" ) . replace ( "\\ " , "-" )
75
+ & base
76
+ . replace ( "/" , "-" )
77
+ . replace ( "\\ " , "-" )
78
+ . to_ascii_lowercase ( )
76
79
} ;
77
80
78
81
// We have to build header links in advance so that we can know the ranges
79
82
// for the headers in one page.
80
83
// Insert a dummy div to make sure that we can locate the specific page.
81
84
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
+ ) ) ;
83
89
84
90
// Update the context with data for this file
85
91
let ctx_path = path
@@ -772,6 +778,26 @@ fn make_data(
772
778
Ok ( data)
773
779
}
774
780
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
+
775
801
/// Goes through the rendered HTML, making sure all header tags have
776
802
/// an anchor respectively so people can link to sections directly.
777
803
fn build_header_links ( html : & str , path_id : Option < & str > ) -> String {
0 commit comments