Merge pull request #1506 from flavio/feature/suggest-an-edit-link

Feature/suggest an edit link
This commit is contained in:
Eric Huss 2021-04-26 11:03:50 -07:00 committed by GitHub
commit a72d6002b7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 45 additions and 2 deletions

View File

@ -11,6 +11,7 @@ edition = "2018"
mathjax-support = true mathjax-support = true
site-url = "/mdBook/" site-url = "/mdBook/"
git-repository-url = "https://github.com/rust-lang/mdBook/tree/master/guide" git-repository-url = "https://github.com/rust-lang/mdBook/tree/master/guide"
edit-url-template = "https://github.com/rust-lang/mdBook/edit/master/guide/{path}"
[output.html.playground] [output.html.playground]
editable = true editable = true

View File

@ -201,6 +201,14 @@ 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`.
- **edit-url-template:** Edit url template, when provided shows a
"Suggest an edit" button for directly jumping to editing the currently
viewed page. For e.g. GitHub projects set this to
`https://github.com/<owner>/<repo>/edit/master/{path}` or for
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.
@ -286,6 +294,7 @@ additional-js = ["custom.js"]
no-section-label = false no-section-label = false
git-repository-url = "https://github.com/rust-lang/mdBook" git-repository-url = "https://github.com/rust-lang/mdBook"
git-repository-icon = "fa-github" git-repository-icon = "fa-github"
edit-url-template = "https://github.com/rust-lang/mdBook/edit/master/guide/{path}"
site-url = "/example-book/" site-url = "/example-book/"
cname = "myproject.rs" cname = "myproject.rs"
input-404 = "not-found.md" input-404 = "not-found.md"

View File

@ -160,6 +160,8 @@ pub struct Chapter {
pub sub_items: Vec<BookItem>, pub sub_items: Vec<BookItem>,
/// The chapter's location, relative to the `SUMMARY.md` file. /// The chapter's location, relative to the `SUMMARY.md` file.
pub path: Option<PathBuf>, pub path: Option<PathBuf>,
/// The chapter's source file, relative to the `SUMMARY.md` file.
pub source_path: Option<PathBuf>,
/// An ordered list of the names of each chapter above this one in the hierarchy. /// An ordered list of the names of each chapter above this one in the hierarchy.
pub parent_names: Vec<String>, pub parent_names: Vec<String>,
} }
@ -169,13 +171,15 @@ impl Chapter {
pub fn new<P: Into<PathBuf>>( pub fn new<P: Into<PathBuf>>(
name: &str, name: &str,
content: String, content: String,
path: P, p: P,
parent_names: Vec<String>, parent_names: Vec<String>,
) -> Chapter { ) -> Chapter {
let path: PathBuf = p.into();
Chapter { Chapter {
name: name.to_string(), name: name.to_string(),
content, content,
path: Some(path.into()), path: Some(path.clone()),
source_path: Some(path),
parent_names, parent_names,
..Default::default() ..Default::default()
} }
@ -188,6 +192,7 @@ impl Chapter {
name: name.to_string(), name: name.to_string(),
content: String::new(), content: String::new(),
path: None, path: None,
source_path: None,
parent_names, parent_names,
..Default::default() ..Default::default()
} }
@ -438,6 +443,7 @@ And here is some \
content: String::from("Hello World!"), content: String::from("Hello World!"),
number: Some(SectionNumber(vec![1, 2])), number: Some(SectionNumber(vec![1, 2])),
path: Some(PathBuf::from("second.md")), path: Some(PathBuf::from("second.md")),
source_path: Some(PathBuf::from("second.md")),
parent_names: vec![String::from("Chapter 1")], parent_names: vec![String::from("Chapter 1")],
sub_items: Vec::new(), sub_items: Vec::new(),
}; };
@ -446,6 +452,7 @@ And here is some \
content: String::from(DUMMY_SRC), content: String::from(DUMMY_SRC),
number: None, number: None,
path: Some(PathBuf::from("chapter_1.md")), path: Some(PathBuf::from("chapter_1.md")),
source_path: Some(PathBuf::from("chapter_1.md")),
parent_names: Vec::new(), parent_names: Vec::new(),
sub_items: vec![ sub_items: vec![
BookItem::Chapter(nested.clone()), BookItem::Chapter(nested.clone()),
@ -470,6 +477,7 @@ And here is some \
name: String::from("Chapter 1"), name: String::from("Chapter 1"),
content: String::from(DUMMY_SRC), content: String::from(DUMMY_SRC),
path: Some(PathBuf::from("chapter_1.md")), path: Some(PathBuf::from("chapter_1.md")),
source_path: Some(PathBuf::from("chapter_1.md")),
..Default::default() ..Default::default()
})], })],
..Default::default() ..Default::default()
@ -510,6 +518,7 @@ And here is some \
content: String::from(DUMMY_SRC), content: String::from(DUMMY_SRC),
number: None, number: None,
path: Some(PathBuf::from("Chapter_1/index.md")), path: Some(PathBuf::from("Chapter_1/index.md")),
source_path: Some(PathBuf::from("Chapter_1/index.md")),
parent_names: Vec::new(), parent_names: Vec::new(),
sub_items: vec![ sub_items: vec![
BookItem::Chapter(Chapter::new( BookItem::Chapter(Chapter::new(
@ -562,6 +571,7 @@ And here is some \
content: String::from(DUMMY_SRC), content: String::from(DUMMY_SRC),
number: None, number: None,
path: Some(PathBuf::from("Chapter_1/index.md")), path: Some(PathBuf::from("Chapter_1/index.md")),
source_path: Some(PathBuf::from("Chapter_1/index.md")),
parent_names: Vec::new(), parent_names: Vec::new(),
sub_items: vec![ sub_items: vec![
BookItem::Chapter(Chapter::new( BookItem::Chapter(Chapter::new(

View File

@ -522,6 +522,10 @@ 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
/// directly jumping to editing the currently viewed page.
/// Contains {path} that is replaced with chapter source file path
pub edit_url_template: 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
@ -554,6 +558,7 @@ impl Default for HtmlConfig {
search: None, search: None,
git_repository_url: None, git_repository_url: None,
git_repository_icon: None, git_repository_icon: None,
edit_url_template: None,
input_404: None, input_404: None,
site_url: None, site_url: None,
cname: None, cname: None,

View File

@ -37,6 +37,18 @@ impl HtmlHandlebars {
_ => return Ok(()), _ => return Ok(()),
}; };
if let Some(ref edit_url_template) = ctx.html_config.edit_url_template {
let full_path = "src/".to_owned()
+ ch.source_path
.clone()
.unwrap_or_default()
.to_str()
.unwrap_or_default();
let edit_url = 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 = ch.content.clone();
let content = utils::render_markdown(&content, ctx.html_config.curly_quotes); let content = utils::render_markdown(&content, ctx.html_config.curly_quotes);

View File

@ -148,6 +148,12 @@
<i id="git-repository-button" class="fa {{git_repository_icon}}"></i> <i id="git-repository-button" class="fa {{git_repository_icon}}"></i>
</a> </a>
{{/if}} {{/if}}
{{#if git_repository_edit_url}}
<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>
</a>
{{/if}}
</div> </div>
</div> </div>