[ReviewFix] Replace edit baseurl with template and make visibility independent of git_repository_url.

This commit is contained in:
Jonas Berlin 2020-02-18 00:41:03 +02:00 committed by Ruben Moor
parent a6f13ff54d
commit 6f109aac57
4 changed files with 21 additions and 41 deletions

View File

@ -201,12 +201,15 @@ The following configuration options are available:
an icon link will be output in the menu bar of the book. 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 - **git-repository-icon:** The FontAwesome icon class to use for the git
repository link. Defaults to `fa-github`. repository link. Defaults to `fa-github`.
- **git-repository-edit-baseurl:** The base url for suggesting an edit - **git-repository-edit-url-template:** Git repository file edit url
to individual pages/chapters of the book. If **git-repository-url** is defined, template, when provided shows an "Suggest an edit" button for
defaults to **git-repository-url**/blob/master which works for e.g. GitHub. directly jumping to editing the currently viewed page in the git
The page source path is appended to this url, e.g. `/src/SUMMARY.md`. So when repository. For e.g. GitHub projects set this to
this or **git-repository-url** is configured an icon link will be output in `https://github.com/<owner>/<repo>/edit/master/{path}` or for
the menu bar of the book. Bitbucket projects set it to
`https://bitbucket.org/<owner>/<repo>/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. - **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 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. needs to be created, as an absolute path from the build directory, (e.g.

View File

@ -522,19 +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 /// [custom domain]: https://docs.github.com/en/github/working-with-github-pages/managing-a-custom-domain-for-your-github-pages-site
pub cname: Option<String>, pub cname: Option<String>,
/// Edit url template, when set shows a "Suggest an edit" button for /// Git repository file edit url template, when set shows an
/// directly jumping to editing the currently viewed page. /// "Suggest an edit" button for directly jumping to editing the
/// Contains {path} that is replaced with chapter source file path /// currently viewed page in the git repository. Contains {path}
pub edit_url_template: Option<String>, /// that is replaced with chapter source file path
/// Endpoint of websocket, for livereload usage. Value loaded from .toml file pub git_repository_edit_url_template: Option<String>,
/// is ignored, because our code overrides this field with the value [`LIVE_RELOAD_ENDPOINT`]
///
/// [`LIVE_RELOAD_ENDPOINT`]: cmd::serve::LIVE_RELOAD_ENDPOINT
/// 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<String>,
/// This is used as a bit of a workaround for the `mdbook serve` command. /// 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 /// 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 /// `mdbook serve` command needs a way to let the HTML renderer know where

View File

@ -37,16 +37,11 @@ impl HtmlHandlebars {
_ => return Ok(()), _ => return Ok(()),
}; };
if let Some(ref edit_url_template) = ctx.html_config.edit_url_template { if let Some(ref git_repository_edit_url_template) =
let full_path = ctx.book_config.src.to_str().unwrap_or_default().to_owned() ctx.html_config.git_repository_edit_url_template
+ "/" {
+ ch.source_path let full_path = "src/".to_owned() + path.to_str().unwrap();
.clone() let edit_url = git_repository_edit_url_template.replace("{path}", &full_path);
.unwrap_or_default()
.to_str()
.unwrap_or_default();
let edit_url = edit_url_template.replace("{path}", &full_path);
ctx.data ctx.data
.insert("git_repository_edit_url".to_owned(), json!(edit_url)); .insert("git_repository_edit_url".to_owned(), json!(edit_url));
} }
@ -766,16 +761,6 @@ fn make_data(
if let Some(ref git_repository_url) = html_config.git_repository_url { if let Some(ref git_repository_url) = html_config.git_repository_url {
data.insert("git_repository_url".to_owned(), json!(git_repository_url)); data.insert("git_repository_url".to_owned(), json!(git_repository_url));
let defaultEditBaseUrl = 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 => &defaultEditBaseUrl,
};
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 { let git_repository_icon = match html_config.git_repository_icon {

View File

@ -152,8 +152,8 @@
<i id="git-repository-button" class="fab {{git_repository_icon}}"></i> <i id="git-repository-button" class="fab {{git_repository_icon}}"></i>
</a> </a>
{{/if}} {{/if}}
{{#if git_repository_edit_baseurl}} {{#if git_repository_edit_url}}
<a href="{{git_repository_edit_baseurl}}/src/{{path}}" title="Suggest an edit" aria-label="Suggest an edit"> <a href="{{git_repository_edit_url}}" title="Suggest an edit" aria-label="Suggest an edit">
<i id="git-edit-button" class="fa fa-edit"></i> <i id="git-edit-button" class="fa fa-edit"></i>
</a> </a>
{{/if}} {{/if}}