rename some internal variables to better indicate what is happening

This commit is contained in:
Joe Pea 2021-09-26 15:51:55 -07:00
parent e94b629c14
commit 62d111fc66
3 changed files with 23 additions and 17 deletions

View File

@ -133,7 +133,7 @@ async function main() {
plugins: [
DocsifyCarbon.create('CEBI6KQE', 'docsifyjsorg'),
function (hook, vm) {
hook.beforeEach(function (html) {
hook.beforeEach(function (markdown) {
if (/githubusercontent\\.com/.test(vm.route.file)) {
url = vm.route.file
.replace('raw.githubusercontent.com', 'github.com')
@ -150,7 +150,7 @@ async function main() {
var editHtml = '[:memo: Edit Document](' + url + ')\\n';
return (
editHtml +
html +
markdown +
'\\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>'
);

View File

@ -23,5 +23,5 @@ export function initMixin(proto) {
}
function initPlugin(vm) {
[].concat(vm.config.plugins).forEach(fn => isFn(fn) && fn(vm._lifecycle, vm));
[].concat(vm.config.plugins).forEach(fn => isFn(fn) && fn(vm._hooksApi, vm));
}

View File

@ -11,36 +11,42 @@ export function initLifecycle(vm) {
];
vm._hooks = {};
vm._lifecycle = {};
vm._hooksApi = {};
hooks.forEach(hook => {
const arr = (vm._hooks[hook] = []);
vm._lifecycle[hook] = fn => arr.push(fn);
vm._hooksApi[hook] = fn => arr.push(fn);
});
}
export function callHook(vm, hookName, data, next = noop) {
export function callHook(vm, hookName, markdownOrHtml, next = noop) {
const queue = vm._hooks[hookName];
const step = function (index) {
const runNextHook = function(index) {
const hookFn = queue[index];
if (index >= queue.length) {
next(data);
next(markdownOrHtml);
} else if (typeof hookFn === 'function') {
if (hookFn.length === 2) {
hookFn(data, result => {
data = result;
step(index + 1);
});
const doneCallback = result => {
markdownOrHtml = result;
runNextHook(index + 1);
};
hookFn(markdownOrHtml, doneCallback);
} else {
const result = hookFn(data);
data = result === undefined ? data : result;
step(index + 1);
const result = hookFn(markdownOrHtml);
markdownOrHtml = result === undefined ? markdownOrHtml : result;
runNextHook(index + 1);
}
} else {
step(index + 1);
console.warn(
'Skipping hook that was not a function (all plugin hooks should be functions). The hook was: ',
hookFn
);
runNextHook(index + 1);
}
};
step(0);
runNextHook(0);
}