Skip to content

Commit a72d600

Browse files
authored
Merge pull request #1506 from flavio/feature/suggest-an-edit-link
Feature/suggest an edit link
2 parents b54e73e + 7525b35 commit a72d600

File tree

6 files changed

+45
-2
lines changed

6 files changed

+45
-2
lines changed

guide/book.toml

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ edition = "2018"
1111
mathjax-support = true
1212
site-url = "/mdBook/"
1313
git-repository-url = "https://github.com/rust-lang/mdBook/tree/master/guide"
14+
edit-url-template = "https://github.com/rust-lang/mdBook/edit/master/guide/{path}"
1415

1516
[output.html.playground]
1617
editable = true

guide/src/format/config.md

+9
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,14 @@ The following configuration options are available:
201201
an icon link will be output in the menu bar of the book.
202202
- **git-repository-icon:** The FontAwesome icon class to use for the git
203203
repository link. Defaults to `fa-github`.
204+
- **edit-url-template:** Edit url template, when provided shows a
205+
"Suggest an edit" button for directly jumping to editing the currently
206+
viewed page. For e.g. GitHub projects set this to
207+
`https://github.com/<owner>/<repo>/edit/master/{path}` or for
208+
Bitbucket projects set it to
209+
`https://bitbucket.org/<owner>/<repo>/src/master/{path}?mode=edit`
210+
where {path} will be replaced with the full path of the file in the
211+
repository.
204212
- **redirect:** A subtable used for generating redirects when a page is moved.
205213
The table contains key-value pairs where the key is where the redirect file
206214
needs to be created, as an absolute path from the build directory, (e.g.
@@ -286,6 +294,7 @@ additional-js = ["custom.js"]
286294
no-section-label = false
287295
git-repository-url = "https://github.com/rust-lang/mdBook"
288296
git-repository-icon = "fa-github"
297+
edit-url-template = "https://github.com/rust-lang/mdBook/edit/master/guide/{path}"
289298
site-url = "/example-book/"
290299
cname = "myproject.rs"
291300
input-404 = "not-found.md"

src/book/book.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,8 @@ pub struct Chapter {
160160
pub sub_items: Vec<BookItem>,
161161
/// The chapter's location, relative to the `SUMMARY.md` file.
162162
pub path: Option<PathBuf>,
163+
/// The chapter's source file, relative to the `SUMMARY.md` file.
164+
pub source_path: Option<PathBuf>,
163165
/// An ordered list of the names of each chapter above this one in the hierarchy.
164166
pub parent_names: Vec<String>,
165167
}
@@ -169,13 +171,15 @@ impl Chapter {
169171
pub fn new<P: Into<PathBuf>>(
170172
name: &str,
171173
content: String,
172-
path: P,
174+
p: P,
173175
parent_names: Vec<String>,
174176
) -> Chapter {
177+
let path: PathBuf = p.into();
175178
Chapter {
176179
name: name.to_string(),
177180
content,
178-
path: Some(path.into()),
181+
path: Some(path.clone()),
182+
source_path: Some(path),
179183
parent_names,
180184
..Default::default()
181185
}
@@ -188,6 +192,7 @@ impl Chapter {
188192
name: name.to_string(),
189193
content: String::new(),
190194
path: None,
195+
source_path: None,
191196
parent_names,
192197
..Default::default()
193198
}
@@ -438,6 +443,7 @@ And here is some \
438443
content: String::from("Hello World!"),
439444
number: Some(SectionNumber(vec![1, 2])),
440445
path: Some(PathBuf::from("second.md")),
446+
source_path: Some(PathBuf::from("second.md")),
441447
parent_names: vec![String::from("Chapter 1")],
442448
sub_items: Vec::new(),
443449
};
@@ -446,6 +452,7 @@ And here is some \
446452
content: String::from(DUMMY_SRC),
447453
number: None,
448454
path: Some(PathBuf::from("chapter_1.md")),
455+
source_path: Some(PathBuf::from("chapter_1.md")),
449456
parent_names: Vec::new(),
450457
sub_items: vec![
451458
BookItem::Chapter(nested.clone()),
@@ -470,6 +477,7 @@ And here is some \
470477
name: String::from("Chapter 1"),
471478
content: String::from(DUMMY_SRC),
472479
path: Some(PathBuf::from("chapter_1.md")),
480+
source_path: Some(PathBuf::from("chapter_1.md")),
473481
..Default::default()
474482
})],
475483
..Default::default()
@@ -510,6 +518,7 @@ And here is some \
510518
content: String::from(DUMMY_SRC),
511519
number: None,
512520
path: Some(PathBuf::from("Chapter_1/index.md")),
521+
source_path: Some(PathBuf::from("Chapter_1/index.md")),
513522
parent_names: Vec::new(),
514523
sub_items: vec![
515524
BookItem::Chapter(Chapter::new(
@@ -562,6 +571,7 @@ And here is some \
562571
content: String::from(DUMMY_SRC),
563572
number: None,
564573
path: Some(PathBuf::from("Chapter_1/index.md")),
574+
source_path: Some(PathBuf::from("Chapter_1/index.md")),
565575
parent_names: Vec::new(),
566576
sub_items: vec![
567577
BookItem::Chapter(Chapter::new(

src/config.rs

+5
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,10 @@ pub struct HtmlConfig {
522522
///
523523
/// [custom domain]: https://docs.github.com/en/github/working-with-github-pages/managing-a-custom-domain-for-your-github-pages-site
524524
pub cname: Option<String>,
525+
/// Edit url template, when set shows a "Suggest an edit" button for
526+
/// directly jumping to editing the currently viewed page.
527+
/// Contains {path} that is replaced with chapter source file path
528+
pub edit_url_template: Option<String>,
525529
/// This is used as a bit of a workaround for the `mdbook serve` command.
526530
/// Basically, because you set the websocket port from the command line, the
527531
/// `mdbook serve` command needs a way to let the HTML renderer know where
@@ -554,6 +558,7 @@ impl Default for HtmlConfig {
554558
search: None,
555559
git_repository_url: None,
556560
git_repository_icon: None,
561+
edit_url_template: None,
557562
input_404: None,
558563
site_url: None,
559564
cname: None,

src/renderer/html_handlebars/hbs_renderer.rs

+12
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,18 @@ impl HtmlHandlebars {
3737
_ => return Ok(()),
3838
};
3939

40+
if let Some(ref edit_url_template) = ctx.html_config.edit_url_template {
41+
let full_path = "src/".to_owned()
42+
+ ch.source_path
43+
.clone()
44+
.unwrap_or_default()
45+
.to_str()
46+
.unwrap_or_default();
47+
let edit_url = edit_url_template.replace("{path}", &full_path);
48+
ctx.data
49+
.insert("git_repository_edit_url".to_owned(), json!(edit_url));
50+
}
51+
4052
let content = ch.content.clone();
4153
let content = utils::render_markdown(&content, ctx.html_config.curly_quotes);
4254

src/theme/index.hbs

+6
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,12 @@
148148
<i id="git-repository-button" class="fa {{git_repository_icon}}"></i>
149149
</a>
150150
{{/if}}
151+
{{#if git_repository_edit_url}}
152+
<a href="{{git_repository_edit_url}}" title="Suggest an edit" aria-label="Suggest an edit">
153+
<i id="git-edit-button" class="fa fa-edit"></i>
154+
</a>
155+
{{/if}}
156+
151157
</div>
152158
</div>
153159

0 commit comments

Comments
 (0)