UNPKG

@frontender-magazine/builder

Version:
70 lines (66 loc) 1.75 kB
const deepmerge = require('deepmerge'); const pluginBase = require('../../libs/PluginBase'); /** * @typedef {object} PluginMeta * @property {string} name - plugin name * @property {string[]} dependency - array of plugins that we need to run first * @property {boolean} async - function return Promise? */ /** * @namespace * @typedef {object} Plugin * @property {PluginMeta} meta - plugins mata data * @property {function} before - plugin function */ module.exports = deepmerge(pluginBase, { meta: { name: 'restoreCodeSnippets', dependency: ['matchContainer', 'mercury', 'fetch'], }, /** * match mercury and fetch dom containers * @param {object} unmodified - current article sate * @return {object} - modified article state */ [['mutation:before']]: (unmodified) => { const { meta: { name, dependency, domain, }, dependencyCheck, domainCheck, } = module.exports; const { url, stack, } = unmodified; const modified = { dom: {}, stack: [], ...unmodified, }; const { dom: { mercury, matched, }, } = modified; try { if (!domainCheck(url, domain)) return unmodified; dependencyCheck(stack, dependency, name); modified.stack.push(name); if (!matched) return unmodified; const mercuryCodeBlocks = mercury.window.document.querySelectorAll('pre>code'); const originalCodeBlocks = matched.querySelectorAll('pre>code'); mercuryCodeBlocks.forEach((block, index) => { block.replaceWith(originalCodeBlocks[index]); }); return modified; } catch (error) { console.log(error); return unmodified; } }, });