From 1345c05b18e8d16c76a8e39276e1cdf61fe1ae54 Mon Sep 17 00:00:00 2001 From: Mathieu David Date: Fri, 24 Feb 2017 11:51:00 +0100 Subject: [PATCH] Fix anchors, Fixes #211 --- src/renderer/html_handlebars/hbs_renderer.rs | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/renderer/html_handlebars/hbs_renderer.rs b/src/renderer/html_handlebars/hbs_renderer.rs index 3e12d39a..e47173d6 100644 --- a/src/renderer/html_handlebars/hbs_renderer.rs +++ b/src/renderer/html_handlebars/hbs_renderer.rs @@ -95,14 +95,15 @@ impl Renderer for HtmlHandlebars { debug!("[*]: Render template"); let rendered = try!(handlebars.render("index", &data)); + let filename = Path::new(&ch.path).with_extension("html"); + // create links for headers - let rendered = build_header_links(rendered); + let rendered = build_header_links(rendered, filename.to_str().unwrap_or("")); // fix code blocks let rendered = fix_code_blocks(rendered); // Write to file - let filename = Path::new(&ch.path).with_extension("html"); info!("[*] Creating {:?} ✓", filename.display()); try!(book.write_file(filename, &rendered.into_bytes())); @@ -144,7 +145,7 @@ impl Renderer for HtmlHandlebars { debug!("[*]: Render template"); let rendered = try!(handlebars.render("index", &data)); - let rendered = build_header_links(rendered); + let rendered = build_header_links(rendered, "print.html"); // fix code blocks let rendered = fix_code_blocks(rendered); @@ -224,7 +225,7 @@ fn make_data(book: &MDBook) -> Result Ok(data) } -fn build_header_links(html: String) -> String { +fn build_header_links(html: String, filename: &str) -> String { let regex = Regex::new(r"(.*?)").unwrap(); regex.replace_all(&html, |caps: &Captures| { @@ -251,7 +252,8 @@ fn build_header_links(html: String) -> String { } }).collect::(); - format!("{text}", level=level, id=id, text=text) + format!("{text}", + level=level, id=id, text=text, filename=filename) }).into_owned() }