Skip to content

Commit 25c1ca1

Browse files
authored
Merge pull request #866 from rust-lang-nursery/gh828
Fixing links in print.html
2 parents acbb951 + 23ac06e commit 25c1ca1

File tree

6 files changed

+370
-61
lines changed

6 files changed

+370
-61
lines changed

src/renderer/html_handlebars/hbs_renderer.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,11 @@ impl HtmlHandlebars {
3333
if let BookItem::Chapter(ref ch) = *item {
3434
let content = ch.content.clone();
3535
let content = utils::render_markdown(&content, ctx.html_config.curly_quotes);
36-
print_content.push_str(&content);
36+
37+
let string_path = ch.path.parent().unwrap().display().to_string();
38+
39+
let fixed_content = utils::render_markdown_with_base(&ch.content, ctx.html_config.curly_quotes, &string_path);
40+
print_content.push_str(&fixed_content);
3741

3842
// Update the context with data for this file
3943
let path = ch

src/utils/mod.rs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,21 @@ pub fn id_from_content(content: &str) -> String {
6666
normalize_id(trimmed)
6767
}
6868

69-
fn adjust_links(event: Event) -> Event {
69+
fn adjust_links<'a>(event: Event<'a>, with_base: &str) -> Event<'a> {
7070
lazy_static! {
7171
static ref HTTP_LINK: Regex = Regex::new("^https?://").unwrap();
72-
static ref MD_LINK: Regex = Regex::new("(?P<link>.*).md(?P<anchor>#.*)?").unwrap();
72+
static ref MD_LINK: Regex = Regex::new(r"(?P<link>.*)\.md(?P<anchor>#.*)?").unwrap();
7373
}
7474

7575
match event {
7676
Event::Start(Tag::Link(dest, title)) => {
7777
if !HTTP_LINK.is_match(&dest) {
78+
let dest = if !with_base.is_empty() {
79+
format!("{}/{}", with_base, dest)
80+
} else {
81+
dest.clone().into_owned()
82+
};
83+
7884
if let Some(caps) = MD_LINK.captures(&dest) {
7985
let mut html_link = [&caps["link"], ".html"].concat();
8086

@@ -94,6 +100,10 @@ fn adjust_links(event: Event) -> Event {
94100

95101
/// Wrapper around the pulldown-cmark parser for rendering markdown to HTML.
96102
pub fn render_markdown(text: &str, curly_quotes: bool) -> String {
103+
render_markdown_with_base(text, curly_quotes, "")
104+
}
105+
106+
pub fn render_markdown_with_base(text: &str, curly_quotes: bool, base: &str) -> String {
97107
let mut s = String::with_capacity(text.len() * 3 / 2);
98108

99109
let mut opts = Options::empty();
@@ -104,7 +114,7 @@ pub fn render_markdown(text: &str, curly_quotes: bool) -> String {
104114
let mut converter = EventQuoteConverter::new(curly_quotes);
105115
let events = p
106116
.map(clean_codeblock_headers)
107-
.map(adjust_links)
117+
.map(|event| adjust_links(event, base))
108118
.map(|event| converter.convert(event));
109119

110120
html::push_html(&mut s, events);
@@ -220,6 +230,12 @@ mod tests {
220230
render_markdown("[example_anchor](example.md#anchor)", false),
221231
"<p><a href=\"example.html#anchor\">example_anchor</a></p>\n"
222232
);
233+
234+
// this anchor contains 'md' inside of it
235+
assert_eq!(
236+
render_markdown("[phantom data](foo.html#phantomdata)", false),
237+
"<p><a href=\"foo.html#phantomdata\">phantom data</a></p>\n"
238+
);
223239
}
224240

225241
#[test]

tests/dummy_book/src/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
- [Includes](first/includes.md)
99
- [Recursive](first/recursive.md)
1010
- [Second Chapter](second.md)
11+
- [Nested Chapter](second/nested.md)
1112

1213
---
1314

tests/dummy_book/src/second/nested.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Testing relative links for the print page
2+
3+
When we link to [the first section](../first/nested.md), it should work on
4+
both the print page and the non-print page.

tests/rendered_output.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ const TOC_TOP_LEVEL: &[&'static str] = &[
3131
"Introduction",
3232
];
3333
const TOC_SECOND_LEVEL: &[&'static str] =
34-
&["1.1. Nested Chapter", "1.2. Includes", "1.3. Recursive"];
34+
&["1.1. Nested Chapter", "1.2. Includes", "2.1. Nested Chapter", "1.3. Recursive"];
3535

3636
/// Make sure you can load the dummy book and build it without panicking.
3737
#[test]
@@ -109,6 +109,20 @@ fn check_correct_cross_links_in_nested_dir() {
109109
);
110110
}
111111

112+
#[test]
113+
fn check_correct_relative_links_in_print_page() {
114+
let temp = DummyBook::new().build().unwrap();
115+
let md = MDBook::load(temp.path()).unwrap();
116+
md.build().unwrap();
117+
118+
let first = temp.path().join("book");
119+
120+
assert_contains_strings(
121+
first.join("print.html"),
122+
&[r##"<a href="second/../first/nested.html">the first section</a>,"##],
123+
);
124+
}
125+
112126
#[test]
113127
fn rendered_code_has_playpen_stuff() {
114128
let temp = DummyBook::new().build().unwrap();
@@ -443,7 +457,7 @@ mod search {
443457
assert_eq!(docs[&some_section]["body"], "");
444458
assert_eq!(
445459
docs[&summary]["body"],
446-
"Dummy Book Introduction First Chapter Nested Chapter Includes Recursive Second Chapter Conclusion"
460+
"Dummy Book Introduction First Chapter Nested Chapter Includes Recursive Second Chapter Nested Chapter Conclusion"
447461
);
448462
assert_eq!(docs[&summary]["breadcrumbs"], "First Chapter » Summary");
449463
assert_eq!(docs[&conclusion]["body"], "I put &lt;HTML&gt; in here!");

0 commit comments

Comments
 (0)