Skip to content

Commit 8670bcc

Browse files
authored
Merge pull request #2914 from ehuss/fix-print-relative
Fix print page links for internal links to non-chapters
2 parents e8d7dd6 + 005f4d6 commit 8670bcc

File tree

3 files changed

+23
-13
lines changed

3 files changed

+23
-13
lines changed

crates/mdbook-html/src/html/print.rs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::html::{ChapterTree, Element, serialize};
99
use crate::utils::{ToUrlPath, id_from_content, normalize_path, unique_id};
1010
use mdbook_core::static_regex;
1111
use std::collections::{HashMap, HashSet};
12-
use std::path::{Component, PathBuf};
12+
use std::path::PathBuf;
1313

1414
/// Takes all the chapter trees, modifies them to be suitable to render for
1515
/// the print page, and returns an string of all the chapters rendered to a
@@ -166,13 +166,9 @@ fn rewrite_links(
166166
{
167167
lookup_key.pop();
168168
lookup_key.push(href_path);
169-
let normalized = normalize_path(&lookup_key);
170-
// If this points outside of the book, don't modify it.
171-
let is_outside = matches!(
172-
normalized.components().next(),
173-
Some(Component::ParentDir | Component::RootDir)
174-
);
175-
if is_outside || !href_path.ends_with(".html") {
169+
lookup_key = normalize_path(&lookup_key);
170+
let is_a_chapter = path_to_root_id.contains_key(&lookup_key);
171+
if !is_a_chapter {
176172
// Make the link relative to the print page location.
177173
let mut rel_path = normalize_path(&base.join(href_path)).to_url_path();
178174
if let Some(anchor) = caps.name("anchor") {
@@ -184,10 +180,7 @@ fn rewrite_links(
184180
}
185181
}
186182

187-
let lookup_key = normalize_path(&lookup_key);
188-
189-
let anchor = caps.name("anchor");
190-
let id = match anchor {
183+
let id = match caps.name("anchor") {
191184
Some(anchor_id) => {
192185
let anchor_id = anchor_id.as_str().to_string();
193186
match id_remap.get(&lookup_key) {
@@ -204,7 +197,15 @@ fn rewrite_links(
204197
}
205198
None => match path_to_root_id.get(&lookup_key) {
206199
Some(id) => id.to_string(),
207-
None => continue,
200+
None => {
201+
// This should be guaranteed that either the
202+
// chapter itself is in the map (for anchor-only
203+
// links), or the is_a_chapter check above.
204+
panic!(
205+
"internal error: expected `{lookup_key:?}` to be in \
206+
root map (chapter path is `{html_path:?}`)"
207+
);
208+
}
208209
},
209210
};
210211
el.insert_attr(attr, format!("#{id}").into());

tests/testsuite/print/relative_links/expected/print.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ <h1 id="testing-relative-links-for-the-print-page"><a class="header" href="#test
99
<p>A <a href="#some-section">fragment link</a> should work.</p>
1010
<p>Link <a href="../std/foo/bar.html">outside</a>.</p>
1111
<p>Link <a href="../std/foo/bar.html#panic">outside with anchor</a>.</p>
12+
<p>Link <a href="first/alpha/beta.html">inside but doesn’t exist</a>.
13+
Link <a href="first/alpha/beta.html#anchor">inside but doesn’t exist with anchor</a>.
14+
Link <a href="first/alpha/gamma.html">inside to html</a>.
15+
Link <a href="first/alpha/gamma.html#anchor">inside to html with anchor</a>.</p>
1216
<p><img src="images/picture.png" alt="Some image"></p>
1317
<p><a href="#first-nested">HTML Link</a></p>
1418
<img src="images/picture.png" alt="raw html">

tests/testsuite/print/relative_links/src/second/nested.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ Link [outside](../../std/foo/bar.html).
1111

1212
Link [outside with anchor](../../std/foo/bar.html#panic).
1313

14+
Link [inside but doesn't exist](../first/alpha/beta.md).
15+
Link [inside but doesn't exist with anchor](../first/alpha/beta.md#anchor).
16+
Link [inside to html](../first/alpha/gamma.html).
17+
Link [inside to html with anchor](../first/alpha/gamma.html#anchor).
18+
1419
![Some image](../images/picture.png)
1520

1621
<a href="../first/nested.md">HTML Link</a>

0 commit comments

Comments
 (0)