Skip to content

Commit cd34f06

Browse files
feat: Update project docs sidebar config (#193)
* chore: Update project docs config * Simplify some changes * Fix empty section group
1 parent aaa0e27 commit cd34f06

File tree

3 files changed

+58
-65
lines changed

3 files changed

+58
-65
lines changed

app/components/DocsLayout.tsx

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,6 @@ const useMenuConfig = ({
4848
framework: string
4949
repo: string
5050
}) => {
51-
const frameworkMenuItems =
52-
config.frameworkMenus.find((d) => d.framework === framework)?.menuItems ??
53-
[]
54-
5551
const localMenu: MenuItem = {
5652
label: 'Menu',
5753
children: [
@@ -81,19 +77,26 @@ const useMenuConfig = ({
8177
return [
8278
localMenu,
8379
// Merge the two menus together based on their group labels
84-
...config.menu.map((d) => {
85-
const match = frameworkMenuItems.find((d2) => d2.label === d.label)
80+
...config.sections.map((section) => {
81+
const frameworkDocs = section.frameworks?.find(
82+
(f) => f.label === framework
83+
)
84+
const frameworkItems = frameworkDocs?.children ?? []
85+
86+
const children = [
87+
...section.children.map((d) => ({ ...d, badge: 'core' })),
88+
...frameworkItems.map((d) => ({ ...d, badge: framework })),
89+
]
90+
91+
if (children.length === 0) {
92+
return undefined
93+
}
94+
8695
return {
87-
label: d.label,
88-
children: [
89-
...d.children.map((d) => ({ ...d, badge: 'core' })),
90-
...(match?.children ?? []).map((d) => ({ ...d, badge: framework })),
91-
],
96+
label: section.label,
97+
children,
9298
}
9399
}),
94-
...frameworkMenuItems.filter(
95-
(d) => !config.menu.find((dd) => dd.label === d.label)
96-
),
97100
].filter(Boolean)
98101
}
99102

@@ -212,7 +215,7 @@ export function DocsLayout({
212215
const detailsRef = React.useRef<HTMLElement>(null!)
213216

214217
const flatMenu = React.useMemo(
215-
() => menuConfig.flatMap((d) => d.children),
218+
() => menuConfig.flatMap((d) => d?.children),
216219
[menuConfig]
217220
)
218221

@@ -223,7 +226,7 @@ export function DocsLayout({
223226
''
224227
)
225228

226-
const index = flatMenu.findIndex((d) => d.to === relativePathname)
229+
const index = flatMenu.findIndex((d) => d?.to === relativePathname)
227230
const prevItem = flatMenu[index - 1]
228231
const nextItem = flatMenu[index + 1]
229232

@@ -232,10 +235,10 @@ export function DocsLayout({
232235
const menuItems = menuConfig.map((group, i) => {
233236
return (
234237
<div key={i}>
235-
<div className="text-[.9em] uppercase font-black">{group.label}</div>
238+
<div className="text-[.9em] uppercase font-black">{group?.label}</div>
236239
<div className="h-2" />
237240
<div className="ml-2 space-y-px text-[.9em]">
238-
{group.children?.map((child, i) => {
241+
{group?.children?.map((child, i) => {
239242
const linkClasses = `flex items-center justify-between group px-2 py-1 rounded-lg hover:bg-gray-500 hover:bg-opacity-10`
240243

241244
return (

app/utils/config.ts

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
import { z } from 'zod'
22
import { fetchRepoFile } from './documents.server'
33

4-
export type FrameworkMenu = {
5-
framework: string
6-
menuItems: MenuItem[]
7-
}
8-
94
export type MenuItem = {
105
label: string | React.ReactNode
116
children: {
@@ -15,30 +10,38 @@ export type MenuItem = {
1510
}[]
1611
}
1712

18-
const menuItemSchema = z.object({
19-
label: z.string(),
20-
children: z.array(
21-
z.object({
22-
label: z.string(),
23-
to: z.string(),
24-
badge: z.string().optional(),
25-
})
26-
),
27-
})
28-
29-
const frameworkMenuSchema = z.object({
30-
framework: z.string(),
31-
menuItems: z.array(menuItemSchema),
32-
})
33-
3413
const configSchema = z.object({
3514
docSearch: z.object({
3615
appId: z.string(),
3716
apiKey: z.string(),
3817
indexName: z.string(),
3918
}),
40-
menu: z.array(menuItemSchema),
41-
frameworkMenus: z.array(frameworkMenuSchema),
19+
sections: z.array(
20+
z.object({
21+
label: z.string(),
22+
children: z.array(
23+
z.object({
24+
label: z.string(),
25+
to: z.string(),
26+
badge: z.string().optional(),
27+
})
28+
),
29+
frameworks: z
30+
.array(
31+
z.object({
32+
label: z.string(),
33+
children: z.array(
34+
z.object({
35+
label: z.string(),
36+
to: z.string(),
37+
badge: z.string().optional(),
38+
})
39+
),
40+
})
41+
)
42+
.optional(),
43+
})
44+
),
4245
users: z.array(z.string()).optional(),
4346
})
4447

tanstack-docs-config.schema.json

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"title": "TanStack Docs Config",
55
"description": "Config file for the documentation of a TanStack project.",
66
"type": "object",
7-
"required": ["docSearch", "menu", "frameworkMenus"],
7+
"required": ["docSearch", "sections"],
88
"additionalProperties": false,
99
"properties": {
1010
"$schema": {
@@ -26,8 +26,8 @@
2626
}
2727
}
2828
},
29-
"menu": {
30-
"description": "Doc pages that are NOT framework-specific.",
29+
"sections": {
30+
"description": "Section groups for the sidebar.",
3131
"type": "array",
3232
"items": {
3333
"type": "object",
@@ -38,6 +38,7 @@
3838
"type": "string"
3939
},
4040
"children": {
41+
"description": "Framework-agnostic docs go here.",
4142
"type": "array",
4243
"items": {
4344
"type": "object",
@@ -55,36 +56,20 @@
5556
}
5657
}
5758
}
58-
}
59-
}
60-
}
61-
},
62-
"frameworkMenus": {
63-
"description": "Doc pages that are framework-specific.",
64-
"type": "array",
65-
"items": {
66-
"type": "object",
67-
"required": ["framework", "menuItems"],
68-
"additionalProperties": false,
69-
"properties": {
70-
"framework": {
71-
"type": "string"
7259
},
73-
"menuItems": {
60+
"frameworks": {
7461
"type": "array",
7562
"items": {
7663
"type": "object",
77-
"required": ["label", "children"],
78-
"additionalProperties": false,
7964
"properties": {
80-
"label": {
81-
"type": "string"
82-
},
65+
"label": { "type": "string" },
8366
"children": {
67+
"description": "Framework-specific docs go here.",
8468
"type": "array",
8569
"items": {
8670
"type": "object",
8771
"required": ["label", "to"],
72+
"additionalProperties": false,
8873
"properties": {
8974
"label": {
9075
"type": "string"
@@ -98,7 +83,9 @@
9883
}
9984
}
10085
}
101-
}
86+
},
87+
"required": ["label", "children"],
88+
"additionalProperties": false
10289
}
10390
}
10491
}

0 commit comments

Comments
 (0)