2017-02-22 21:29:26 +08:00
|
|
|
# Write a plugin
|
|
|
|
|
2017-03-10 05:19:07 +08:00
|
|
|
A plugin is simply a function that takes `hook` as an argument. The hook supports handling of asynchronous tasks.
|
2017-02-22 21:29:26 +08:00
|
|
|
|
|
|
|
## Full configuration
|
|
|
|
|
|
|
|
```js
|
|
|
|
window.$docsify = {
|
|
|
|
plugins: [
|
|
|
|
function (hook, vm) {
|
|
|
|
hook.init(function() {
|
|
|
|
// Called when the script starts running, only trigger once, no arguments,
|
|
|
|
})
|
|
|
|
|
|
|
|
hook.beforeEach(function(content) {
|
|
|
|
// Invoked each time before parsing the Markdown file.
|
|
|
|
// ...
|
|
|
|
return content
|
|
|
|
})
|
|
|
|
|
|
|
|
hook.afterEach(function(html, next) {
|
|
|
|
// Invoked each time after the Markdown file is parsed.
|
|
|
|
// beforeEach and afterEach support asynchronous。
|
|
|
|
// ...
|
|
|
|
// call `next(html)` when task is done.
|
|
|
|
next(html)
|
|
|
|
})
|
|
|
|
|
|
|
|
hook.doneEach(function() {
|
|
|
|
// Invoked each time after the data is fully loaded, no arguments,
|
|
|
|
// ...
|
|
|
|
})
|
|
|
|
|
|
|
|
hook.mounted(function() {
|
|
|
|
// Called after initial completion. Only trigger once, no arguments.
|
|
|
|
})
|
|
|
|
|
|
|
|
hook.ready(function() {
|
|
|
|
// Called after initial completion, no arguments.
|
|
|
|
})
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
!> You can get internal methods through `window.Docsify`. Get the current instance through the second argument.
|
|
|
|
|
|
|
|
## Example
|
|
|
|
|
2017-05-30 20:14:31 +08:00
|
|
|
#### footer
|
|
|
|
|
2017-02-22 21:29:26 +08:00
|
|
|
Add footer component in each pages.
|
|
|
|
|
|
|
|
```js
|
|
|
|
window.$docsify = {
|
|
|
|
plugins: [
|
|
|
|
function (hook) {
|
|
|
|
var footer = [
|
|
|
|
'<hr/>',
|
|
|
|
'<footer>',
|
|
|
|
'<span><a href="https://github.com/QingWei-Li">cinwell</a> ©2017.</span>',
|
|
|
|
'<span>Proudly published with <a href="https://github.com/QingWei-Li/docsify" target="_blank">docsify</a>.</span>',
|
|
|
|
'</footer>'
|
|
|
|
].join('')
|
|
|
|
|
|
|
|
hook.afterEach(function (html) {
|
|
|
|
return html + footer
|
|
|
|
})
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
2017-03-10 05:19:07 +08:00
|
|
|
```
|
2017-05-30 20:14:31 +08:00
|
|
|
|
|
|
|
### Edit Button
|
|
|
|
|
|
|
|
```js
|
|
|
|
window.$docsify = {
|
|
|
|
plugins: [
|
|
|
|
function(hook, vm) {
|
|
|
|
hook.beforeEach(function (html) {
|
2017-05-31 08:39:01 +08:00
|
|
|
var url = 'https://github.com/QingWei-Li/docsify/blob/master/docs' + vm.route.file
|
2017-05-30 20:14:31 +08:00
|
|
|
var editHtml = '[📝 EDIT DOCUMENT](' + url + ')\n'
|
|
|
|
|
|
|
|
return editHtml
|
|
|
|
+ html
|
|
|
|
+ '\n----\n'
|
|
|
|
+ 'Last modified {docsify-updated} '
|
|
|
|
+ editHtml
|
|
|
|
})
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
```
|