diff --git a/crates/mdbook-core/src/utils/mod.rs b/crates/mdbook-core/src/utils/mod.rs index c36b16e960..1fb4abe9ec 100644 --- a/crates/mdbook-core/src/utils/mod.rs +++ b/crates/mdbook-core/src/utils/mod.rs @@ -43,7 +43,7 @@ pub fn normalize_id(content: &str) -> String { /// Generate an ID for use with anchors which is derived from a "normalised" /// string. -fn id_from_content(content: &str) -> String { +pub fn id_from_content(content: &str) -> String { let mut content = content.to_string(); // Skip any tags or html-encoded stuff diff --git a/crates/mdbook-html/src/html_handlebars/hbs_renderer.rs b/crates/mdbook-html/src/html_handlebars/hbs_renderer.rs index e703752a7e..a2d8853208 100644 --- a/crates/mdbook-html/src/html_handlebars/hbs_renderer.rs +++ b/crates/mdbook-html/src/html_handlebars/hbs_renderer.rs @@ -67,7 +67,25 @@ impl HtmlHandlebars { print_content .push_str(r#"
"#); } - print_content.push_str(&fixed_content); + let print_page_id = { + let mut base = path.display().to_string(); + if base.ends_with(".md") { + base.truncate(base.len() - 3); + } + &base + .replace("/", "-") + .replace("\\", "-") + .to_ascii_lowercase() + }; + + // We have to build header links in advance so that we can know the ranges + // for the headers in one page. + // Insert a dummy div to make sure that we can locate the specific page. + print_content.push_str(&(format!(r#""#))); + print_content.push_str(&build_header_links( + &build_print_element_id(&fixed_content, &print_page_id), + Some(print_page_id), + )); // Update the context with data for this file let ctx_path = path @@ -239,7 +257,23 @@ impl HtmlHandlebars { code_config: &Code, edition: Option'one'
\n"); } @@ -79,7 +83,8 @@ fn it_can_make_quotes_curly_except_when_they_are_in_code() {'three'
‘four’
more text with spaces
"#; - let options = HtmlRenderOptions::new(&Path::new("")); + let redirect = HashMap::new(); + let options = HtmlRenderOptions::new(&Path::new(""), &redirect); assert_eq!(render_markdown(input, &options), expected); } @@ -115,7 +121,8 @@ fn rust_code_block_properties_are_passed_as_space_delimited_class() { let expected = r#"
"#;
- let options = HtmlRenderOptions::new(&Path::new(""));
+ let redirect = HashMap::new();
+ let options = HtmlRenderOptions::new(&Path::new(""), &redirect);
assert_eq!(render_markdown(input, &options), expected);
}
@@ -128,7 +135,8 @@ fn rust_code_block_properties_with_whitespace_are_passed_as_space_delimited_clas
let expected = r#"
"#;
- let options = HtmlRenderOptions::new(&Path::new(""));
+ let redirect = HashMap::new();
+ let options = HtmlRenderOptions::new(&Path::new(""), &redirect);
assert_eq!(render_markdown(input, &options), expected);
}
@@ -141,13 +149,15 @@ fn rust_code_block_without_properties_has_proper_html_class() {
let expected = r#"
"#;
- let options = HtmlRenderOptions::new(&Path::new(""));
+ let redirect = HashMap::new();
+ let options = HtmlRenderOptions::new(&Path::new(""), &redirect);
assert_eq!(render_markdown(input, &options), expected);
let input = r#"
```rust
```
"#;
- let options = HtmlRenderOptions::new(&Path::new(""));
+ let redirect = HashMap::new();
+ let options = HtmlRenderOptions::new(&Path::new(""), &redirect);
assert_eq!(render_markdown(input, &options), expected);
}
diff --git a/guide/src/misc/contributors.md b/guide/src/misc/contributors.md
index 362a21fe4f..ff3549091f 100644
--- a/guide/src/misc/contributors.md
+++ b/guide/src/misc/contributors.md
@@ -20,5 +20,6 @@ shout-out to them!
- Vivek Akupatni ([apatniv](https://github.com/apatniv))
- Eric Huss ([ehuss](https://github.com/ehuss))
- Josh Rotenberg ([joshrotenberg](https://github.com/joshrotenberg))
+- Songlin Jiang ([HollowMan6](https://github.com/HollowMan6))
If you feel you're missing from this list, feel free to add yourself in a PR.
diff --git a/tests/testsuite/print.rs b/tests/testsuite/print.rs
index 0e0cdfe05c..57f02501db 100644
--- a/tests/testsuite/print.rs
+++ b/tests/testsuite/print.rs
@@ -8,17 +8,17 @@ fn relative_links() {
BookTest::from_dir("print/relative_links")
.check_main_file("book/print.html",
str![[r##"
-When we link to the first section, it should work on +
When we link to the first section, it should work on both the print page and the non-print page.
-A fragment link should work.
+A fragment link should work.
Link outside.