Skip to content

Commit

Permalink
Merge pull request #1554 from joshrotenberg/edit_url_custom_src
Browse files Browse the repository at this point in the history
Use the configured book src directory for the edit url template path
  • Loading branch information
ehuss committed Jun 2, 2021
2 parents e9e889f + 60aaa7a commit d325c60
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/renderer/html_handlebars/hbs_renderer.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -936,6 +940,7 @@ struct RenderItemContext<'a> {
destination: PathBuf,
data: serde_json::Map<String, serde_json::Value>,
is_index: bool,
book_config: BookConfig,
html_config: HtmlConfig,
edition: Option<RustEdition>,
chapter_titles: &'a HashMap<PathBuf, String>,
Expand Down
51 changes: 51 additions & 0 deletions tests/rendered_output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Item = Component> + '_ {
path.components().skip_while(|c| match c {
Component::Prefix(_) | Component::RootDir => true,
Expand Down

0 comments on commit d325c60

Please sign in to comment.