Skip to content
Open
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
8 changes: 7 additions & 1 deletion apps/www/app/(docs)/docs/[[...slug]]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { getBreadcrumbItems } from "fumadocs-core/breadcrumb"
import { findNeighbour } from "fumadocs-core/server"
import type { BreadcrumbList, TechArticle, WithContext } from "schema-dts"

import { getNeighboursFromConfig } from "@/config/docs"
import { siteConfig } from "@/config/site"
import { replaceComponentSource } from "@/lib/docs"
import { source } from "@/lib/source"
Expand Down Expand Up @@ -89,7 +90,12 @@ export default async function DocPage({ params }: DocPageProps) {
const { doc, page } = await getDocFromParams({ params })
const MDX = doc.body
const content = await doc.getText("raw")
const neighbours = findNeighbour(source.pageTree, page.url)
const configNeighbours = getNeighboursFromConfig(page.url)
const treeNeighbours = findNeighbour(source.pageTree, page.url)
const neighbours = {
previous: configNeighbours.previous ?? treeNeighbours.previous,
next: configNeighbours.next ?? treeNeighbours.next,
}
const breadcrumbs = getBreadcrumbItems(page.url, source.pageTree, {
includeRoot: { url: "/docs" },
includePage: true,
Expand Down
47 changes: 47 additions & 0 deletions apps/www/config/docs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,53 @@ interface DocsConfig {
sidebarNav: NavItemWithChildren[]
}

export interface DocNavLink {
url: string
name: string
}

function flattenSidebarItems(
items: NavItemWithChildren[],
acc: DocNavLink[] = []
): DocNavLink[] {
for (const item of items) {
if (item.href) {
acc.push({ url: item.href, name: item.title })
}
if (item.items?.length) {
flattenSidebarItems(item.items, acc)
}
}
return acc
}

function getFlattenedDocsNav(): DocNavLink[] {
const result: DocNavLink[] = []
for (const section of docsConfig.sidebarNav) {
if (section.items?.length) {
flattenSidebarItems(section.items, result)
}
}
return result
}

export function getNeighboursFromConfig(currentUrl: string): {
previous?: DocNavLink
next?: DocNavLink
} {
const nav = getFlattenedDocsNav()
const normalized = currentUrl.replace(/\/$/, "") || "/"
const index = nav.findIndex((item) => {
const itemNorm = item.url.replace(/\/$/, "") || "/"
return itemNorm === normalized
})
if (index < 0) return {}
return {
previous: index > 0 ? nav.at(index - 1) : undefined,
next: index < nav.length - 1 ? nav.at(index + 1) : undefined,
}
}

export const docsConfig: DocsConfig = {
mainNav: [
{
Expand Down
Loading