Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: plugin hook. #1815

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);