|
1 | 1 | import type { Sidebar as Sidebar_, SidebarItem } from "../types.ts"; |
2 | 2 |
|
3 | | -function generateCrumbs( |
| 3 | +function buildBreadcrumbPath( |
4 | 4 | url: string, |
5 | 5 | title: string, |
6 | 6 | items: SidebarItem[], |
7 | | - current: SidebarItem[] = [], |
8 | | -): SidebarItem[] { |
| 7 | + breadcrumbPath: SidebarItem[] = [], |
| 8 | +): SidebarItem[] | null { |
9 | 9 | for (const item of items) { |
10 | | - const foundTargetPage = item.href === url; |
11 | | - |
12 | | - if (foundTargetPage) { |
13 | | - current.push({ title: title }); |
14 | | - return current; |
| 10 | + // Check if this is the target page |
| 11 | + if (item.href === url) { |
| 12 | + return [...breadcrumbPath, { title }]; |
15 | 13 | } |
16 | 14 |
|
17 | | - if (item.items) { |
18 | | - const childItems: SidebarItem[] = []; |
19 | | - generateCrumbs(url, title, item.items, childItems); |
| 15 | + // If item has children, search recursively |
| 16 | + if (item.items && item.items.length > 0) { |
| 17 | + const childPath = buildBreadcrumbPath( |
| 18 | + url, |
| 19 | + title, |
| 20 | + item.items, |
| 21 | + [...breadcrumbPath, { title: item.title, href: item.href }], |
| 22 | + ); |
20 | 23 |
|
21 | | - if (childItems.length > 0) { |
22 | | - if (item.href) { |
23 | | - current.push({ title: item.title, href: item.href }); |
24 | | - } |
25 | | - current.push(...childItems); |
26 | | - return current; |
| 24 | + if (childPath) { |
| 25 | + return childPath; |
27 | 26 | } |
28 | 27 | } |
29 | 28 | } |
30 | 29 |
|
31 | | - return current; |
| 30 | + return null; |
32 | 31 | } |
33 | 32 |
|
34 | | -export default function (props: { |
| 33 | +export default function Breadcrumbs(props: { |
35 | 34 | title: string; |
36 | 35 | sidebar: Sidebar_; |
37 | 36 | url: string; |
38 | | - sectionTitle: string; |
39 | | - sectionHref: string; |
40 | | -}, helpers: Lume.Helpers) { |
41 | | - const crumbs: SidebarItem[] = []; |
| 37 | +}) { |
| 38 | + let breadcrumbs: SidebarItem[] = []; |
| 39 | + |
| 40 | + // Extract the top-level section from the URL (e.g., "runtime", "deploy") |
| 41 | + const topLevelSection = props.url.split("/").filter(Boolean)[0]; |
42 | 42 |
|
| 43 | + // Define section name mappings for better display |
| 44 | + const sectionNames: Record<string, string> = { |
| 45 | + "runtime": "Runtime", |
| 46 | + "services": "Services", |
| 47 | + "deploy": "Deploy", |
| 48 | + "subhosting": "Subhosting", |
| 49 | + "examples": "Examples", |
| 50 | + "lint": "Lint", |
| 51 | + "api": "API Reference", |
| 52 | + }; |
| 53 | + |
| 54 | + // Search through all sidebar sections to find the breadcrumb path |
43 | 55 | for (const section of props.sidebar) { |
| 56 | + // Check if the current URL is a top-level section |
44 | 57 | if (section.href === props.url) { |
45 | | - crumbs.push({ title: props.title }); |
| 58 | + // If this page IS the section, just show the page title |
| 59 | + breadcrumbs = [{ title: props.title }]; |
46 | 60 | break; |
47 | 61 | } |
48 | 62 |
|
49 | | - const rootItem = { title: section.title, href: section.href }; |
50 | | - const potentialCrumbs = generateCrumbs( |
51 | | - props.url, |
52 | | - props.title, |
53 | | - section?.items || [], |
54 | | - [rootItem], |
55 | | - ); |
| 63 | + // Search within section items if they exist |
| 64 | + if (section.items) { |
| 65 | + const foundPath = buildBreadcrumbPath( |
| 66 | + props.url, |
| 67 | + props.title, |
| 68 | + section.items, |
| 69 | + [], // Start with empty path, we'll add section as base |
| 70 | + ); |
56 | 71 |
|
57 | | - if (potentialCrumbs.length > 1) { |
58 | | - crumbs.push(...potentialCrumbs); |
59 | | - break; |
| 72 | + if (foundPath) { |
| 73 | + // Add the top-level section as the base breadcrumb |
| 74 | + const sectionDisplayName = sectionNames[topLevelSection] || |
| 75 | + (topLevelSection?.charAt(0).toUpperCase() + |
| 76 | + topLevelSection?.slice(1)) || |
| 77 | + "Docs"; |
| 78 | + |
| 79 | + breadcrumbs = [ |
| 80 | + { title: sectionDisplayName, href: `/${topLevelSection}/` }, |
| 81 | + ...foundPath, |
| 82 | + ]; |
| 83 | + break; |
| 84 | + } |
60 | 85 | } |
61 | 86 | } |
62 | 87 |
|
| 88 | + // If no breadcrumbs found, just show the current page title |
| 89 | + if (breadcrumbs.length === 0) { |
| 90 | + breadcrumbs = [{ title: props.title }]; |
| 91 | + } |
| 92 | + |
63 | 93 | const linkClasses = |
64 | | - `flex items-center pl-3 py-1.5 underline underline-offset-4 decoration-foreground-tertiary hover:text-foreground-secondary hover:underline-medium hover:bg-foreground-quaternary dark:hover:bg-background-secondary dark:hover:text-foreground-primary rounded transition duration-100 text-xs`; |
| 94 | + "flex items-center pl-3 py-1.5 underline underline-offset-4 decoration-foreground-tertiary hover:text-foreground-secondary hover:underline-medium hover:bg-foreground-quaternary dark:hover:bg-background-secondary dark:hover:text-foreground-primary rounded transition duration-100 text-xs"; |
65 | 95 |
|
66 | 96 | const chevronClasses = |
67 | | - `after:w-4 after:h-4 after:[background:url(./img/chevron.svg)_no-repeat_center] after:inline-block after:ml-2`; |
| 97 | + "after:w-4 after:h-4 after:[background:url(./img/chevron.svg)_no-repeat_center] after:inline-block after:ml-2"; |
68 | 98 |
|
69 | 99 | return ( |
70 | 100 | <nav> |
71 | 101 | <ul |
72 | | - class="flex flex- items-center text-center mt-2 -ml-3 text-foreground-secondary sm:mt-4" |
| 102 | + class="flex items-center text-center mt-2 -ml-3 text-foreground-secondary sm:mt-4" |
73 | 103 | itemscope |
74 | 104 | itemtype="https://schema.org/BreadcrumbList" |
75 | 105 | > |
76 | | - <li |
77 | | - itemprop="itemListElement" |
78 | | - itemscope |
79 | | - itemtype="https://schema.org/ListItem" |
80 | | - > |
81 | | - <a |
82 | | - class={`${linkClasses} ${chevronClasses}`} |
83 | | - itemprop="item" |
84 | | - href={props.sectionHref} |
| 106 | + {breadcrumbs.map((crumb, i) => ( |
| 107 | + <li |
| 108 | + key={i} |
| 109 | + itemprop="itemListElement" |
| 110 | + itemscope |
| 111 | + itemtype="https://schema.org/ListItem" |
85 | 112 | > |
86 | | - <span |
87 | | - itemprop="name" |
88 | | - dangerouslySetInnerHTML={{ __html: props.sectionTitle }} |
89 | | - /> |
90 | | - </a> |
91 | | - <meta itemprop="position" content="1" /> |
92 | | - </li> |
93 | | - {crumbs.map((crumb, i) => ( |
94 | | - <> |
95 | | - <li |
96 | | - itemprop="itemListElement" |
97 | | - itemscope |
98 | | - itemtype="https://schema.org/ListItem" |
99 | | - > |
100 | | - {crumb.href |
101 | | - ? ( |
102 | | - <a |
103 | | - href={crumb.href} |
104 | | - itemprop="item" |
105 | | - class={`${linkClasses} ${chevronClasses}`} |
106 | | - > |
107 | | - <span itemprop="name">{crumb.title}</span> |
108 | | - </a> |
109 | | - ) |
110 | | - : ( |
111 | | - <span |
112 | | - itemprop="name" |
113 | | - class={`flex items-center pl-2 py-1.5 text-xs ${ |
114 | | - i < crumbs.length - 1 ? chevronClasses : "" |
115 | | - }`} |
116 | | - dangerouslySetInnerHTML={{ |
117 | | - __html: helpers.md(crumb.title, true), |
118 | | - }} |
119 | | - /> |
120 | | - )} |
121 | | - <meta itemprop="position" content={String(i + 2)} /> |
122 | | - </li> |
123 | | - </> |
| 113 | + {crumb.href |
| 114 | + ? ( |
| 115 | + <a |
| 116 | + href={crumb.href} |
| 117 | + itemprop="item" |
| 118 | + class={`${linkClasses} ${ |
| 119 | + i < breadcrumbs.length - 1 ? chevronClasses : "" |
| 120 | + }`} |
| 121 | + > |
| 122 | + <span itemprop="name"> |
| 123 | + {typeof crumb.title === "string" |
| 124 | + ? crumb.title |
| 125 | + : crumb.title} |
| 126 | + </span> |
| 127 | + </a> |
| 128 | + ) |
| 129 | + : ( |
| 130 | + <span |
| 131 | + itemprop="name" |
| 132 | + class={`flex items-center pl-2 py-1.5 text-xs ${ |
| 133 | + i < breadcrumbs.length - 1 ? chevronClasses : "" |
| 134 | + }`} |
| 135 | + > |
| 136 | + {typeof crumb.title === "string" ? crumb.title : crumb.title} |
| 137 | + </span> |
| 138 | + )} |
| 139 | + <meta itemprop="position" content={String(i + 1)} /> |
| 140 | + </li> |
124 | 141 | ))} |
125 | 142 | </ul> |
126 | 143 | </nav> |
|
0 commit comments