Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Update project docs sidebar config #193

Merged
merged 3 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 21 additions & 18 deletions app/components/DocsLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,6 @@ const useMenuConfig = ({
framework: string
repo: string
}) => {
const frameworkMenuItems =
config.frameworkMenus.find((d) => d.framework === framework)?.menuItems ??
[]

const localMenu: MenuItem = {
label: 'Menu',
children: [
Expand Down Expand Up @@ -81,19 +77,26 @@ const useMenuConfig = ({
return [
localMenu,
// Merge the two menus together based on their group labels
...config.menu.map((d) => {
const match = frameworkMenuItems.find((d2) => d2.label === d.label)
...config.sections.map((section) => {
const frameworkDocs = section.frameworks?.find(
(f) => f.label === framework
)
const frameworkItems = frameworkDocs?.children ?? []

const children = [
...section.children.map((d) => ({ ...d, badge: 'core' })),
...frameworkItems.map((d) => ({ ...d, badge: framework })),
]

if (children.length === 0) {
return undefined
}

return {
label: d.label,
children: [
...d.children.map((d) => ({ ...d, badge: 'core' })),
...(match?.children ?? []).map((d) => ({ ...d, badge: framework })),
],
label: section.label,
children,
}
}),
...frameworkMenuItems.filter(
(d) => !config.menu.find((dd) => dd.label === d.label)
),
].filter(Boolean)
}

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

const flatMenu = React.useMemo(
() => menuConfig.flatMap((d) => d.children),
() => menuConfig.flatMap((d) => d?.children),
[menuConfig]
)

Expand All @@ -223,7 +226,7 @@ export function DocsLayout({
''
)

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

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

return (
Expand Down
49 changes: 26 additions & 23 deletions app/utils/config.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import { z } from 'zod'
import { fetchRepoFile } from './documents.server'

export type FrameworkMenu = {
framework: string
menuItems: MenuItem[]
}

export type MenuItem = {
label: string | React.ReactNode
children: {
Expand All @@ -15,30 +10,38 @@ export type MenuItem = {
}[]
}

const menuItemSchema = z.object({
label: z.string(),
children: z.array(
z.object({
label: z.string(),
to: z.string(),
badge: z.string().optional(),
})
),
})

const frameworkMenuSchema = z.object({
framework: z.string(),
menuItems: z.array(menuItemSchema),
})

const configSchema = z.object({
docSearch: z.object({
appId: z.string(),
apiKey: z.string(),
indexName: z.string(),
}),
menu: z.array(menuItemSchema),
frameworkMenus: z.array(frameworkMenuSchema),
sections: z.array(
z.object({
label: z.string(),
children: z.array(
z.object({
label: z.string(),
to: z.string(),
badge: z.string().optional(),
})
),
frameworks: z
.array(
z.object({
label: z.string(),
children: z.array(
z.object({
label: z.string(),
to: z.string(),
badge: z.string().optional(),
})
),
})
)
.optional(),
})
),
users: z.array(z.string()).optional(),
})

Expand Down
35 changes: 11 additions & 24 deletions tanstack-docs-config.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"title": "TanStack Docs Config",
"description": "Config file for the documentation of a TanStack project.",
"type": "object",
"required": ["docSearch", "menu", "frameworkMenus"],
"required": ["docSearch", "sections"],
"additionalProperties": false,
"properties": {
"$schema": {
Expand All @@ -26,8 +26,8 @@
}
}
},
"menu": {
"description": "Doc pages that are NOT framework-specific.",
"sections": {
"description": "Section groups for the sidebar.",
"type": "array",
"items": {
"type": "object",
Expand All @@ -38,6 +38,7 @@
"type": "string"
},
"children": {
"description": "Framework-agnostic docs go here.",
"type": "array",
"items": {
"type": "object",
Expand All @@ -55,36 +56,20 @@
}
}
}
}
}
}
},
"frameworkMenus": {
"description": "Doc pages that are framework-specific.",
"type": "array",
"items": {
"type": "object",
"required": ["framework", "menuItems"],
"additionalProperties": false,
"properties": {
"framework": {
"type": "string"
},
"menuItems": {
"frameworks": {
"type": "array",
"items": {
"type": "object",
"required": ["label", "children"],
"additionalProperties": false,
"properties": {
"label": {
"type": "string"
},
"label": { "type": "string" },
"children": {
"description": "Framework-specific docs go here.",
"type": "array",
"items": {
"type": "object",
"required": ["label", "to"],
"additionalProperties": false,
"properties": {
"label": {
"type": "string"
Expand All @@ -98,7 +83,9 @@
}
}
}
}
},
"required": ["label", "children"],
"additionalProperties": false
}
}
}
Expand Down
Loading