Merge pull request #2914 from ehuss/fix-print-relative
Fix print page links for internal links to non-chapters
This commit is contained in:
commit
8670bcc540
3 changed files with 23 additions and 13 deletions
|
|
@ -9,7 +9,7 @@ use crate::html::{ChapterTree, Element, serialize};
|
||||||
use crate::utils::{ToUrlPath, id_from_content, normalize_path, unique_id};
|
use crate::utils::{ToUrlPath, id_from_content, normalize_path, unique_id};
|
||||||
use mdbook_core::static_regex;
|
use mdbook_core::static_regex;
|
||||||
use std::collections::{HashMap, HashSet};
|
use std::collections::{HashMap, HashSet};
|
||||||
use std::path::{Component, PathBuf};
|
use std::path::PathBuf;
|
||||||
|
|
||||||
/// Takes all the chapter trees, modifies them to be suitable to render for
|
/// Takes all the chapter trees, modifies them to be suitable to render for
|
||||||
/// the print page, and returns an string of all the chapters rendered to a
|
/// the print page, and returns an string of all the chapters rendered to a
|
||||||
|
|
@ -166,13 +166,9 @@ fn rewrite_links(
|
||||||
{
|
{
|
||||||
lookup_key.pop();
|
lookup_key.pop();
|
||||||
lookup_key.push(href_path);
|
lookup_key.push(href_path);
|
||||||
let normalized = normalize_path(&lookup_key);
|
lookup_key = normalize_path(&lookup_key);
|
||||||
// If this points outside of the book, don't modify it.
|
let is_a_chapter = path_to_root_id.contains_key(&lookup_key);
|
||||||
let is_outside = matches!(
|
if !is_a_chapter {
|
||||||
normalized.components().next(),
|
|
||||||
Some(Component::ParentDir | Component::RootDir)
|
|
||||||
);
|
|
||||||
if is_outside || !href_path.ends_with(".html") {
|
|
||||||
// Make the link relative to the print page location.
|
// Make the link relative to the print page location.
|
||||||
let mut rel_path = normalize_path(&base.join(href_path)).to_url_path();
|
let mut rel_path = normalize_path(&base.join(href_path)).to_url_path();
|
||||||
if let Some(anchor) = caps.name("anchor") {
|
if let Some(anchor) = caps.name("anchor") {
|
||||||
|
|
@ -184,10 +180,7 @@ fn rewrite_links(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let lookup_key = normalize_path(&lookup_key);
|
let id = match caps.name("anchor") {
|
||||||
|
|
||||||
let anchor = caps.name("anchor");
|
|
||||||
let id = match anchor {
|
|
||||||
Some(anchor_id) => {
|
Some(anchor_id) => {
|
||||||
let anchor_id = anchor_id.as_str().to_string();
|
let anchor_id = anchor_id.as_str().to_string();
|
||||||
match id_remap.get(&lookup_key) {
|
match id_remap.get(&lookup_key) {
|
||||||
|
|
@ -204,7 +197,15 @@ fn rewrite_links(
|
||||||
}
|
}
|
||||||
None => match path_to_root_id.get(&lookup_key) {
|
None => match path_to_root_id.get(&lookup_key) {
|
||||||
Some(id) => id.to_string(),
|
Some(id) => id.to_string(),
|
||||||
None => continue,
|
None => {
|
||||||
|
// This should be guaranteed that either the
|
||||||
|
// chapter itself is in the map (for anchor-only
|
||||||
|
// links), or the is_a_chapter check above.
|
||||||
|
panic!(
|
||||||
|
"internal error: expected `{lookup_key:?}` to be in \
|
||||||
|
root map (chapter path is `{html_path:?}`)"
|
||||||
|
);
|
||||||
|
}
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
el.insert_attr(attr, format!("#{id}").into());
|
el.insert_attr(attr, format!("#{id}").into());
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,10 @@ both the print page and the non-print page.</p>
|
||||||
<p>A <a href="#some-section">fragment link</a> should work.</p>
|
<p>A <a href="#some-section">fragment link</a> should work.</p>
|
||||||
<p>Link <a href="../std/foo/bar.html">outside</a>.</p>
|
<p>Link <a href="../std/foo/bar.html">outside</a>.</p>
|
||||||
<p>Link <a href="../std/foo/bar.html#panic">outside with anchor</a>.</p>
|
<p>Link <a href="../std/foo/bar.html#panic">outside with anchor</a>.</p>
|
||||||
|
<p>Link <a href="first/alpha/beta.html">inside but doesn’t exist</a>.
|
||||||
|
Link <a href="first/alpha/beta.html#anchor">inside but doesn’t exist with anchor</a>.
|
||||||
|
Link <a href="first/alpha/gamma.html">inside to html</a>.
|
||||||
|
Link <a href="first/alpha/gamma.html#anchor">inside to html with anchor</a>.</p>
|
||||||
<p><img src="images/picture.png" alt="Some image"></p>
|
<p><img src="images/picture.png" alt="Some image"></p>
|
||||||
<p><a href="#first-nested">HTML Link</a></p>
|
<p><a href="#first-nested">HTML Link</a></p>
|
||||||
<img src="images/picture.png" alt="raw html">
|
<img src="images/picture.png" alt="raw html">
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,11 @@ Link [outside](../../std/foo/bar.html).
|
||||||
|
|
||||||
Link [outside with anchor](../../std/foo/bar.html#panic).
|
Link [outside with anchor](../../std/foo/bar.html#panic).
|
||||||
|
|
||||||
|
Link [inside but doesn't exist](../first/alpha/beta.md).
|
||||||
|
Link [inside but doesn't exist with anchor](../first/alpha/beta.md#anchor).
|
||||||
|
Link [inside to html](../first/alpha/gamma.html).
|
||||||
|
Link [inside to html with anchor](../first/alpha/gamma.html#anchor).
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
<a href="../first/nested.md">HTML Link</a>
|
<a href="../first/nested.md">HTML Link</a>
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue