Skip to content

Commit 033d04f

Browse files
committed
[ReviewFix] Replace edit baseurl with template and make visibility independent of git_repository_url.
1 parent ed35321 commit 033d04f

File tree

4 files changed

+25
-29
lines changed

4 files changed

+25
-29
lines changed

book-example/src/format/config.md

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -178,12 +178,15 @@ The following configuration options are available:
178178
an icon link will be output in the menu bar of the book.
179179
- **git-repository-icon:** The FontAwesome icon class to use for the git
180180
repository link. Defaults to `fa-github`.
181-
- **git-repository-edit-baseurl:** The base url for suggesting an edit
182-
to individual pages/chapters of the book. If **git-repository-url** is defined,
183-
defaults to **git-repository-url**/blob/master which works for e.g. GitHub.
184-
The page source path is appended to this url, e.g. `/src/SUMMARY.md`. So when
185-
this or **git-repository-url** is configured an icon link will be output in
186-
the menu bar of the book.
181+
- **git-repository-edit-url-template:** Git repository file edit url
182+
template, when provided shows an "Suggest an edit" button for
183+
directly jumping to editing the currently viewed page in the git
184+
repository. For e.g. GitHub projects set this to
185+
`https://github.com/<owner>/<repo>/edit/master/{path}` or for
186+
Bitbucket projects set it to
187+
`https://bitbucket.org/<owner>/<repo>/src/master/{path}?mode=edit`
188+
where {path} will be replaced with the full path of the file in the
189+
repository.
187190

188191
Available configuration options for the `[output.html.fold]` table:
189192

src/config.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -467,11 +467,11 @@ pub struct HtmlConfig {
467467
/// FontAwesome icon class to use for the Git repository link.
468468
/// Defaults to `fa-github` if `None`.
469469
pub git_repository_icon: Option<String>,
470-
/// Git repository file edit baseurl, below which e.g. src/SUMMARY.md can
471-
/// be viewed/edited
472-
/// Defaults to git_repository_url + `/blob/master` if `None` and
473-
/// git_repository_url is not `None` - works for e.g. GitHub master branch
474-
pub git_repository_edit_baseurl: Option<String>,
470+
/// Git repository file edit url template, when set shows an
471+
/// "Suggest an edit" button for directly jumping to editing the
472+
/// currently viewed page in the git repository. Contains {path}
473+
/// that is replaced with chapter source file path
474+
pub git_repository_edit_url_template: Option<String>,
475475
/// This is used as a bit of a workaround for the `mdbook serve` command.
476476
/// Basically, because you set the websocket port from the command line, the
477477
/// `mdbook serve` command needs a way to let the HTML renderer know where

src/renderer/html_handlebars/hbs_renderer.rs

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,15 @@ impl HtmlHandlebars {
5353
bail!(ErrorKind::ReservedFilenameError(ch.path.clone()));
5454
};
5555

56+
if let Some(ref git_repository_edit_url_template) =
57+
ctx.html_config.git_repository_edit_url_template
58+
{
59+
let full_path = "src/".to_owned() + path;
60+
let edit_url = git_repository_edit_url_template.replace("{path}", &full_path);
61+
ctx.data
62+
.insert("git_repository_edit_url".to_owned(), json!(edit_url));
63+
}
64+
5665
// Non-lexical lifetimes needed :'(
5766
let title: String;
5867
{
@@ -489,22 +498,6 @@ fn make_data(
489498

490499
if let Some(ref git_repository_url) = html_config.git_repository_url {
491500
data.insert("git_repository_url".to_owned(), json!(git_repository_url));
492-
let default_edit_baseurl = git_repository_url.to_owned() + "/blob/master";
493-
let git_repository_edit_baseurl = match html_config.git_repository_edit_baseurl {
494-
Some(ref git_repository_edit_baseurl) => git_repository_edit_baseurl,
495-
None => &default_edit_baseurl,
496-
};
497-
data.insert(
498-
"git_repository_edit_baseurl".to_owned(),
499-
json!(git_repository_edit_baseurl),
500-
);
501-
} else {
502-
if let Some(ref git_repository_edit_baseurl) = html_config.git_repository_edit_baseurl {
503-
data.insert(
504-
"git_repository_edit_baseurl".to_owned(),
505-
json!(git_repository_edit_baseurl),
506-
);
507-
}
508501
}
509502

510503
let git_repository_icon = match html_config.git_repository_icon {

src/theme/index.hbs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,8 @@
131131
<i id="git-repository-button" class="fa {{git_repository_icon}}"></i>
132132
</a>
133133
{{/if}}
134-
{{#if git_repository_edit_baseurl}}
135-
<a href="{{git_repository_edit_baseurl}}/src/{{path}}" title="Suggest an edit" aria-label="Suggest an edit">
134+
{{#if git_repository_edit_url}}
135+
<a href="{{git_repository_edit_url}}" title="Suggest an edit" aria-label="Suggest an edit">
136136
<i id="git-edit-button" class="fa fa-edit"></i>
137137
</a>
138138
{{/if}}

0 commit comments

Comments
 (0)