diff --git a/src/renderer/html_handlebars/hbs_renderer.rs b/src/renderer/html_handlebars/hbs_renderer.rs
index f12fd16875..b5ef228d34 100644
--- a/src/renderer/html_handlebars/hbs_renderer.rs
+++ b/src/renderer/html_handlebars/hbs_renderer.rs
@@ -33,7 +33,11 @@ impl HtmlHandlebars {
if let BookItem::Chapter(ref ch) = *item {
let content = ch.content.clone();
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
let path = ch
diff --git a/src/utils/mod.rs b/src/utils/mod.rs
index 31abe98cae..aacdb1c711 100644
--- a/src/utils/mod.rs
+++ b/src/utils/mod.rs
@@ -66,7 +66,7 @@ pub fn id_from_content(content: &str) -> String {
normalize_id(trimmed)
}
-fn adjust_links(event: Event) -> Event {
+fn adjust_links<'a>(event: Event<'a>, with_base: &str) -> Event<'a> {
lazy_static! {
static ref HTTP_LINK: Regex = Regex::new("^https?://").unwrap();
static ref MD_LINK: Regex = Regex::new("(?P.*).md(?P#.*)?").unwrap();
@@ -75,6 +75,12 @@ fn adjust_links(event: Event) -> Event {
match event {
Event::Start(Tag::Link(dest, title)) => {
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) {
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.
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 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 events = p
.map(clean_codeblock_headers)
- .map(adjust_links)
+ .map(|event| adjust_links(event, base))
.map(|event| converter.convert(event));
html::push_html(&mut s, events);