Skip to content
This repository was archived by the owner on Oct 27, 2020. It is now read-only.

Commit bcc59f4

Browse files
committed
plugin compatibility with the gitbook 2.0.1
1 parent 91e99bf commit bcc59f4

File tree

2 files changed

+33
-7
lines changed

2 files changed

+33
-7
lines changed

book/plugin.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
require(["gitbook"], function(gitbook) {
2+
gitbook.events.bind("page.change", function() {
3+
// move the edit link to the header, after font-setting icon
4+
$(document.getElementById("edit-link")).insertAfter("#font-settings-wrapper");
5+
});
6+
});

index.js

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
var path = require('path');
22

33
module.exports = {
4+
book: {
5+
assets: "./book",
6+
js: ["plugin.js"]
7+
},
48
hooks: {
59
// After html generation
6-
"page:after": function(page) {
10+
"page": function(page) {
711
var config = this.options.pluginsConfig["edit-link"] || {};
812

913
if (!config.base) {
@@ -14,14 +18,30 @@ module.exports = {
1418
config.label = "Edit This Page";
1519
}
1620

17-
newPath = path.relative(this.options.originalInput, page.rawPath);
21+
// add slash at the end if not present
22+
var base = config.base;
23+
if(base.slice(-1) != "/") {
24+
base = base + "/";
25+
}
26+
27+
// relative path to the page
28+
var newPath = path.relative(this.root, page.rawPath);
29+
30+
// language, if configured
31+
var lang = "";
32+
if(this.context.config.language) {
33+
lang = this.context.config.language + "/";
34+
}
1835

19-
rtEditLink = '<a href="' + config.base + '/' + newPath + '" class="btn fa fa-edit pull-left">&nbsp;&nbsp;' + config.label + '</a>';
36+
rtEditLink = '<a id="edit-link" href="' + base + lang + newPath + '" class="btn fa fa-edit pull-left">&nbsp;&nbsp;' + config.label + '</a>';
2037

21-
page.content = page.content.replace (
22-
'<!-- Actions Right -->',
23-
rtEditLink + '<!-- Actions Right -->'
24-
)
38+
page.sections
39+
.filter(function(section) {
40+
return section.type == 'normal';
41+
})
42+
.forEach(function(section) {
43+
section.content = rtEditLink + section.content;
44+
});
2545

2646
return page;
2747
}

0 commit comments

Comments
 (0)