From bbf54d7459889697c74101bd0cd4f45f6c0d2ca0 Mon Sep 17 00:00:00 2001 From: Jonas Berlin Date: Tue, 18 Feb 2020 00:41:03 +0200 Subject: [PATCH] [ReviewFix] Replace edit baseurl with template and make visibility independent of git_repository_url. --- guide/src/format/config.md | 15 +++++++----- src/config.rs | 10 ++++---- src/renderer/html_handlebars/hbs_renderer.rs | 25 +++++++------------- src/theme/index.hbs | 4 ++-- 4 files changed, 25 insertions(+), 29 deletions(-) diff --git a/guide/src/format/config.md b/guide/src/format/config.md index f1f65128..7b19654b 100644 --- a/guide/src/format/config.md +++ b/guide/src/format/config.md @@ -201,12 +201,15 @@ The following configuration options are available: an icon link will be output in the menu bar of the book. - **git-repository-icon:** The FontAwesome icon class to use for the git repository link. Defaults to `fa-github`. -- **git-repository-edit-baseurl:** The base url for suggesting an edit - to individual pages/chapters of the book. If **git-repository-url** is defined, - defaults to **git-repository-url**/blob/master which works for e.g. GitHub. - The page source path is appended to this url, e.g. `/src/SUMMARY.md`. So when - this or **git-repository-url** is configured an icon link will be output in - the menu bar of the book. +- **git-repository-edit-url-template:** Git repository file edit url + template, when provided shows an "Suggest an edit" button for + directly jumping to editing the currently viewed page in the git + repository. For e.g. GitHub projects set this to + `https://github.com///edit/master/{path}` or for + Bitbucket projects set it to + `https://bitbucket.org///src/master/{path}?mode=edit` + where {path} will be replaced with the full path of the file in the + repository. - **redirect:** A subtable used for generating redirects when a page is moved. The table contains key-value pairs where the key is where the redirect file needs to be created, as an absolute path from the build directory, (e.g. diff --git a/src/config.rs b/src/config.rs index 1191542b..061a2499 100644 --- a/src/config.rs +++ b/src/config.rs @@ -522,11 +522,11 @@ pub struct HtmlConfig { /// /// [custom domain]: https://docs.github.com/en/github/working-with-github-pages/managing-a-custom-domain-for-your-github-pages-site pub cname: Option, - /// Git repository file edit baseurl, below which e.g. src/SUMMARY.md can - /// be viewed/edited - /// Defaults to git_repository_url + `/blob/master` if `None` and - /// git_repository_url is not `None` - works for e.g. GitHub master branch - pub git_repository_edit_baseurl: Option, + /// Git repository file edit url template, when set shows an + /// "Suggest an edit" button for directly jumping to editing the + /// currently viewed page in the git repository. Contains {path} + /// that is replaced with chapter source file path + pub git_repository_edit_url_template: Option, /// This is used as a bit of a workaround for the `mdbook serve` command. /// Basically, because you set the websocket port from the command line, the /// `mdbook serve` command needs a way to let the HTML renderer know where diff --git a/src/renderer/html_handlebars/hbs_renderer.rs b/src/renderer/html_handlebars/hbs_renderer.rs index b0903e7b..9279d5d3 100644 --- a/src/renderer/html_handlebars/hbs_renderer.rs +++ b/src/renderer/html_handlebars/hbs_renderer.rs @@ -37,6 +37,15 @@ impl HtmlHandlebars { _ => return Ok(()), }; + 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 edit_url = git_repository_edit_url_template.replace("{path}", &full_path); + ctx.data + .insert("git_repository_edit_url".to_owned(), json!(edit_url)); + } + let content = ch.content.clone(); let content = utils::render_markdown(&content, ctx.html_config.curly_quotes); @@ -680,22 +689,6 @@ fn make_data( if let Some(ref git_repository_url) = html_config.git_repository_url { data.insert("git_repository_url".to_owned(), json!(git_repository_url)); - let default_edit_baseurl = git_repository_url.to_owned() + "/blob/master"; - let git_repository_edit_baseurl = match html_config.git_repository_edit_baseurl { - Some(ref git_repository_edit_baseurl) => git_repository_edit_baseurl, - None => &default_edit_baseurl, - }; - data.insert( - "git_repository_edit_baseurl".to_owned(), - json!(git_repository_edit_baseurl), - ); - } else { - if let Some(ref git_repository_edit_baseurl) = html_config.git_repository_edit_baseurl { - data.insert( - "git_repository_edit_baseurl".to_owned(), - json!(git_repository_edit_baseurl), - ); - } } let git_repository_icon = match html_config.git_repository_icon { diff --git a/src/theme/index.hbs b/src/theme/index.hbs index a1cbe6e4..669690b4 100644 --- a/src/theme/index.hbs +++ b/src/theme/index.hbs @@ -148,8 +148,8 @@ {{/if}} - {{#if git_repository_edit_baseurl}} - + {{#if git_repository_edit_url}} + {{/if}}