Skip to content

Commit e605fff

Browse files
committed
feat: plugin hook.
1 parent 54cc5f9 commit e605fff

File tree

3 files changed

+52
-12
lines changed

3 files changed

+52
-12
lines changed

index.html

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@
8787
plugins: [
8888
DocsifyCarbon.create('CEBI6KQE', 'docsifyjsorg'),
8989
function (hook, vm) {
90-
hook.beforeEach(function (html) {
90+
let demo = (html, matchedPluginHooks =[]) => {
9191
if (/githubusercontent\.com/.test(vm.route.file)) {
9292
url = vm.route.file
9393
.replace('raw.githubusercontent.com', 'github.com')
@@ -101,15 +101,30 @@
101101
'https://github.com/docsifyjs/docsify/blob/develop/docs/' +
102102
vm.route.file;
103103
}
104+
104105
var editHtml = '[:memo: Edit Document](' + url + ')\n';
106+
// matchedPluginHooks[0](editHtml)
107+
matchedPluginHooks.forEach(hook=>{
108+
if(typeof hook === 'function'){
109+
hook(html, 10086)
110+
}
111+
})
105112
return (
106113
editHtml +
107114
html +
108115
'\n\n----\n\n' +
109116
'<a href="https://docsify.js.org" target="_blank" style="color: inherit; font-weight: normal; text-decoration: none;">Powered by docsify</a>\n\n' +
110117
'<a href="https://vercel.com/?utm_source=docsifyjs&utm_campaign=oss" target="_blank" title="Vercel has given us a Pro account"><img src="/docs/_media/powered-by-vercel.svg" alt="Vercel" width="150"></a>'
111118
);
112-
});
119+
}
120+
demo.enableDocsifyPluginHook=true
121+
demo.pluginMeta = {
122+
name:"koy"
123+
}
124+
hook.beforeEach(
125+
demo
126+
127+
);
113128
},
114129
],
115130
};

src/core/init/lifecycle.js

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { noop } from '../util/core';
1+
import { isFn, noop } from '../util/core';
22

33
/** @typedef {import('../Docsify').Constructor} Constructor */
44

@@ -27,24 +27,49 @@ export function Lifecycle(Base) {
2727
});
2828
}
2929

30-
callHook(hookName, data, next = noop) {
30+
callHook(hookName, data, next = noop, registeredPluginHooks = []) {
31+
// let myPredictor = (meta)=>{
32+
// return true;
33+
// }
34+
35+
// let myWorker = (ctx, number) => {
36+
// console.log(`Current ctx is ${ctx}`)
37+
// console.log(`Current number is ${number}`)
38+
// }
39+
// registeredPluginHooks.push({predicator:myPredictor, operator:myWorker})
40+
3141
const queue = this._hooks[hookName];
3242
const catchPluginErrors = this.config.catchPluginErrors;
3343

3444
const step = function (index) {
3545
const hookFn = queue[index];
36-
46+
let matchedPluginHooks = [];
3747
if (index >= queue.length) {
3848
next(data);
3949
} else if (typeof hookFn === 'function') {
4050
const errTitle = 'Docsify plugin error';
51+
// find all matched pluginHook
52+
53+
if (hookFn.enableDocsifyPluginHook) {
54+
registeredPluginHooks.forEach(registeredPluginHook => {
55+
const pluginMeta = hookFn.pluginMeta || {};
56+
const hookPredicator = registeredPluginHook.predicator;
57+
isFn(hookPredicator) &&
58+
hookPredicator(pluginMeta) &&
59+
matchedPluginHooks.push(registeredPluginHook.operator);
60+
});
61+
}
4162

4263
if (hookFn.length === 2) {
4364
try {
44-
hookFn(data, result => {
45-
data = result;
46-
step(index + 1);
47-
});
65+
hookFn(
66+
data,
67+
result => {
68+
data = result;
69+
step(index + 1);
70+
},
71+
matchedPluginHooks
72+
);
4873
} catch (err) {
4974
if (catchPluginErrors) {
5075
console.error(errTitle, err);
@@ -56,7 +81,7 @@ export function Lifecycle(Base) {
5681
}
5782
} else {
5883
try {
59-
const result = hookFn(data);
84+
const result = hookFn(data, matchedPluginHooks);
6085

6186
data = result === undefined ? data : result;
6287
step(index + 1);

src/plugins/search/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const CONFIG = {
1313
pathNamespaces: undefined,
1414
};
1515

16-
const install = function (hook, vm) {
16+
const search1 = (hook, vm) => {
1717
const { util } = Docsify;
1818
const opts = vm.config.search || CONFIG;
1919

@@ -43,4 +43,4 @@ const install = function (hook, vm) {
4343
});
4444
};
4545

46-
$docsify.plugins = [].concat(install, $docsify.plugins);
46+
$docsify.plugins = [].concat(search1, $docsify.plugins);

0 commit comments

Comments
 (0)