Skip to content

Commit

Permalink
feat: plugin hook.
Browse files Browse the repository at this point in the history
  • Loading branch information
Koooooo-7 committed Jun 5, 2022
1 parent 54cc5f9 commit e605fff
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 12 deletions.
19 changes: 17 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
plugins: [
DocsifyCarbon.create('CEBI6KQE', 'docsifyjsorg'),
function (hook, vm) {
hook.beforeEach(function (html) {
let demo = (html, matchedPluginHooks =[]) => {
if (/githubusercontent\.com/.test(vm.route.file)) {
url = vm.route.file
.replace('raw.githubusercontent.com', 'github.com')
Expand All @@ -101,15 +101,30 @@
'https://github.com/docsifyjs/docsify/blob/develop/docs/' +
vm.route.file;
}

var editHtml = '[:memo: Edit Document](' + url + ')\n';
// matchedPluginHooks[0](editHtml)
matchedPluginHooks.forEach(hook=>{
if(typeof hook === 'function'){
hook(html, 10086)
}
})
return (
editHtml +
html +
'\n\n----\n\n' +
'<a href="https://docsify.js.org" target="_blank" style="color: inherit; font-weight: normal; text-decoration: none;">Powered by docsify</a>\n\n' +
'<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>'
);
});
}
demo.enableDocsifyPluginHook=true
demo.pluginMeta = {
name:"koy"
}
hook.beforeEach(
demo

);
},
],
};
Expand Down
41 changes: 33 additions & 8 deletions src/core/init/lifecycle.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { noop } from '../util/core';
import { isFn, noop } from '../util/core';

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

Expand Down Expand Up @@ -27,24 +27,49 @@ export function Lifecycle(Base) {
});
}

callHook(hookName, data, next = noop) {
callHook(hookName, data, next = noop, registeredPluginHooks = []) {
// let myPredictor = (meta)=>{
// return true;
// }

// let myWorker = (ctx, number) => {
// console.log(`Current ctx is ${ctx}`)
// console.log(`Current number is ${number}`)
// }
// registeredPluginHooks.push({predicator:myPredictor, operator:myWorker})

const queue = this._hooks[hookName];
const catchPluginErrors = this.config.catchPluginErrors;

const step = function (index) {
const hookFn = queue[index];

let matchedPluginHooks = [];
if (index >= queue.length) {
next(data);
} else if (typeof hookFn === 'function') {
const errTitle = 'Docsify plugin error';
// find all matched pluginHook

if (hookFn.enableDocsifyPluginHook) {
registeredPluginHooks.forEach(registeredPluginHook => {
const pluginMeta = hookFn.pluginMeta || {};
const hookPredicator = registeredPluginHook.predicator;
isFn(hookPredicator) &&
hookPredicator(pluginMeta) &&
matchedPluginHooks.push(registeredPluginHook.operator);
});
}

if (hookFn.length === 2) {
try {
hookFn(data, result => {
data = result;
step(index + 1);
});
hookFn(
data,
result => {
data = result;
step(index + 1);
},
matchedPluginHooks
);
} catch (err) {
if (catchPluginErrors) {
console.error(errTitle, err);
Expand All @@ -56,7 +81,7 @@ export function Lifecycle(Base) {
}
} else {
try {
const result = hookFn(data);
const result = hookFn(data, matchedPluginHooks);

data = result === undefined ? data : result;
step(index + 1);
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/search/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const CONFIG = {
pathNamespaces: undefined,
};

const install = function (hook, vm) {
const search1 = (hook, vm) => {
const { util } = Docsify;
const opts = vm.config.search || CONFIG;

Expand Down Expand Up @@ -43,4 +43,4 @@ const install = function (hook, vm) {
});
};

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

0 comments on commit e605fff

Please sign in to comment.