Skip to content

Commit e2b0b05

Browse files
committed
requested changes
1 parent bcd63f9 commit e2b0b05

File tree

3 files changed

+23
-15
lines changed

3 files changed

+23
-15
lines changed

guide/src/format/configuration/renderers.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ edit-url-template = "https://github.com/rust-lang/mdBook/edit/master/guide/{path
109109
site-url = "/example-book/"
110110
cname = "myproject.rs"
111111
input-404 = "not-found.md"
112+
use-site-url-as-root = false
112113
```
113114

114115
The following configuration options are available:

src/renderer/html_handlebars/hbs_renderer.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ impl HtmlHandlebars {
5656

5757
let content = ch.content.clone();
5858
let content = if ctx.html_config.use_site_url_as_root {
59-
utils::render_markdown_with_path(
59+
utils::render_markdown_with_abs_path(
6060
&content,
6161
ctx.html_config.curly_quotes,
6262
None,
@@ -66,12 +66,8 @@ impl HtmlHandlebars {
6666
utils::render_markdown(&content, ctx.html_config.curly_quotes)
6767
};
6868

69-
let fixed_content = utils::render_markdown_with_path(
70-
&ch.content,
71-
ctx.html_config.curly_quotes,
72-
Some(path),
73-
None,
74-
);
69+
let fixed_content =
70+
utils::render_markdown_with_path(&ch.content, ctx.html_config.curly_quotes, Some(path));
7571
if !ctx.is_index && ctx.html_config.print.page_break {
7672
// Add page break between chapters
7773
// See https://developer.mozilla.org/en-US/docs/Web/CSS/break-before and https://developer.mozilla.org/en-US/docs/Web/CSS/page-break-before

src/utils/mod.rs

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -126,20 +126,25 @@ fn adjust_links<'a>(event: Event<'a>, path: Option<&Path>, abs_url: Option<&Stri
126126
}
127127

128128
if let Some(caps) = MD_LINK.captures(&dest) {
129-
fixed_link.push_str(&caps["link"]);
129+
fixed_link.push_str(&caps["link"].trim_start_matches('/'));
130130
fixed_link.push_str(".html");
131131
if let Some(anchor) = caps.name("anchor") {
132132
fixed_link.push_str(anchor.as_str());
133133
}
134+
} else if !fixed_link.is_empty() {
135+
// prevent links with double slashes
136+
fixed_link.push_str(&dest.trim_start_matches('/'));
134137
} else {
135138
fixed_link.push_str(&dest);
136139
};
137-
if fixed_link.starts_with('/') {
138-
fixed_link = match abs_url {
139-
Some(abs_url) => format!("{}{}", abs_url.trim_end_matches('/'), &fixed_link),
140-
None => fixed_link,
140+
if dest.starts_with('/') || path.is_some() {
141+
if let Some(abs_url) = abs_url {
142+
fixed_link = format!(
143+
"{}/{}",
144+
abs_url.trim_end_matches('/'),
145+
&fixed_link.trim_start_matches('/')
146+
);
141147
}
142-
.into();
143148
}
144149
return CowStr::from(format!("{}", fixed_link));
145150
}
@@ -181,7 +186,7 @@ fn adjust_links<'a>(event: Event<'a>, path: Option<&Path>, abs_url: Option<&Stri
181186

182187
/// Wrapper around the pulldown-cmark parser for rendering markdown to HTML.
183188
pub fn render_markdown(text: &str, curly_quotes: bool) -> String {
184-
render_markdown_with_path(text, curly_quotes, None, None)
189+
render_markdown_with_path(text, curly_quotes, None)
185190
}
186191

187192
pub fn new_cmark_parser(text: &str, curly_quotes: bool) -> Parser<'_, '_> {
@@ -196,12 +201,18 @@ pub fn new_cmark_parser(text: &str, curly_quotes: bool) -> Parser<'_, '_> {
196201
Parser::new_ext(text, opts)
197202
}
198203

199-
pub fn render_markdown_with_path(
204+
pub fn render_markdown_with_path(text: &str, curly_quotes: bool, path: Option<&Path>) -> String {
205+
render_markdown_with_abs_path(text, curly_quotes, path, None)
206+
}
207+
208+
pub fn render_markdown_with_abs_path(
200209
text: &str,
201210
curly_quotes: bool,
202211
path: Option<&Path>,
203212
abs_url: Option<&String>,
204213
) -> String {
214+
// This function should be merged with `render_markdown_with_path`
215+
// in the future. Currently, it is used not to break compatibility.
205216
let mut s = String::with_capacity(text.len() * 3 / 2);
206217
let p = new_cmark_parser(text, curly_quotes);
207218
let events = p

0 commit comments

Comments
 (0)