Skip to content

Commit a770c57

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 <hollowman@opensuse.org>
1 parent 6a41fe3 commit a770c57

File tree

9 files changed

+391
-84
lines changed

9 files changed

+391
-84
lines changed

crates/mdbook-core/src/utils/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ pub fn normalize_id(content: &str) -> String {
5555

5656
/// Generate an ID for use with anchors which is derived from a "normalised"
5757
/// string.
58-
fn id_from_content(content: &str) -> String {
58+
pub fn id_from_content(content: &str) -> String {
5959
let mut content = content.to_string();
6060

6161
// Skip any tags or html-encoded stuff

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

Lines changed: 76 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,25 @@ impl HtmlHandlebars {
6868
print_content
6969
.push_str(r#"<div style="break-before: page; page-break-before: always;"></div>"#);
7070
}
71-
print_content.push_str(&fixed_content);
71+
let print_page_id = {
72+
let mut base = path.display().to_string();
73+
if base.ends_with(".md") {
74+
base.truncate(base.len() - 3);
75+
}
76+
&base
77+
.replace("/", "-")
78+
.replace("\\", "-")
79+
.to_ascii_lowercase()
80+
};
81+
82+
// We have to build header links in advance so that we can know the ranges
83+
// for the headers in one page.
84+
// Insert a dummy div to make sure that we can locate the specific page.
85+
print_content.push_str(&(format!(r#"<div id="{print_page_id}"></div>"#)));
86+
print_content.push_str(&build_header_links(
87+
&build_print_element_id(&fixed_content, &print_page_id),
88+
Some(print_page_id),
89+
));
7290

7391
// Update the context with data for this file
7492
let ctx_path = path
@@ -238,7 +256,23 @@ impl HtmlHandlebars {
238256
code_config: &Code,
239257
edition: Option<RustEdition>,
240258
) -> String {
241-
let rendered = build_header_links(&rendered);
259+
let rendered = build_header_links(&rendered, None);
260+
let rendered = self.post_process_common(rendered, &playground_config, code_config, edition);
261+
262+
rendered
263+
}
264+
265+
/// Applies some post-processing to the HTML to apply some adjustments.
266+
///
267+
/// This common function is used for both normal chapters (via
268+
/// `post_process`) and the combined print page.
269+
fn post_process_common(
270+
&self,
271+
rendered: String,
272+
playground_config: &Playground,
273+
code_config: &Code,
274+
edition: Option<RustEdition>,
275+
) -> String {
242276
let rendered = fix_code_blocks(&rendered);
243277
let rendered = add_playground_pre(&rendered, playground_config, edition);
244278
let rendered = hide_lines(&rendered, code_config);
@@ -490,7 +524,7 @@ impl Renderer for HtmlHandlebars {
490524
debug!("Render template");
491525
let rendered = handlebars.render("index", &data)?;
492526

493-
let rendered = self.post_process(
527+
let rendered = self.post_process_common(
494528
rendered,
495529
&html_config.playground,
496530
&html_config.code,
@@ -691,9 +725,32 @@ fn make_data(
691725
Ok(data)
692726
}
693727

728+
/// Go through the rendered print page HTML,
729+
/// add path id prefix to all the elements id as well as footnote links.
730+
fn build_print_element_id(html: &str, print_page_id: &str) -> String {
731+
static_regex!(ALL_ID, r#"(<[^>]*?id=")([^"]+?)""#);
732+
static_regex!(
733+
FOOTNOTE_ID,
734+
r##"(<sup [^>]*?class="footnote-reference"[^>]*?>[^<]*?<a [^>]*?href="#)([^"]+?)""##
735+
);
736+
737+
let temp_html = ALL_ID.replace_all(html, |caps: &Captures<'_>| {
738+
format!("{}{}-{}\"", &caps[1], print_page_id, &caps[2])
739+
});
740+
741+
FOOTNOTE_ID
742+
.replace_all(&temp_html, |caps: &Captures<'_>| {
743+
format!("{}{}-{}\"", &caps[1], print_page_id, &caps[2])
744+
})
745+
.into_owned()
746+
}
747+
694748
/// Goes through the rendered HTML, making sure all header tags have
695749
/// an anchor respectively so people can link to sections directly.
696-
fn build_header_links(html: &str) -> String {
750+
///
751+
/// `print_page_id` should be set to the print page ID prefix when adjusting the
752+
/// print page.
753+
fn build_header_links(html: &str, print_page_id: Option<&str>) -> String {
697754
static_regex!(
698755
BUILD_HEADER_LINKS,
699756
r#"<h(\d)(?: id="([^"]+)")?(?: class="([^"]+)")?>(.*?)</h\d>"#
@@ -723,21 +780,34 @@ fn build_header_links(html: &str) -> String {
723780
caps.get(2).map(|x| x.as_str().to_string()),
724781
caps.get(3).map(|x| x.as_str().to_string()),
725782
&mut id_counter,
783+
print_page_id,
726784
)
727785
})
728786
.into_owned()
729787
}
730788

731789
/// Insert a single link into a header, making sure each link gets its own
732790
/// unique ID by appending an auto-incremented number (if necessary).
791+
///
792+
/// For `print.html`, we will add a path id prefix.
733793
fn insert_link_into_header(
734794
level: usize,
735795
content: &str,
736796
id: Option<String>,
737797
classes: Option<String>,
738798
id_counter: &mut HashMap<String, usize>,
799+
print_page_id: Option<&str>,
739800
) -> String {
740-
let id = id.unwrap_or_else(|| utils::unique_id_from_content(content, id_counter));
801+
let id = if let Some(print_page_id) = print_page_id {
802+
let content_id = {
803+
#[allow(deprecated)]
804+
utils::id_from_content(content)
805+
};
806+
let with_prefix = format!("{} {}", print_page_id, content_id);
807+
id.unwrap_or_else(|| utils::unique_id_from_content(&with_prefix, id_counter))
808+
} else {
809+
id.unwrap_or_else(|| utils::unique_id_from_content(content, id_counter))
810+
};
741811
let classes = classes
742812
.map(|s| format!(" class=\"{s}\""))
743813
.unwrap_or_default();
@@ -1126,7 +1196,7 @@ mod tests {
11261196
];
11271197

11281198
for (src, should_be) in inputs {
1129-
let got = build_header_links(src);
1199+
let got = build_header_links(src, None);
11301200
assert_eq!(got, should_be);
11311201
}
11321202
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,8 @@ fn render_item(
129129
.with_extension("html")
130130
.to_url_path();
131131

132-
let options = HtmlRenderOptions::new(&chapter_path);
132+
let redirect = HashMap::new();
133+
let options = HtmlRenderOptions::new(&chapter_path, &redirect);
133134
let mut p = new_cmark_parser(&chapter.content, &options.markdown_options).peekable();
134135

135136
let mut in_heading = false;

crates/mdbook-html/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ use std::path::Path;
1212
/// Creates an [`HtmlRenderOptions`] from the given config.
1313
pub fn html_render_options_from_config<'a>(
1414
path: &'a Path,
15-
config: &HtmlConfig,
15+
config: &'a HtmlConfig,
1616
) -> HtmlRenderOptions<'a> {
17-
let mut options = HtmlRenderOptions::new(path);
17+
let mut options = HtmlRenderOptions::new(path, &config.redirect);
1818
options.markdown_options.smart_punctuation = config.smart_punctuation;
1919
options
2020
}

0 commit comments

Comments
 (0)