Fix the bug

This commit is contained in:
Steve Klabnik 2019-01-15 13:19:10 -05:00
parent a481735fa2
commit 2ddbb37f49
2 changed files with 17 additions and 3 deletions

View File

@ -33,7 +33,11 @@ impl HtmlHandlebars {
if let BookItem::Chapter(ref ch) = *item { if let BookItem::Chapter(ref ch) = *item {
let content = ch.content.clone(); let content = ch.content.clone();
let content = utils::render_markdown(&content, ctx.html_config.curly_quotes); let content = utils::render_markdown(&content, ctx.html_config.curly_quotes);
print_content.push_str(&content);
let string_path = ch.path.parent().unwrap().display().to_string();
let fixed_content = utils::render_markdown_with_base(&ch.content, ctx.html_config.curly_quotes, &string_path);
print_content.push_str(&fixed_content);
// Update the context with data for this file // Update the context with data for this file
let path = ch let path = ch

View File

@ -66,7 +66,7 @@ pub fn id_from_content(content: &str) -> String {
normalize_id(trimmed) normalize_id(trimmed)
} }
fn adjust_links(event: Event) -> Event { fn adjust_links<'a>(event: Event<'a>, with_base: &str) -> Event<'a> {
lazy_static! { lazy_static! {
static ref HTTP_LINK: Regex = Regex::new("^https?://").unwrap(); static ref HTTP_LINK: Regex = Regex::new("^https?://").unwrap();
static ref MD_LINK: Regex = Regex::new("(?P<link>.*).md(?P<anchor>#.*)?").unwrap(); static ref MD_LINK: Regex = Regex::new("(?P<link>.*).md(?P<anchor>#.*)?").unwrap();
@ -75,6 +75,12 @@ fn adjust_links(event: Event) -> Event {
match event { match event {
Event::Start(Tag::Link(dest, title)) => { Event::Start(Tag::Link(dest, title)) => {
if !HTTP_LINK.is_match(&dest) { if !HTTP_LINK.is_match(&dest) {
let dest = if !with_base.is_empty() {
format!("{}/{}", with_base, dest)
} else {
dest.clone().into_owned()
};
if let Some(caps) = MD_LINK.captures(&dest) { if let Some(caps) = MD_LINK.captures(&dest) {
let mut html_link = [&caps["link"], ".html"].concat(); let mut html_link = [&caps["link"], ".html"].concat();
@ -94,6 +100,10 @@ fn adjust_links(event: Event) -> Event {
/// 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_base(text, curly_quotes, "")
}
pub fn render_markdown_with_base(text: &str, curly_quotes: bool, base: &str) -> String {
let mut s = String::with_capacity(text.len() * 3 / 2); let mut s = String::with_capacity(text.len() * 3 / 2);
let mut opts = Options::empty(); let mut opts = Options::empty();
@ -104,7 +114,7 @@ pub fn render_markdown(text: &str, curly_quotes: bool) -> String {
let mut converter = EventQuoteConverter::new(curly_quotes); let mut converter = EventQuoteConverter::new(curly_quotes);
let events = p let events = p
.map(clean_codeblock_headers) .map(clean_codeblock_headers)
.map(adjust_links) .map(|event| adjust_links(event, base))
.map(|event| converter.convert(event)); .map(|event| converter.convert(event));
html::push_html(&mut s, events); html::push_html(&mut s, events);