UNPKG

@docuify/engine

Version:

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

112 lines (110 loc) 3.36 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/build_tree.ts var build_tree_exports = {}; __export(build_tree_exports, { buildTree: () => buildTree }); module.exports = __toCommonJS(build_tree_exports); var idCounter = 0; function generateId() { return `node-${idCounter++}`; } function createFolderNode(name, fullPath, parentId) { return { id: generateId(), name, fullPath, type: "folder", parentId, children: [] }; } function createFileNode(file, name, fullPath, parentId) { const nodeId = generateId(); const transformQueue = []; const getRawContent = file.loadContent; async function transformContent(rawContent) { let result = rawContent; for (const transformFunction of transformQueue) { result = await transformFunction(result); } return result; } async function loadContent() { if (!getRawContent) { throw new Error(`No content getter for ${file.path}`); } const raw = await getRawContent(); return transformContent(raw); } return { id: nodeId, name, fullPath, type: "file", parentId, extension: file.extension, metadata: file.metadata ? { ...file.metadata } : void 0, // For inspection or debugging purposes — it's like peeking under the hood. _contentTransformQueue: transformQueue, // All the file's special powers are grouped here. Transform, load, mutate! actions: { useTransform: (fn) => { transformQueue.push(fn); }, transformContent, loadContent } }; } function buildTree(sourceFiles) { const root = { id: generateId(), name: "root", // Root node — the top of the tree. No parents. Just responsibilities. fullPath: ".", type: "folder", children: [] }; for (const file of sourceFiles) { const parts = file.path.split("/"); let current = root; for (let i = 0; i < parts.length; i++) { const part = parts[i]; const isLast = i === parts.length - 1; if (!current.children) { current.children = []; } let found = current.children.find((child) => child.name === part); if (!found) { const fullPath = parts.slice(0, i + 1).join("/"); const newNode = isLast && file.type === "file" ? createFileNode(file, part, fullPath, current.id) : createFolderNode(part, fullPath, current.id); current.children.push(newNode); found = newNode; } current = found; } } return root; } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { buildTree });