Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

next
ts 85 lines 2.72 KB
Raw
1 /**
2 * Flattens a fumadocs page tree into a url -> sidebar-position lookup.
3 *
4 * Analytics needs to know which sidebar group a click came from, and the
5 * rendered sidebar cannot answer that: separators render as `<p>` and folders
6 * as `<button>`, both fumadocs-internal details that change on upgrade. The
7 * tree is the stable source, so the mapping is derived here at build time and
8 * the click handler only does an href lookup.
9 *
10 * Mirrors the traversal in `app/llms.txt/route.ts`.
11 */
12 import type { ReactNode } from 'react';
13 import type { Root, Node } from 'fumadocs-core/page-tree';
14
15 export interface SidebarNavEntry {
16 /** Enclosing separator heading, e.g. "Core concepts". */
17 group: string | null;
18 /** Enclosing folder title, e.g. "Authentication"; null at the top level. */
19 folder: string | null;
20 /** 1 = top-level row, 2 = folder child, 3 = nested. */
21 depth: number;
22 /**
23 * 1-based position among sibling rows, counting only this level: a folder
24 * occupies one row whether or not it is expanded, and its children get their
25 * own 1..n sequence. Counting through folders instead would report a
26 * collapsed folder's hidden children as rows the reader had to scroll past.
27 */
28 position: number;
29 }
30
31 export type SidebarNavIndex = Record<string, SidebarNavEntry>;
32
33 /** Sidebar badges wrap names in elements, so only plain names yield text. */
34 function nodeText(name: ReactNode): string | null {
35 if (typeof name === 'string') return name;
36 if (typeof name === 'number') return String(name);
37 return null;
38 }
39
40 /**
41 * `group` and `position` are per-level locals, so a separator nested inside a
42 * folder can only affect that folder's own children — it cannot reset the
43 * counter or the heading for the folder's siblings.
44 */
45 function walkNodes(
46 nodes: Node[],
47 folder: string | null,
48 depth: number,
49 parentGroup: string | null,
50 index: SidebarNavIndex
51 ): void {
52 let group = parentGroup;
53 let position = 0;
54
55 for (const node of nodes) {
56 if (node.type === 'separator') {
57 group = nodeText(node.name);
58 position = 0;
59 continue;
60 }
61
62 position += 1;
63
64 if (node.type === 'page') {
65 index[node.url] = { group, folder, depth, position };
66 continue;
67 }
68
69 const folderName = nodeText(node.name);
70
71 // A folder's index page is reached by clicking the folder's own row, so it
72 // keeps the folder's depth and position rather than its children's.
73 if (node.index) {
74 index[node.index.url] = { group, folder: folderName, depth, position };
75 }
76
77 walkNodes(node.children, folderName, depth + 1, group, index);
78 }
79 }
80
81 export function buildSidebarNavIndex(tree: Root): SidebarNavIndex {
82 const index: SidebarNavIndex = {};
83 walkNodes(tree.children, null, 1, null, index);
84 return index;
85 }