forked from CopilotKit/CopilotKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpatch-pagetree.ts
More file actions
127 lines (110 loc) · 4.32 KB
/
Copy pathpatch-pagetree.ts
File metadata and controls
127 lines (110 loc) · 4.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import { DocsLayoutProps } from "fumadocs-ui/layouts/docs";
import { INTEGRATION_METADATA } from "./integrations";
type Node = DocsLayoutProps["tree"]["children"][number];
/**
* Special mappings for folder names that don't exactly match integration labels.
* Key: folder name from meta.json title
* Value: integration ID
*/
const FOLDER_NAME_TO_INTEGRATION_ID: Record<string, string> = {
AutoGen2: "ag2",
autogen2: "ag2",
};
const FOLDER_TITLE_OVERRIDES: Record<string, string> = {
"/integrations/langgraph/generative-ui/a2ui": "Declerative Gen-UI (A2UI)",
};
/**
* Non-integration root folders that have index.mdx pages.
* Maps folder name (from meta.json title) to the index URL.
* Workaround for fumadocs v16 not resolving indexUrl for root folders.
*/
const ROOT_FOLDER_INDEX_URLS: Record<string, string> = {
Learn: "/learn",
};
/**
* Patches the pageTree to set missing indexUrl for integration folders.
* This fixes an issue where fumadocs v16 doesn't set indexUrl for root folders
* even when they have index.mdx files and "root": true in meta.json.
*
* The patch works by:
* 1. Finding folders without indexUrl
* 2. Matching them to integration metadata by name (with special case handling)
* 3. Setting the indexUrl from the integration's href
*/
export function patchPageTree(
pageTree: DocsLayoutProps["tree"],
): DocsLayoutProps["tree"] {
const patched = { ...pageTree };
function patchNode(node: Node): Node {
const patchedNode = { ...node } as any;
const folderIndexUrl =
typeof patchedNode.index?.url === "string" ? patchedNode.index.url : "";
const folderUrl =
typeof patchedNode.url === "string" ? patchedNode.url : "";
const folderLookupUrl = folderIndexUrl || folderUrl;
if (
patchedNode.type === "folder" &&
folderLookupUrl &&
FOLDER_TITLE_OVERRIDES[folderLookupUrl]
) {
patchedNode.name = FOLDER_TITLE_OVERRIDES[folderLookupUrl];
}
// If this is a folder without an indexUrl, try to set it
if (patchedNode.type === "folder" && !patchedNode.index?.url) {
let integrationId: string | undefined;
// First, check special mappings (e.g., "AutoGen2" -> "ag2")
const folderName =
typeof patchedNode.name === "string" ? patchedNode.name : undefined;
if (folderName) {
integrationId =
FOLDER_NAME_TO_INTEGRATION_ID[folderName] ||
FOLDER_NAME_TO_INTEGRATION_ID[folderName.toLowerCase()];
}
// If no special mapping, try to match by integration metadata (by folder name)
if (!integrationId && folderName) {
integrationId = Object.keys(INTEGRATION_METADATA).find((id) => {
const meta =
INTEGRATION_METADATA[id as keyof typeof INTEGRATION_METADATA];
const folderNameLower = folderName.toLowerCase();
const labelLower = meta.label.toLowerCase();
const idLower = id.toLowerCase();
return folderNameLower === labelLower || folderNameLower === idLower;
});
}
// Check non-integration root folders (e.g., Learn)
if (!integrationId && folderName && ROOT_FOLDER_INDEX_URLS[folderName]) {
patchedNode.index = {
url: ROOT_FOLDER_INDEX_URLS[folderName],
} as any;
} else if (integrationId) {
const meta =
INTEGRATION_METADATA[
integrationId as keyof typeof INTEGRATION_METADATA
];
// Type assertion needed because fumadocs expects a full Item type, but we only need to set the URL
patchedNode.index = { url: meta.href } as any;
} else {
// If no integration match, check if folder has exactly one child page
// In that case, set the indexUrl to that child's URL
const children = patchedNode.children || [];
const pageChildren = children.filter(
(child: any) => child.type === "page",
);
if (pageChildren.length === 1) {
const singlePage = pageChildren[0] as any;
const pageUrl = singlePage.url;
if (pageUrl) {
patchedNode.index = { url: pageUrl } as any;
}
}
}
}
// Recursively patch children
if (patchedNode.children) {
patchedNode.children = patchedNode.children.map(patchNode);
}
return patchedNode;
}
patched.children = patched.children.map(patchNode) as any;
return patched;
}