Skip to content

Commit eb35065

Browse files
committed
support file as function
1 parent 470917a commit eb35065

File tree

3 files changed

+55
-14
lines changed

3 files changed

+55
-14
lines changed

README.md

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,22 @@ A plugin for docsify to generate a `edit on github` button on every pages. click
1717
</script>
1818
```
1919

20-
* `docBase`: the document folder of your github project (e.g.: https://github.com/docsifyjs/docsify/blob/master/docs/)
21-
* `docEditBase`: edit link of your github pages, by default, this is set automatically according to `docBase`
22-
* `title`: the text of the button, default value: `Edit on github`
20+
* `docBase`: [String] the document folder of your github project (e.g.: https://github.com/docsifyjs/docsify/blob/master/docs/)
21+
* `docEditBase`: [String] edit link of your github pages, by default, this is set automatically according to `docBase`
22+
* `title`: [String | Function] the text of the button, default value: `Edit on github`. If passed as function, then the title can be customized according to file path. for example:
23+
```
24+
EditOnGithubPlugin.create(
25+
'https://github.com/docsifyjs/docsify/blob/master/docs/',
26+
null,
27+
function(file) {
28+
if (file.indexOf('en') === -1) {
29+
return '编辑'
30+
} else {
31+
return 'edit on git'
32+
}
33+
}
34+
)
35+
```
2336

2437
[Code example](https://github.com/njleonzhang/vue-data-tables/blob/6bb632419506a14ceff559708180883097d5afa2/docs/index.html#L179-L181)
2538

index.html

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,19 @@
1616
name: 'docsify-edit-on-github',
1717
repo: 'https://github.com/njleonzhang/docsify-edit-on-github',
1818
plugins: [
19+
// EditOnGithubPlugin.create(
20+
// 'https://github.com/njleonzhang/docsify-edit-on-github/blob/master/'
21+
// ),
1922
EditOnGithubPlugin.create(
20-
'https://github.com/njleonzhang/docsify-edit-on-github/blob/master/'
23+
'https://gitlab.meipian.cn/fore/project/article-core/blob/master/',
24+
null,
25+
function(file) {
26+
if (file.indexOf('en') === -1) {
27+
return '编辑'
28+
} else {
29+
return 'edit on git'
30+
}
31+
}
2132
)
2233
]
2334
}

index.js

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
;(function(win) {
2+
function isFunction(functionToCheck) {
3+
return functionToCheck && {}.toString.call(functionToCheck) === '[object Function]'
4+
}
5+
26
win.EditOnGithubPlugin = {}
37

48
function create(docBase, docEditBase, title) {
@@ -20,23 +24,36 @@
2024

2125
win.EditOnGithubPlugin.editDoc = editDoc
2226

23-
return function(hook, vm) {
24-
win.EditOnGithubPlugin.onClick = function(event) {
25-
EditOnGithubPlugin.editDoc(event, vm)
26-
}
27-
28-
var header = [
27+
function generateHeader(title) {
28+
return header = [
2929
'<div style="overflow: auto">',
3030
'<p style="float: right"><a style="text-decoration: underline; cursor: pointer"',
31-
'target="_blank" onclick="EditOnGithubPlugin.onClick(event)">',
31+
'onclick="EditOnGithubPlugin.onClick(event)">',
3232
title,
3333
'</a></p>',
3434
'</div>'
3535
].join('')
36+
}
37+
38+
return function(hook, vm) {
39+
win.EditOnGithubPlugin.onClick = function(event) {
40+
EditOnGithubPlugin.editDoc(event, vm)
41+
}
42+
43+
if (isFunction(title)) {
44+
45+
hook.afterEach(function (html) {
46+
return generateHeader(title(vm.route.file)) + html
47+
})
48+
} else {
49+
var header = generateHeader(title)
50+
51+
hook.afterEach(function (html) {
52+
return header + html
53+
})
54+
}
55+
3656

37-
hook.afterEach(function (html) {
38-
return header + html
39-
})
4057
}
4158
}
4259

0 commit comments

Comments
 (0)