diff --git a/book-example/src/format/config.md b/book-example/src/format/config.md index e1145aa81d..cfde945a2e 100644 --- a/book-example/src/format/config.md +++ b/book-example/src/format/config.md @@ -178,6 +178,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-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. Available configuration options for the `[output.html.fold]` table: diff --git a/src/config.rs b/src/config.rs index 18d70ed751..6bacc114ac 100644 --- a/src/config.rs +++ b/src/config.rs @@ -467,6 +467,11 @@ pub struct HtmlConfig { /// FontAwesome icon class to use for the Git repository link. /// Defaults to `fa-github` if `None`. pub git_repository_icon: 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 04284c50a6..962bee6e92 100644 --- a/src/renderer/html_handlebars/hbs_renderer.rs +++ b/src/renderer/html_handlebars/hbs_renderer.rs @@ -53,6 +53,15 @@ impl HtmlHandlebars { bail!(ErrorKind::ReservedFilenameError(ch.path.clone())); }; + if let Some(ref git_repository_edit_url_template) = + ctx.html_config.git_repository_edit_url_template + { + let full_path = "src/".to_owned() + path; + let edit_url = git_repository_edit_url_template.replace("{path}", &full_path); + ctx.data + .insert("git_repository_edit_url".to_owned(), json!(edit_url)); + } + // Non-lexical lifetimes needed :'( let title: String; { diff --git a/src/theme/index.hbs b/src/theme/index.hbs index b205670f70..0906fc3973 100644 --- a/src/theme/index.hbs +++ b/src/theme/index.hbs @@ -131,6 +131,11 @@ {{/if}} + {{#if git_repository_edit_url}} + + + + {{/if}}