UNPKG

@docuify/engine

Version:

A flexible, pluggable engine for building and transforming documentation content from source files.

67 lines (65 loc) 2.18 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // lib/utils/walk_tree_with_plugins.ts var walk_tree_with_plugins_exports = {}; __export(walk_tree_with_plugins_exports, { walkTreeWithPlugins: () => walkTreeWithPlugins }); module.exports = __toCommonJS(walk_tree_with_plugins_exports); async function walkTreeWithPlugins(root, plugins) { const state = {}; let currentRoot = root; for (const plugin of plugins) { if (plugin.applyBefore) { const result = await plugin.applyBefore(currentRoot, state); if (result) currentRoot = result; } } async function walk(node, ancestors = [], index) { const context = { parent: ancestors[ancestors.length - 1], ancestors, index, visit: (child, ctx) => walk(child, ctx.ancestors, ctx.index), state }; for (const plugin of plugins) { if (plugin.onVisit) { await plugin.onVisit(node, context); } } if (node.children) { for (let i = 0; i < node.children.length; i++) { await walk(node.children[i], [...ancestors, node], i); } } } await walk(currentRoot); for (const plugin of plugins) { if (plugin.applyAfter) { const result = await plugin.applyAfter(currentRoot, state); if (result) currentRoot = result; } } return currentRoot; } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { walkTreeWithPlugins });