Skip to content

Commit d325c60

Browse files
authored
Merge pull request #1554 from joshrotenberg/edit_url_custom_src
Use the configured book src directory for the edit url template path
2 parents e9e889f + 60aaa7a commit d325c60

File tree

2 files changed

+58
-2
lines changed

2 files changed

+58
-2
lines changed

src/renderer/html_handlebars/hbs_renderer.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::book::{Book, BookItem};
2-
use crate::config::{Config, HtmlConfig, Playground, RustEdition};
2+
use crate::config::{BookConfig, Config, HtmlConfig, Playground, RustEdition};
33
use crate::errors::*;
44
use crate::renderer::html_handlebars::helpers;
55
use crate::renderer::{RenderContext, Renderer};
@@ -38,12 +38,14 @@ impl HtmlHandlebars {
3838
};
3939

4040
if let Some(ref edit_url_template) = ctx.html_config.edit_url_template {
41-
let full_path = "src/".to_owned()
41+
let full_path = ctx.book_config.src.to_str().unwrap_or_default().to_owned()
42+
+ "/"
4243
+ ch.source_path
4344
.clone()
4445
.unwrap_or_default()
4546
.to_str()
4647
.unwrap_or_default();
48+
4749
let edit_url = edit_url_template.replace("{path}", &full_path);
4850
ctx.data
4951
.insert("git_repository_edit_url".to_owned(), json!(edit_url));
@@ -458,6 +460,7 @@ impl Renderer for HtmlHandlebars {
458460
}
459461

460462
fn render(&self, ctx: &RenderContext) -> Result<()> {
463+
let book_config = &ctx.config.book;
461464
let html_config = ctx.config.html_config().unwrap_or_default();
462465
let src_dir = ctx.root.join(&ctx.config.book.src);
463466
let destination = &ctx.destination;
@@ -520,6 +523,7 @@ impl Renderer for HtmlHandlebars {
520523
destination: destination.to_path_buf(),
521524
data: data.clone(),
522525
is_index,
526+
book_config: book_config.clone(),
523527
html_config: html_config.clone(),
524528
edition: ctx.config.rust.edition,
525529
chapter_titles: &ctx.chapter_titles,
@@ -936,6 +940,7 @@ struct RenderItemContext<'a> {
936940
destination: PathBuf,
937941
data: serde_json::Map<String, serde_json::Value>,
938942
is_index: bool,
943+
book_config: BookConfig,
939944
html_config: HtmlConfig,
940945
edition: Option<RustEdition>,
941946
chapter_titles: &'a HashMap<PathBuf, String>,

tests/rendered_output.rs

+51
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,57 @@ fn redirects_are_emitted_correctly() {
541541
}
542542
}
543543

544+
#[test]
545+
fn edit_url_has_default_src_dir_edit_url() {
546+
let temp = DummyBook::new().build().unwrap();
547+
let book_toml = r#"
548+
[book]
549+
title = "implicit"
550+
551+
[output.html]
552+
edit-url-template = "https://github.com/rust-lang/mdBook/edit/master/guide/{path}"
553+
"#;
554+
555+
write_file(&temp.path(), "book.toml", book_toml.as_bytes()).unwrap();
556+
557+
let md = MDBook::load(temp.path()).unwrap();
558+
md.build().unwrap();
559+
560+
let index_html = temp.path().join("book").join("index.html");
561+
assert_contains_strings(
562+
index_html,
563+
&vec![
564+
r#"href="https://github.com/rust-lang/mdBook/edit/master/guide/src/README.md" title="Suggest an edit""#,
565+
],
566+
);
567+
}
568+
569+
#[test]
570+
fn edit_url_has_configured_src_dir_edit_url() {
571+
let temp = DummyBook::new().build().unwrap();
572+
let book_toml = r#"
573+
[book]
574+
title = "implicit"
575+
src = "src2"
576+
577+
[output.html]
578+
edit-url-template = "https://github.com/rust-lang/mdBook/edit/master/guide/{path}"
579+
"#;
580+
581+
write_file(&temp.path(), "book.toml", book_toml.as_bytes()).unwrap();
582+
583+
let md = MDBook::load(temp.path()).unwrap();
584+
md.build().unwrap();
585+
586+
let index_html = temp.path().join("book").join("index.html");
587+
assert_contains_strings(
588+
index_html,
589+
&vec![
590+
r#"href="https://github.com/rust-lang/mdBook/edit/master/guide/src2/README.md" title="Suggest an edit""#,
591+
],
592+
);
593+
}
594+
544595
fn remove_absolute_components(path: &Path) -> impl Iterator<Item = Component> + '_ {
545596
path.components().skip_while(|c| match c {
546597
Component::Prefix(_) | Component::RootDir => true,

0 commit comments

Comments
 (0)