Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
Mirrored from https://github.com/Camo67/composio.git
Synced Nov 21, 03:37 UTC
| 1 | import type { Folder, Item, Node, Root } from 'fumadocs-core/page-tree'; |
| 2 | import { |
| 3 | DOCS_PRODUCTS, |
| 4 | type DocsProduct, |
| 5 | type ProductSidebarItem, |
| 6 | } from './home-navigation'; |
| 7 | |
| 8 | function findPage(nodes: Node[], url: string): Item | null { |
| 9 | for (const node of nodes) { |
| 10 | if (node.type === 'page' && node.url === url) return node; |
| 11 | if (node.type === 'folder') { |
| 12 | if (node.index?.url === url) return node.index; |
| 13 | const nested = findPage(node.children, url); |
| 14 | if (nested) return nested; |
| 15 | } |
| 16 | } |
| 17 | return null; |
| 18 | } |
| 19 | |
| 20 | function findFolder(nodes: Node[], path: string): Folder | null { |
| 21 | for (const node of nodes) { |
| 22 | if (node.type !== 'folder') continue; |
| 23 | if (node.$ref?.folder === path) return node; |
| 24 | const nested = findFolder(node.children, path); |
| 25 | if (nested) return nested; |
| 26 | } |
| 27 | return null; |
| 28 | } |
| 29 | |
| 30 | function resolveSidebarItem(tree: Root, item: ProductSidebarItem): Node { |
| 31 | if (item.type === 'link') { |
| 32 | return { type: 'page', name: item.label, url: item.url, external: item.external }; |
| 33 | } |
| 34 | |
| 35 | if (item.type === 'group') { |
| 36 | if (item.links.length === 0) throw new Error(`Product sidebar group is empty: ${item.label}`); |
| 37 | const children = item.links.map(link => resolveSidebarItem(tree, { type: 'page', ...link })); |
| 38 | const first = children[0]; |
| 39 | if (first.type !== 'page') { |
| 40 | throw new Error(`Product sidebar group has no first page: ${item.label}`); |
| 41 | } |
| 42 | return { |
| 43 | type: 'folder', |
| 44 | name: item.label, |
| 45 | index: first.url.startsWith('/docs/') ? first : undefined, |
| 46 | children, |
| 47 | }; |
| 48 | } |
| 49 | |
| 50 | const sourceNode = |
| 51 | item.type === 'page' |
| 52 | ? findPage(tree.children, item.url) |
| 53 | : findFolder(tree.children, item.path); |
| 54 | |
| 55 | if (!sourceNode) { |
| 56 | const target = item.type === 'page' ? item.url : item.path; |
| 57 | throw new Error(`Product sidebar target does not exist: ${target}`); |
| 58 | } |
| 59 | |
| 60 | return item.label ? { ...sourceNode, name: item.label } : sourceNode; |
| 61 | } |
| 62 | |
| 63 | export function buildProductPageTree(tree: Root, product: DocsProduct): Root { |
| 64 | const children = DOCS_PRODUCTS[product].sidebar.flatMap(group => [ |
| 65 | { type: 'separator' as const, name: group.label }, |
| 66 | ...group.items.map(item => resolveSidebarItem(tree, item)), |
| 67 | ]); |
| 68 | |
| 69 | // Fumadocs memoizes trees by root ID, so each audience needs a distinct ID |
| 70 | // for client-side product switches to replace the sidebar immediately. |
| 71 | return { ...tree, $id: `docs-product:${product}`, children }; |
| 72 | } |
| 73 | |
| 74 | export function pageTreeUrls(tree: Root): string[] { |
| 75 | const urls: string[] = []; |
| 76 | |
| 77 | function visit(nodes: Node[]): void { |
| 78 | for (const node of nodes) { |
| 79 | if (node.type === 'page') { |
| 80 | urls.push(node.url); |
| 81 | } else if (node.type === 'folder') { |
| 82 | if ( |
| 83 | node.index && |
| 84 | !node.children.some(child => child.type === 'page' && child.url === node.index?.url) |
| 85 | ) { |
| 86 | urls.push(node.index.url); |
| 87 | } |
| 88 | visit(node.children); |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | visit(tree.children); |
| 94 | return urls; |
| 95 | } |