Skip to content

UI Library for Design Engineers. Animated components and effects you can copy and paste into your apps. Free. Open Source.

fix(docs): align prev/next navigation with sidebar order (#887)

Yeom-JinHo39671fbf2888
diff --git a/apps/www/app/(docs)/docs/[[...slug]]/page.tsx b/apps/www/app/(docs)/docs/[[...slug]]/page.tsx
index 165c809d..73f451e6 100644
--- a/apps/www/app/(docs)/docs/[[...slug]]/page.tsx
+++ b/apps/www/app/(docs)/docs/[[...slug]]/page.tsx
@@ -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"
@@ -87,7 +88,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,
diff --git a/apps/www/config/docs.ts b/apps/www/config/docs.ts
index d152085a..f568ca9c 100644
--- a/apps/www/config/docs.ts
+++ b/apps/www/config/docs.ts
@@ -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: [
{