-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild-breadcrumbs.ts
More file actions
41 lines (35 loc) · 1.04 KB
/
build-breadcrumbs.ts
File metadata and controls
41 lines (35 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import type { Page } from "content-collections"
import type { SidebarSection } from "~/utils/create-sidebar-tree"
import { buildDocPathFromSlug } from "~/utils/path-builders"
export const buildBreadcrumbs = (
items: SidebarSection[],
pathname: string,
documentationPages: Pick<Page, "title" | "slug">[] = []
) => {
// for standalone pages: /:filename
for (const page of documentationPages) {
const docPath = buildDocPathFromSlug(page.slug)
if (docPath === pathname) {
return [page.title]
}
}
// for sectioned pages: /:section/:subsection?/:filename
let trail: string[] = []
const walk = (section: SidebarSection, acc: string[]): boolean => {
for (const doc of section.documentationPages) {
const docPath = buildDocPathFromSlug(doc.slug)
if (docPath === pathname) {
trail = [...acc, section.title, doc.title]
return true
}
}
for (const sub of section.subsections) {
if (walk(sub, [...acc, section.title])) return true
}
return false
}
for (const root of items) {
if (walk(root, [])) break
}
return trail
}