@@ -8,7 +8,7 @@ use mdbook_core::book::{Book, BookItem, Chapter};
8
8
use mdbook_core:: config:: { BookConfig , Code , Config , HtmlConfig , Playground , RustEdition } ;
9
9
use mdbook_core:: utils;
10
10
use mdbook_core:: utils:: fs:: get_404_output_file;
11
- use mdbook_markdown:: { render_markdown, render_markdown_with_path } ;
11
+ use mdbook_markdown:: { render_markdown, render_markdown_with_path_and_redirects } ;
12
12
use mdbook_renderer:: { RenderContext , Renderer } ;
13
13
use regex:: { Captures , Regex } ;
14
14
use serde_json:: json;
@@ -58,16 +58,38 @@ impl HtmlHandlebars {
58
58
59
59
let content = render_markdown ( & ch. content , ctx. html_config . smart_punctuation ) ;
60
60
61
- let fixed_content =
62
- render_markdown_with_path ( & ch. content , ctx. html_config . smart_punctuation , Some ( path) ) ;
61
+ let printed_item = render_markdown_with_path_and_redirects (
62
+ & ch. content ,
63
+ ctx. html_config . smart_punctuation ,
64
+ Some ( path) ,
65
+ & ctx. html_config . redirect ,
66
+ ) ;
63
67
if prev_ch. is_some ( ) && ctx. html_config . print . page_break {
64
68
// Add page break between chapters
65
69
// See https://developer.mozilla.org/en-US/docs/Web/CSS/break-before and https://developer.mozilla.org/en-US/docs/Web/CSS/page-break-before
66
70
// Add both two CSS properties because of the compatibility issue
67
71
print_content
68
72
. push_str ( r#"<div style="break-before: page; page-break-before: always;"></div>"# ) ;
69
73
}
70
- print_content. push_str ( & fixed_content) ;
74
+ let print_page_id = {
75
+ let mut base = path. display ( ) . to_string ( ) ;
76
+ if base. ends_with ( ".md" ) {
77
+ base. truncate ( base. len ( ) - 3 ) ;
78
+ }
79
+ & base
80
+ . replace ( "/" , "-" )
81
+ . replace ( "\\ " , "-" )
82
+ . to_ascii_lowercase ( )
83
+ } ;
84
+
85
+ // We have to build header links in advance so that we can know the ranges
86
+ // for the headers in one page.
87
+ // Insert a dummy div to make sure that we can locate the specific page.
88
+ print_content. push_str ( & ( format ! ( r#"<div id="{print_page_id}"></div>"# ) ) ) ;
89
+ print_content. push_str ( & build_header_links (
90
+ & build_print_element_id ( & printed_item, & print_page_id) ,
91
+ Some ( print_page_id) ,
92
+ ) ) ;
71
93
72
94
// Update the context with data for this file
73
95
let ctx_path = path
@@ -238,7 +260,23 @@ impl HtmlHandlebars {
238
260
code_config : & Code ,
239
261
edition : Option < RustEdition > ,
240
262
) -> String {
241
- let rendered = build_header_links ( & rendered) ;
263
+ let rendered = build_header_links ( & rendered, None ) ;
264
+ let rendered = self . post_process_common ( rendered, & playground_config, code_config, edition) ;
265
+
266
+ rendered
267
+ }
268
+
269
+ /// Applies some post-processing to the HTML to apply some adjustments.
270
+ ///
271
+ /// This common function is used for both normal chapters (via
272
+ /// `post_process`) and the combined print page.
273
+ fn post_process_common (
274
+ & self ,
275
+ rendered : String ,
276
+ playground_config : & Playground ,
277
+ code_config : & Code ,
278
+ edition : Option < RustEdition > ,
279
+ ) -> String {
242
280
let rendered = fix_code_blocks ( & rendered) ;
243
281
let rendered = add_playground_pre ( & rendered, playground_config, edition) ;
244
282
let rendered = hide_lines ( & rendered, code_config) ;
@@ -496,7 +534,7 @@ impl Renderer for HtmlHandlebars {
496
534
debug ! ( "Render template" ) ;
497
535
let rendered = handlebars. render ( "index" , & data) ?;
498
536
499
- let rendered = self . post_process (
537
+ let rendered = self . post_process_common (
500
538
rendered,
501
539
& html_config. playground ,
502
540
& html_config. code ,
@@ -694,9 +732,35 @@ fn make_data(
694
732
Ok ( data)
695
733
}
696
734
735
+ /// Go through the rendered print page HTML,
736
+ /// add path id prefix to all the elements id as well as footnote links.
737
+ fn build_print_element_id ( html : & str , print_page_id : & str ) -> String {
738
+ static ALL_ID : LazyLock < Regex > =
739
+ LazyLock :: new ( || Regex :: new ( r#"(<[^>]*?id=")([^"]+?)""# ) . unwrap ( ) ) ;
740
+ static FOOTNOTE_ID : LazyLock < Regex > = LazyLock :: new ( || {
741
+ Regex :: new (
742
+ r##"(<sup [^>]*?class="footnote-reference"[^>]*?>[^<]*?<a [^>]*?href="#)([^"]+?)""## ,
743
+ )
744
+ . unwrap ( )
745
+ } ) ;
746
+
747
+ let temp_html = ALL_ID . replace_all ( html, |caps : & Captures < ' _ > | {
748
+ format ! ( "{}{}-{}\" " , & caps[ 1 ] , print_page_id, & caps[ 2 ] )
749
+ } ) ;
750
+
751
+ FOOTNOTE_ID
752
+ . replace_all ( & temp_html, |caps : & Captures < ' _ > | {
753
+ format ! ( "{}{}-{}\" " , & caps[ 1 ] , print_page_id, & caps[ 2 ] )
754
+ } )
755
+ . into_owned ( )
756
+ }
757
+
697
758
/// Goes through the rendered HTML, making sure all header tags have
698
759
/// an anchor respectively so people can link to sections directly.
699
- fn build_header_links ( html : & str ) -> String {
760
+ ///
761
+ /// `print_page_id` should be set to the print page ID prefix when adjusting the
762
+ /// print page.
763
+ fn build_header_links ( html : & str , print_page_id : Option < & str > ) -> String {
700
764
static BUILD_HEADER_LINKS : LazyLock < Regex > = LazyLock :: new ( || {
701
765
Regex :: new ( r#"<h(\d)(?: id="([^"]+)")?(?: class="([^"]+)")?>(.*?)</h\d>"# ) . unwrap ( )
702
766
} ) ;
@@ -725,21 +789,34 @@ fn build_header_links(html: &str) -> String {
725
789
caps. get ( 2 ) . map ( |x| x. as_str ( ) . to_string ( ) ) ,
726
790
caps. get ( 3 ) . map ( |x| x. as_str ( ) . to_string ( ) ) ,
727
791
& mut id_counter,
792
+ print_page_id,
728
793
)
729
794
} )
730
795
. into_owned ( )
731
796
}
732
797
733
798
/// Insert a single link into a header, making sure each link gets its own
734
799
/// unique ID by appending an auto-incremented number (if necessary).
800
+ ///
801
+ /// For `print.html`, we will add a path id prefix.
735
802
fn insert_link_into_header (
736
803
level : usize ,
737
804
content : & str ,
738
805
id : Option < String > ,
739
806
classes : Option < String > ,
740
807
id_counter : & mut HashMap < String , usize > ,
808
+ print_page_id : Option < & str > ,
741
809
) -> String {
742
- let id = id. unwrap_or_else ( || utils:: unique_id_from_content ( content, id_counter) ) ;
810
+ let id = if let Some ( print_page_id) = print_page_id {
811
+ let content_id = {
812
+ #[ allow( deprecated) ]
813
+ utils:: id_from_content ( content)
814
+ } ;
815
+ let with_prefix = format ! ( "{} {}" , print_page_id, content_id) ;
816
+ id. unwrap_or_else ( || utils:: unique_id_from_content ( & with_prefix, id_counter) )
817
+ } else {
818
+ id. unwrap_or_else ( || utils:: unique_id_from_content ( content, id_counter) )
819
+ } ;
743
820
let classes = classes
744
821
. map ( |s| format ! ( " class=\" {s}\" " ) )
745
822
. unwrap_or_default ( ) ;
@@ -1122,7 +1199,7 @@ mod tests {
1122
1199
] ;
1123
1200
1124
1201
for ( src, should_be) in inputs {
1125
- let got = build_header_links ( src) ;
1202
+ let got = build_header_links ( src, None ) ;
1126
1203
assert_eq ! ( got, should_be) ;
1127
1204
}
1128
1205
}
0 commit comments