UNPKG

@podlite/schema

Version:

AST tools for Podlite markup language

71 lines 2.48 kB
import { mkRootBlock, toAny, toAnyRules, toHtml, toTree } from '.'; import idMiddleware from './helpers/ids'; import core from './helpers/corePlugins'; export { cleanIds, frozenIds } from './helpers/ids'; export const podlitePluggable = ({ plugins = {} } = {}) => { let instance = function () { }; let _plugins = []; instance.use = (onj) => { for (const plugin of Object.entries(onj)) { const [key, val] = plugin; if (typeof val === 'function') { _plugins.push({ [key]: { toAst: val } }); } else { _plugins.push({ [key]: val }); } } return instance; }; instance.parse = (text, opt = { skipChain: 0, podMode: 1 }) => { const rawTree = toTree().use(idMiddleware).parse(text, opt); const root = mkRootBlock({ margin: '' }, rawTree); return root; }; instance.toAst = ast => { return instance.toAstResult(ast).interator; }; instance.toAstResult = ast => { // get plugins for Ast const toAstPlugins = toAnyRules('toAst', instance.getPlugins()); const result = toAny() .use({ '*': () => (node, ctx, interator) => { if ('content' in node) { node.content = interator(node.content, ctx); } return node; }, }) .use(toAstPlugins) .run(ast); // add second pass const toAstAfterPlugins = toAnyRules('toAstAfter', instance.getPlugins()); if (Object.keys(toAstAfterPlugins).length) { const resultAfter = toAny() .use({ '*': () => (node, ctx, interator) => { if ('content' in node) { node.content = interator(node.content, ctx); } return node; }, }) .use(toAstAfterPlugins) .run(ast); return resultAfter; } return result; }; instance.toHtml = ast => { const toHtmlPlugins = toAnyRules('toHtml', instance.getPlugins()); return toHtml({}).use(toHtmlPlugins).run(ast, null); }; instance.getPlugins = () => _plugins; // init core plugins instance.use(core); // init external plugins instance.use(plugins); return instance; }; //# sourceMappingURL=pluggableParser.js.map