Skip to content

Commit 8d2fee4

Browse files
authored
1 parent 0b1cd06 commit 8d2fee4

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

src/components/Breadcrumbs.tsx

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import clsx from 'clsx';
55
import {ChevronRightIcon} from '@heroicons/react/20/solid';
66
import {useEffect, useState} from 'react';
77
import {usePathname} from 'next/navigation';
8+
import {navigation} from '@/data/navigation';
9+
import {allPosts} from 'contentlayer/generated';
810

911
export function Breadcrumbs({path}: {path: string[]}) {
1012
let pathname = usePathname();
@@ -23,6 +25,36 @@ export function Breadcrumbs({path}: {path: string[]}) {
2325
return version ? `/v/${version}${path}` : path;
2426
};
2527

28+
const getTitleForSlug = (currentPath: string[]): string => {
29+
const href = `/${currentPath.join('/')}`;
30+
31+
for (const navItem of navigation) {
32+
for (const topic of navItem.topics) {
33+
if (topic.href === href) return topic.title;
34+
35+
if (topic.sections) {
36+
for (const section of topic.sections) {
37+
if (section.href === href) return section.title;
38+
39+
if (section.subsections) {
40+
for (const subsection of section.subsections) {
41+
if (subsection.href === href) return subsection.title;
42+
}
43+
}
44+
}
45+
}
46+
}
47+
}
48+
49+
const pagePath = currentPath.join('/');
50+
const post = allPosts.find(post => post._raw.flattenedPath === pagePath);
51+
if (post && post.headings && post.headings.length > 0) {
52+
return post.headings[0].title;
53+
}
54+
55+
throw new Error(`No title found in navigation.ts or page headings for path: ${href}`);
56+
};
57+
2658
// Handle versions
2759
useEffect(() => {
2860
// Extract the version name from the URL path, if any
@@ -67,7 +99,7 @@ export function Breadcrumbs({path}: {path: string[]}) {
6799
: 'text-gray-500 hover:text-link-light dark:hover:text-link'
68100
)}
69101
>
70-
{slug}
102+
{getTitleForSlug(path.slice(0, index + 1))}
71103
</Link>
72104
</div>
73105
</li>

0 commit comments

Comments
 (0)