requested changes

This commit is contained in:
joaofreires 2022-11-11 13:48:48 -03:00
parent bcd63f97c5
commit e2b0b05ef8
No known key found for this signature in database
GPG Key ID: C21AD65E2F4A6C65
3 changed files with 23 additions and 15 deletions

View File

@ -109,6 +109,7 @@ edit-url-template = "https://github.com/rust-lang/mdBook/edit/master/guide/{path
site-url = "/example-book/" site-url = "/example-book/"
cname = "myproject.rs" cname = "myproject.rs"
input-404 = "not-found.md" input-404 = "not-found.md"
use-site-url-as-root = false
``` ```
The following configuration options are available: The following configuration options are available:

View File

@ -56,7 +56,7 @@ impl HtmlHandlebars {
let content = ch.content.clone(); let content = ch.content.clone();
let content = if ctx.html_config.use_site_url_as_root { let content = if ctx.html_config.use_site_url_as_root {
utils::render_markdown_with_path( utils::render_markdown_with_abs_path(
&content, &content,
ctx.html_config.curly_quotes, ctx.html_config.curly_quotes,
None, None,
@ -66,12 +66,8 @@ impl HtmlHandlebars {
utils::render_markdown(&content, ctx.html_config.curly_quotes) utils::render_markdown(&content, ctx.html_config.curly_quotes)
}; };
let fixed_content = utils::render_markdown_with_path( let fixed_content =
&ch.content, utils::render_markdown_with_path(&ch.content, ctx.html_config.curly_quotes, Some(path));
ctx.html_config.curly_quotes,
Some(path),
None,
);
if !ctx.is_index && ctx.html_config.print.page_break { if !ctx.is_index && ctx.html_config.print.page_break {
// Add page break between chapters // Add page break between chapters
// 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 // 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

View File

@ -126,20 +126,25 @@ fn adjust_links<'a>(event: Event<'a>, path: Option<&Path>, abs_url: Option<&Stri
} }
if let Some(caps) = MD_LINK.captures(&dest) { if let Some(caps) = MD_LINK.captures(&dest) {
fixed_link.push_str(&caps["link"]); fixed_link.push_str(&caps["link"].trim_start_matches('/'));
fixed_link.push_str(".html"); fixed_link.push_str(".html");
if let Some(anchor) = caps.name("anchor") { if let Some(anchor) = caps.name("anchor") {
fixed_link.push_str(anchor.as_str()); fixed_link.push_str(anchor.as_str());
} }
} else if !fixed_link.is_empty() {
// prevent links with double slashes
fixed_link.push_str(&dest.trim_start_matches('/'));
} else { } else {
fixed_link.push_str(&dest); fixed_link.push_str(&dest);
}; };
if fixed_link.starts_with('/') { if dest.starts_with('/') || path.is_some() {
fixed_link = match abs_url { if let Some(abs_url) = abs_url {
Some(abs_url) => format!("{}{}", abs_url.trim_end_matches('/'), &fixed_link), fixed_link = format!(
None => fixed_link, "{}/{}",
abs_url.trim_end_matches('/'),
&fixed_link.trim_start_matches('/')
);
} }
.into();
} }
return CowStr::from(format!("{}", fixed_link)); return CowStr::from(format!("{}", fixed_link));
} }
@ -181,7 +186,7 @@ fn adjust_links<'a>(event: Event<'a>, path: Option<&Path>, abs_url: Option<&Stri
/// Wrapper around the pulldown-cmark parser for rendering markdown to HTML. /// Wrapper around the pulldown-cmark parser for rendering markdown to HTML.
pub fn render_markdown(text: &str, curly_quotes: bool) -> String { pub fn render_markdown(text: &str, curly_quotes: bool) -> String {
render_markdown_with_path(text, curly_quotes, None, None) render_markdown_with_path(text, curly_quotes, None)
} }
pub fn new_cmark_parser(text: &str, curly_quotes: bool) -> Parser<'_, '_> { 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<'_, '_> {
Parser::new_ext(text, opts) Parser::new_ext(text, opts)
} }
pub fn render_markdown_with_path( pub fn render_markdown_with_path(text: &str, curly_quotes: bool, path: Option<&Path>) -> String {
render_markdown_with_abs_path(text, curly_quotes, path, None)
}
pub fn render_markdown_with_abs_path(
text: &str, text: &str,
curly_quotes: bool, curly_quotes: bool,
path: Option<&Path>, path: Option<&Path>,
abs_url: Option<&String>, abs_url: Option<&String>,
) -> String { ) -> String {
// This function should be merged with `render_markdown_with_path`
// in the future. Currently, it is used not to break compatibility.
let mut s = String::with_capacity(text.len() * 3 / 2); let mut s = String::with_capacity(text.len() * 3 / 2);
let p = new_cmark_parser(text, curly_quotes); let p = new_cmark_parser(text, curly_quotes);
let events = p let events = p