From e605fff301f80a0d00143cc9b28425cb66df6bc0 Mon Sep 17 00:00:00 2001 From: i544693 <369491420@qq.com> Date: Sun, 5 Jun 2022 17:06:18 +0800 Subject: [PATCH] feat: plugin hook. --- index.html | 19 +++++++++++++++-- src/core/init/lifecycle.js | 41 +++++++++++++++++++++++++++++-------- src/plugins/search/index.js | 4 ++-- 3 files changed, 52 insertions(+), 12 deletions(-) diff --git a/index.html b/index.html index 1e112b233..ccd266270 100644 --- a/index.html +++ b/index.html @@ -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') @@ -101,7 +101,14 @@ '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 + @@ -109,7 +116,15 @@ 'Powered by docsify\n\n' + 'Vercel' ); - }); + } + demo.enableDocsifyPluginHook=true + demo.pluginMeta = { + name:"koy" + } + hook.beforeEach( + demo + + ); }, ], }; diff --git a/src/core/init/lifecycle.js b/src/core/init/lifecycle.js index 94a3981fe..c9d1d217b 100644 --- a/src/core/init/lifecycle.js +++ b/src/core/init/lifecycle.js @@ -1,4 +1,4 @@ -import { noop } from '../util/core'; +import { isFn, noop } from '../util/core'; /** @typedef {import('../Docsify').Constructor} Constructor */ @@ -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); @@ -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); diff --git a/src/plugins/search/index.js b/src/plugins/search/index.js index c3bdab96e..c1b284492 100644 --- a/src/plugins/search/index.js +++ b/src/plugins/search/index.js @@ -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; @@ -43,4 +43,4 @@ const install = function (hook, vm) { }); }; -$docsify.plugins = [].concat(install, $docsify.plugins); +$docsify.plugins = [].concat(search1, $docsify.plugins);