Skip to content

Commit 7ab1885

Browse files
rsbhclaude
andauthored
feat: enable breadcrumbs and prev/next navigation on API pages (#58)
- Show breadcrumbs on API routes using the API page tree - Compute prev/next from flattened API tree for arrow navigation - Prev/next buttons navigate between API endpoints within the spec Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent a3c840e commit 7ab1885

1 file changed

Lines changed: 26 additions & 3 deletions

File tree

packages/chronicle/src/themes/default/Layout.tsx

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import { Breadcrumbs } from '@/components/ui/breadcrumbs';
2121
import { getLandingEntries } from '@/lib/config';
2222
import { getActiveContentDir } from '@/lib/navigation';
2323
import { usePageContext } from '@/lib/page-context';
24-
import type { Node } from 'fumadocs-core/page-tree';
24+
import type { Node, Root } from 'fumadocs-core/page-tree';
2525
import type { ThemeLayoutProps } from '@/types';
2626
import styles from './Layout.module.css';
2727
import { OpenInAI } from './OpenInAI';
@@ -71,7 +71,12 @@ export function Layout({
7171
const isApiRoute = pathname === '/apis' || pathname.startsWith('/apis/');
7272
const isApiBase = (basePath: string) =>
7373
pathname === basePath || pathname.startsWith(`${basePath}/`);
74-
const { prev, next } = page ?? { prev: null, next: null };
74+
const docNav = page ?? { prev: null, next: null };
75+
const apiNav = useMemo(() => {
76+
if (!isApiRoute) return { prev: null, next: null };
77+
return getApiPrevNext(pathname, tree);
78+
}, [isApiRoute, pathname, tree]);
79+
const { prev, next } = isApiRoute ? apiNav : docNav;
7580

7681
const contentEntries = getLandingEntries(config, version.dir);
7782
const activeContentDir = getActiveContentDir(pathname, config);
@@ -200,7 +205,7 @@ export function Layout({
200205
<ArrowRightIcon width={14} height={14} />
201206
</IconButton>
202207
</Flex>
203-
{!isApiRoute && <Breadcrumbs slug={slug} tree={tree} />}
208+
<Breadcrumbs slug={slug} tree={tree} />
204209
</Flex>
205210
<Flex align='center' gap='small'>
206211
{isApiRoute && <TestRequestButton />}
@@ -390,3 +395,21 @@ function ViewDocsButton() {
390395
</Button>
391396
);
392397
}
398+
399+
function getApiPrevNext(pathname: string, tree: Root): { prev: { url: string; title: string } | null; next: { url: string; title: string } | null } {
400+
const pages: { url: string; title: string }[] = [];
401+
function collect(node: Node) {
402+
if (node.type === 'page') {
403+
pages.push({ url: node.url, title: node.name?.toString() ?? '' });
404+
} else if (node.type === 'folder') {
405+
for (const child of node.children) collect(child);
406+
}
407+
}
408+
for (const child of tree.children) collect(child);
409+
410+
const idx = pages.findIndex(p => p.url === pathname);
411+
return {
412+
prev: idx > 0 ? pages[idx - 1] : null,
413+
next: idx >= 0 && idx < pages.length - 1 ? pages[idx + 1] : null,
414+
};
415+
}

0 commit comments

Comments
 (0)