Skip to content

Commit 7fb9b46

Browse files
committed
Make print page (print.html) links link to anchors on the print page
Let all the anchors id on the print page to have a path id prefix to help locate. e.g. bar/foo.md#abc -> #bar-foo-abc Also append a dummy div to the start of the original page to make sure that original page links without an anchor can also be located. Fix to remove all the `./` in the normalized path id so that for "./foo/bar.html#abc" we still get "#foo-bar-abc" Add support for redirect link anchors in print page so that anchors can also be redirected, also handle URL redirect links on print page Handle all the elements id to add a path prefix, also make path id to all be the lower case Fix for print page footnote links by adding the path id prefix Signed-off-by: Hollow Man <[email protected]>
1 parent b2548f6 commit 7fb9b46

File tree

3 files changed

+389
-70
lines changed

3 files changed

+389
-70
lines changed

crates/mdbook-html/src/html_handlebars/hbs_renderer.rs

Lines changed: 86 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use mdbook_core::book::{Book, BookItem};
88
use mdbook_core::config::{BookConfig, Code, Config, HtmlConfig, Playground, RustEdition};
99
use mdbook_core::utils;
1010
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};
1212
use mdbook_renderer::{RenderContext, Renderer};
1313
use regex::{Captures, Regex};
1414
use serde_json::json;
@@ -58,16 +58,38 @@ impl HtmlHandlebars {
5858

5959
let content = render_markdown(&ch.content, ctx.html_config.smart_punctuation());
6060

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+
);
6367
if !ctx.is_index && ctx.html_config.print.page_break {
6468
// Add page break between chapters
6569
// 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
6670
// Add both two CSS properties because of the compatibility issue
6771
print_content
6872
.push_str(r#"<div style="break-before: page; page-break-before: always;"></div>"#);
6973
}
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+
));
7193

7294
// Update the context with data for this file
7395
let ctx_path = path
@@ -219,7 +241,23 @@ impl HtmlHandlebars {
219241
code_config: &Code,
220242
edition: Option<RustEdition>,
221243
) -> String {
222-
let rendered = build_header_links(&rendered);
244+
let rendered = build_header_links(&rendered, None);
245+
let rendered = self.post_process_common(rendered, &playground_config, code_config, edition);
246+
247+
rendered
248+
}
249+
250+
/// Applies some post-processing to the HTML to apply some adjustments.
251+
///
252+
/// This common function is used for both normal chapters (via
253+
/// `post_process`) and the combined print page.
254+
fn post_process_common(
255+
&self,
256+
rendered: String,
257+
playground_config: &Playground,
258+
code_config: &Code,
259+
edition: Option<RustEdition>,
260+
) -> String {
223261
let rendered = fix_code_blocks(&rendered);
224262
let rendered = add_playground_pre(&rendered, playground_config, edition);
225263
let rendered = hide_lines(&rendered, code_config);
@@ -474,7 +512,7 @@ impl Renderer for HtmlHandlebars {
474512
debug!("Render template");
475513
let rendered = handlebars.render("index", &data)?;
476514

477-
let rendered = self.post_process(
515+
let rendered = self.post_process_common(
478516
rendered,
479517
&html_config.playground,
480518
&html_config.code,
@@ -667,9 +705,35 @@ fn make_data(
667705
Ok(data)
668706
}
669707

708+
/// Go through the rendered print page HTML,
709+
/// add path id prefix to all the elements id as well as footnote links.
710+
fn build_print_element_id(html: &str, print_page_id: &str) -> String {
711+
static ALL_ID: LazyLock<Regex> =
712+
LazyLock::new(|| Regex::new(r#"(<[^>]*?id=")([^"]+?)""#).unwrap());
713+
static FOOTNOTE_ID: LazyLock<Regex> = LazyLock::new(|| {
714+
Regex::new(
715+
r##"(<sup [^>]*?class="footnote-reference"[^>]*?>[^<]*?<a [^>]*?href="#)([^"]+?)""##,
716+
)
717+
.unwrap()
718+
});
719+
720+
let temp_html = ALL_ID.replace_all(html, |caps: &Captures<'_>| {
721+
format!("{}{}-{}\"", &caps[1], print_page_id, &caps[2])
722+
});
723+
724+
FOOTNOTE_ID
725+
.replace_all(&temp_html, |caps: &Captures<'_>| {
726+
format!("{}{}-{}\"", &caps[1], print_page_id, &caps[2])
727+
})
728+
.into_owned()
729+
}
730+
670731
/// Goes through the rendered HTML, making sure all header tags have
671732
/// an anchor respectively so people can link to sections directly.
672-
fn build_header_links(html: &str) -> String {
733+
///
734+
/// `print_page_id` should be set to the print page ID prefix when adjusting the
735+
/// print page.
736+
fn build_header_links(html: &str, print_page_id: Option<&str>) -> String {
673737
static BUILD_HEADER_LINKS: LazyLock<Regex> = LazyLock::new(|| {
674738
Regex::new(r#"<h(\d)(?: id="([^"]+)")?(?: class="([^"]+)")?>(.*?)</h\d>"#).unwrap()
675739
});
@@ -698,21 +762,34 @@ fn build_header_links(html: &str) -> String {
698762
caps.get(2).map(|x| x.as_str().to_string()),
699763
caps.get(3).map(|x| x.as_str().to_string()),
700764
&mut id_counter,
765+
print_page_id,
701766
)
702767
})
703768
.into_owned()
704769
}
705770

706771
/// Insert a single link into a header, making sure each link gets its own
707772
/// unique ID by appending an auto-incremented number (if necessary).
773+
///
774+
/// For `print.html`, we will add a path id prefix.
708775
fn insert_link_into_header(
709776
level: usize,
710777
content: &str,
711778
id: Option<String>,
712779
classes: Option<String>,
713780
id_counter: &mut HashMap<String, usize>,
781+
print_page_id: Option<&str>,
714782
) -> String {
715-
let id = id.unwrap_or_else(|| utils::unique_id_from_content(content, id_counter));
783+
let id = if let Some(print_page_id) = print_page_id {
784+
let content_id = {
785+
#[allow(deprecated)]
786+
utils::id_from_content(content)
787+
};
788+
let with_prefix = format!("{} {}", print_page_id, content_id);
789+
id.unwrap_or_else(|| utils::unique_id_from_content(&with_prefix, id_counter))
790+
} else {
791+
id.unwrap_or_else(|| utils::unique_id_from_content(content, id_counter))
792+
};
716793
let classes = classes
717794
.map(|s| format!(" class=\"{s}\""))
718795
.unwrap_or_default();
@@ -1047,7 +1124,7 @@ mod tests {
10471124
];
10481125

10491126
for (src, should_be) in inputs {
1050-
let got = build_header_links(src);
1127+
let got = build_header_links(src, None);
10511128
assert_eq!(got, should_be);
10521129
}
10531130
}

0 commit comments

Comments
 (0)