From 7aff98a8596bd7d52a54f7729f210b53566b9f8c Mon Sep 17 00:00:00 2001 From: Flavio Castelli Date: Mon, 19 Apr 2021 18:58:15 +0200 Subject: [PATCH] Fix generation of edit links The `IndexPreprocessor` rewrites the path for files named `README.md` to be `index.md`. This breaks the edit link in some circumstances. To address this issues, the `Chapter` struct has now a new attribute called `source_path`. This is initialized with the same value as `path`, but is never ever changed. Finally, the edit link is built by using the `source_path` rather than the `path`. --- src/book/book.rs | 14 ++++++++++++-- src/config.rs | 1 + src/renderer/html_handlebars/hbs_renderer.rs | 7 ++++++- 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/book/book.rs b/src/book/book.rs index 95382960..048aef28 100644 --- a/src/book/book.rs +++ b/src/book/book.rs @@ -160,6 +160,8 @@ pub struct Chapter { pub sub_items: Vec, /// The chapter's location, relative to the `SUMMARY.md` file. pub path: Option, + /// The chapter's source file, relative to the `SUMMARY.md` file. + pub source_path: Option, /// An ordered list of the names of each chapter above this one in the hierarchy. pub parent_names: Vec, } @@ -169,13 +171,15 @@ impl Chapter { pub fn new>( name: &str, content: String, - path: P, + p: P, parent_names: Vec, ) -> Chapter { + let path: PathBuf = p.into(); Chapter { name: name.to_string(), content, - path: Some(path.into()), + path: Some(path.clone()), + source_path: Some(path), parent_names, ..Default::default() } @@ -188,6 +192,7 @@ impl Chapter { name: name.to_string(), content: String::new(), path: None, + source_path: None, parent_names, ..Default::default() } @@ -438,6 +443,7 @@ And here is some \ content: String::from("Hello World!"), number: Some(SectionNumber(vec![1, 2])), path: Some(PathBuf::from("second.md")), + source_path: Some(PathBuf::from("second.md")), parent_names: vec![String::from("Chapter 1")], sub_items: Vec::new(), }; @@ -446,6 +452,7 @@ And here is some \ content: String::from(DUMMY_SRC), number: None, path: Some(PathBuf::from("chapter_1.md")), + source_path: Some(PathBuf::from("chapter_1.md")), parent_names: Vec::new(), sub_items: vec![ BookItem::Chapter(nested.clone()), @@ -470,6 +477,7 @@ And here is some \ name: String::from("Chapter 1"), content: String::from(DUMMY_SRC), path: Some(PathBuf::from("chapter_1.md")), + source_path: Some(PathBuf::from("chapter_1.md")), ..Default::default() })], ..Default::default() @@ -510,6 +518,7 @@ And here is some \ content: String::from(DUMMY_SRC), number: None, path: Some(PathBuf::from("Chapter_1/index.md")), + source_path: Some(PathBuf::from("Chapter_1/index.md")), parent_names: Vec::new(), sub_items: vec![ BookItem::Chapter(Chapter::new( @@ -562,6 +571,7 @@ And here is some \ content: String::from(DUMMY_SRC), number: None, path: Some(PathBuf::from("Chapter_1/index.md")), + source_path: Some(PathBuf::from("Chapter_1/index.md")), parent_names: Vec::new(), sub_items: vec![ BookItem::Chapter(Chapter::new( diff --git a/src/config.rs b/src/config.rs index 061a2499..e80e21c4 100644 --- a/src/config.rs +++ b/src/config.rs @@ -559,6 +559,7 @@ impl Default for HtmlConfig { search: None, git_repository_url: None, git_repository_icon: None, + git_repository_edit_url_template: None, input_404: None, site_url: None, cname: None, diff --git a/src/renderer/html_handlebars/hbs_renderer.rs b/src/renderer/html_handlebars/hbs_renderer.rs index 9279d5d3..e1cbab71 100644 --- a/src/renderer/html_handlebars/hbs_renderer.rs +++ b/src/renderer/html_handlebars/hbs_renderer.rs @@ -40,7 +40,12 @@ impl HtmlHandlebars { if let Some(ref git_repository_edit_url_template) = ctx.html_config.git_repository_edit_url_template { - let full_path = "src/".to_owned() + path.to_str().unwrap(); + let full_path = "src/".to_owned() + + ch.source_path + .clone() + .unwrap_or_default() + .to_str() + .unwrap_or_default(); let edit_url = git_repository_edit_url_template.replace("{path}", &full_path); ctx.data .insert("git_repository_edit_url".to_owned(), json!(edit_url));