2018-07-01 07:27:01 -04:00
|
|
|
|
# 自定义插件
|
|
|
|
|
|
|
|
|
|
docsify 提供了一套插件机制,其中提供的钩子(hook)支持处理异步逻辑,可以很方便的扩展功能。
|
|
|
|
|
|
|
|
|
|
## 完整功能
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
window.$docsify = {
|
2020-04-26 01:54:44 -04:00
|
|
|
|
plugins: [
|
|
|
|
|
function(hook, vm) {
|
|
|
|
|
hook.init(function() {
|
|
|
|
|
// 初始化完成后调用,只调用一次,没有参数。
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
hook.beforeEach(function(content) {
|
|
|
|
|
// 每次开始解析 Markdown 内容时调用
|
|
|
|
|
// ...
|
|
|
|
|
return content;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
hook.afterEach(function(html, next) {
|
|
|
|
|
// 解析成 html 后调用。
|
|
|
|
|
// beforeEach 和 afterEach 支持处理异步逻辑
|
|
|
|
|
// ...
|
|
|
|
|
// 异步处理完成后调用 next(html) 返回结果
|
|
|
|
|
next(html);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
hook.doneEach(function() {
|
|
|
|
|
// 每次路由切换时数据全部加载完成后调用,没有参数。
|
|
|
|
|
// ...
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
hook.mounted(function() {
|
|
|
|
|
// 初始化并第一次加载完成数据后调用,只触发一次,没有参数。
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
hook.ready(function() {
|
|
|
|
|
// 初始化并第一次加载完成数据后调用,没有参数。
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
};
|
2018-07-01 07:27:01 -04:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
!> 如果需要用 docsify 的内部方法,可以通过 `window.Docsify` 获取,通过 `vm` 获取当前实例。
|
|
|
|
|
|
|
|
|
|
## 例子
|
|
|
|
|
|
|
|
|
|
|
2020-04-26 01:54:44 -04:00
|
|
|
|
#### footer
|
2018-07-01 07:27:01 -04:00
|
|
|
|
|
|
|
|
|
给每个页面的末尾加上 `footer`
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
window.$docsify = {
|
|
|
|
|
plugins: [
|
2020-04-26 01:54:44 -04:00
|
|
|
|
function(hook) {
|
2018-07-01 07:27:01 -04:00
|
|
|
|
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/docsifyjs/docsify" target="_blank">docsify</a>.</span>',
|
|
|
|
|
'</footer>'
|
2020-04-26 01:54:44 -04:00
|
|
|
|
].join('');
|
2018-07-01 07:27:01 -04:00
|
|
|
|
|
2020-04-26 01:54:44 -04:00
|
|
|
|
hook.afterEach(function(html) {
|
|
|
|
|
return html + footer;
|
|
|
|
|
});
|
2018-07-01 07:27:01 -04:00
|
|
|
|
}
|
|
|
|
|
]
|
2020-04-26 01:54:44 -04:00
|
|
|
|
};
|
2018-07-01 07:27:01 -04:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
### Edit Button
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
window.$docsify = {
|
|
|
|
|
plugins: [
|
|
|
|
|
function(hook, vm) {
|
2020-04-26 01:54:44 -04:00
|
|
|
|
hook.beforeEach(function(html) {
|
|
|
|
|
var url =
|
|
|
|
|
'https://github.com/docsifyjs/docsify/blob/master/docs/' +
|
|
|
|
|
vm.route.file;
|
|
|
|
|
var editHtml = '[📝 EDIT DOCUMENT](' + url + ')\n';
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
editHtml +
|
|
|
|
|
html +
|
|
|
|
|
'\n----\n' +
|
|
|
|
|
'Last modified {docsify-updated} ' +
|
|
|
|
|
editHtml
|
|
|
|
|
);
|
|
|
|
|
});
|
2018-07-01 07:27:01 -04:00
|
|
|
|
}
|
|
|
|
|
]
|
2020-04-26 01:54:44 -04:00
|
|
|
|
};
|
2018-07-01 07:27:01 -04:00
|
|
|
|
```
|
2020-04-26 01:54:44 -04:00
|
|
|
|
|
|
|
|
|
## 小技巧
|
|
|
|
|
|
|
|
|
|
### 获取 docsify 版本
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
console.log(window.Docsify.version)
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
当前版本: <span id='tip-version'>正在加载</span>
|
|
|
|
|
|
|
|
|
|
<script>
|
|
|
|
|
document.getElementById('tip-version').innerText = Docsify.version
|
|
|
|
|
</script>
|