Skip to content

Commit

Permalink
use book_config.src for edit path
Browse files Browse the repository at this point in the history
use book_config.src for edit path

fmt
  • Loading branch information
joshrotenberg committed May 28, 2021
1 parent 89a2e39 commit 54b1b17
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 8 deletions.
17 changes: 9 additions & 8 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,13 +38,11 @@ impl HtmlHandlebars {
};

if let Some(ref edit_url_template) = ctx.html_config.edit_url_template {
let full_path = "src/".to_owned()
+ ch.source_path
.clone()
.unwrap_or_default()
.to_str()
.unwrap_or_default();
let edit_url = edit_url_template.replace("{path}", &full_path);
let src_path = ctx.book_config.src;
let full_path = src_path.join(ch.source_path.clone().unwrap_or_default());

let edit_url =
edit_url_template.replace("{path}", full_path.to_str().unwrap_or_default());
ctx.data
.insert("git_repository_edit_url".to_owned(), json!(edit_url));
}
Expand Down Expand Up @@ -458,6 +456,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 +519,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 +936,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 54b1b17

Please sign in to comment.