UNPKG

svelte-markdown-pages

Version:

Build and render markdown-based content with distributed navigation for Svelte projects

119 lines (118 loc) 3.12 kB
// src/renderer/navigation.ts var NavigationTree = class { constructor(data) { this._flatItems = []; this._pathMap = /* @__PURE__ */ new Map(); this._items = data.items; this._buildIndexes(); } get items() { return this._items; } get flatItems() { return this._flatItems; } findItemByPath(path) { return this._pathMap.get(path); } findItemByName(name) { return this._findItemByNameRecursive(this._items, name); } getBreadcrumbs(path) { const item = this.findItemByPath(path); if (!item) { return []; } const breadcrumbs = []; let current = item; while (current) { breadcrumbs.unshift(current); current = current.parent; } return breadcrumbs; } getSiblings(path) { const item = this.findItemByPath(path); if (!item || !item.parent) { return this._items; } return item.parent.items || []; } getNextSibling(path) { const siblings = this.getSiblings(path); const currentIndex = siblings.findIndex((item) => item.path === path); if (currentIndex === -1 || currentIndex === siblings.length - 1) { return void 0; } return siblings[currentIndex + 1]; } getPreviousSibling(path) { const siblings = this.getSiblings(path); const currentIndex = siblings.findIndex((item) => item.path === path); if (currentIndex <= 0) { return void 0; } return siblings[currentIndex - 1]; } getChildren(path) { let item = this.findItemByName(path); if (!item) { item = this.findItemByPath(path); } if (!item && path.includes("/")) { const pathParts = path.split("/"); const sectionName = pathParts[pathParts.length - 1]; if (sectionName) { item = this.findItemByName(sectionName); } } return item?.items || []; } isExpanded(path) { const item = this.findItemByName(path) || this.findItemByPath(path); return item ? !item.collapsed : false; } toggleExpanded(path) { const item = this.findItemByName(path) || this.findItemByPath(path); if (item) { item.collapsed = !item.collapsed; } } _buildIndexes() { this._flatItems = []; this._pathMap.clear(); this._buildIndexesRecursive(this._items, void 0); } _buildIndexesRecursive(items, parent) { for (const item of items) { item.parent = parent || void 0; this._flatItems.push(item); if (item.path) { this._pathMap.set(item.path, item); } if (item.items) { this._buildIndexesRecursive(item.items, item); } } } _findItemByNameRecursive(items, name) { for (const item of items) { if (item.name === name) { return item; } if (item.items) { const found = this._findItemByNameRecursive(item.items, name); if (found) { return found; } } } return void 0; } }; function createNavigationTree(data) { return new NavigationTree(data); } export { NavigationTree, createNavigationTree }; //# sourceMappingURL=navigation.js.map //# sourceMappingURL=navigation.js.map