diff --git a/src/renderer/html_handlebars/hbs_renderer.rs b/src/renderer/html_handlebars/hbs_renderer.rs index 6425c9ae..bc7c1a92 100644 --- a/src/renderer/html_handlebars/hbs_renderer.rs +++ b/src/renderer/html_handlebars/hbs_renderer.rs @@ -1,5 +1,5 @@ use crate::book::{Book, BookItem}; -use crate::config::{Config, HtmlConfig, Playground, RustEdition}; +use crate::config::{BookConfig, Config, HtmlConfig, Playground, RustEdition}; use crate::errors::*; use crate::renderer::html_handlebars::helpers; use crate::renderer::{RenderContext, Renderer}; @@ -38,12 +38,14 @@ impl HtmlHandlebars { }; if let Some(ref edit_url_template) = ctx.html_config.edit_url_template { - let full_path = "src/".to_owned() + let full_path = ctx.book_config.src.to_str().unwrap_or_default().to_owned() + + "/" + ch.source_path .clone() .unwrap_or_default() .to_str() .unwrap_or_default(); + let edit_url = edit_url_template.replace("{path}", &full_path); ctx.data .insert("git_repository_edit_url".to_owned(), json!(edit_url)); @@ -458,6 +460,7 @@ impl Renderer for HtmlHandlebars { } fn render(&self, ctx: &RenderContext) -> Result<()> { + let book_config = &ctx.config.book; let html_config = ctx.config.html_config().unwrap_or_default(); let src_dir = ctx.root.join(&ctx.config.book.src); let destination = &ctx.destination; @@ -520,6 +523,7 @@ impl Renderer for HtmlHandlebars { destination: destination.to_path_buf(), data: data.clone(), is_index, + book_config: book_config.clone(), html_config: html_config.clone(), edition: ctx.config.rust.edition, chapter_titles: &ctx.chapter_titles, @@ -936,6 +940,7 @@ struct RenderItemContext<'a> { destination: PathBuf, data: serde_json::Map, is_index: bool, + book_config: BookConfig, html_config: HtmlConfig, edition: Option, chapter_titles: &'a HashMap, diff --git a/tests/rendered_output.rs b/tests/rendered_output.rs index 649873ac..c6f998d6 100644 --- a/tests/rendered_output.rs +++ b/tests/rendered_output.rs @@ -541,6 +541,57 @@ fn redirects_are_emitted_correctly() { } } +#[test] +fn edit_url_has_default_src_dir_edit_url() { + let temp = DummyBook::new().build().unwrap(); + let book_toml = r#" + [book] + title = "implicit" + + [output.html] + edit-url-template = "https://github.com/rust-lang/mdBook/edit/master/guide/{path}" + "#; + + write_file(&temp.path(), "book.toml", book_toml.as_bytes()).unwrap(); + + let md = MDBook::load(temp.path()).unwrap(); + md.build().unwrap(); + + let index_html = temp.path().join("book").join("index.html"); + assert_contains_strings( + index_html, + &vec![ + r#"href="https://github.com/rust-lang/mdBook/edit/master/guide/src/README.md" title="Suggest an edit""#, + ], + ); +} + +#[test] +fn edit_url_has_configured_src_dir_edit_url() { + let temp = DummyBook::new().build().unwrap(); + let book_toml = r#" + [book] + title = "implicit" + src = "src2" + + [output.html] + edit-url-template = "https://github.com/rust-lang/mdBook/edit/master/guide/{path}" + "#; + + write_file(&temp.path(), "book.toml", book_toml.as_bytes()).unwrap(); + + let md = MDBook::load(temp.path()).unwrap(); + md.build().unwrap(); + + let index_html = temp.path().join("book").join("index.html"); + assert_contains_strings( + index_html, + &vec![ + r#"href="https://github.com/rust-lang/mdBook/edit/master/guide/src2/README.md" title="Suggest an edit""#, + ], + ); +} + fn remove_absolute_components(path: &Path) -> impl Iterator + '_ { path.components().skip_while(|c| match c { Component::Prefix(_) | Component::RootDir => true,