mirror of
https://github.com/hanzo-docs/docs.git
synced 2026-10-26 10:55:57 +00:00
Stop adding root twice to Breadcrumb (#3101)
* Stop adding root twice to Breadcrumbs * Use `item.index?.url` by default in Breadcrumb * Update tests to test includeRoot * Format and a changeset * Add test for includeRoot object form with custom URL Made-with: Cursor
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
---
|
||||
"fumadocs-core": patch
|
||||
---
|
||||
|
||||
Include root items only once in Breadcrumb.
|
||||
|
||||
Previously, when `includeRoot` was set to `true`, the root item was added twice to breadcrumbs.
|
||||
|
||||
@@ -73,8 +73,14 @@ export function getBreadcrumbItemsFromPath(
|
||||
});
|
||||
break;
|
||||
case 'folder':
|
||||
if (item.root && !includeRoot) {
|
||||
if (item.root) {
|
||||
items = [];
|
||||
if (includeRoot) {
|
||||
items.push({
|
||||
name: tree.name,
|
||||
url: typeof includeRoot === 'object' ? includeRoot.url : item.index?.url,
|
||||
});
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -95,13 +101,6 @@ export function getBreadcrumbItemsFromPath(
|
||||
}
|
||||
}
|
||||
|
||||
if (includeRoot) {
|
||||
items.unshift({
|
||||
name: tree.name,
|
||||
url: typeof includeRoot === 'object' ? includeRoot.url : undefined,
|
||||
});
|
||||
}
|
||||
|
||||
return items;
|
||||
}
|
||||
|
||||
|
||||
@@ -96,6 +96,63 @@ test('Breadcrumbs', () => {
|
||||
]);
|
||||
|
||||
expect(getBreadcrumbItems('/invalid', tree)).toMatchInlineSnapshot(`[]`);
|
||||
|
||||
const treeWithRoot: Root = {
|
||||
name: 'Docs',
|
||||
children: [
|
||||
{
|
||||
type: 'folder',
|
||||
name: 'Docs',
|
||||
root: true,
|
||||
index: { type: 'page', name: 'Introduction', url: '/docs' },
|
||||
children: [
|
||||
{
|
||||
type: 'folder',
|
||||
name: 'Guides',
|
||||
index: { type: 'page', name: 'Guides', url: '/docs/guides' },
|
||||
children: [
|
||||
{
|
||||
type: 'page',
|
||||
name: 'Getting Started',
|
||||
url: '/docs/guides/getting-started',
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
};
|
||||
expect(
|
||||
getBreadcrumbItems('/docs/guides/getting-started', treeWithRoot, {
|
||||
includeRoot: true,
|
||||
includePage: true,
|
||||
}),
|
||||
).toStrictEqual([
|
||||
{ name: 'Docs', url: '/docs' },
|
||||
{ name: 'Guides', url: '/docs/guides' },
|
||||
{ name: 'Getting Started', url: '/docs/guides/getting-started' },
|
||||
]);
|
||||
|
||||
expect(
|
||||
getBreadcrumbItems('/docs/guides/getting-started', treeWithRoot, {
|
||||
includeRoot: false,
|
||||
includePage: true,
|
||||
}),
|
||||
).toStrictEqual([
|
||||
{ name: 'Guides', url: '/docs/guides' },
|
||||
{ name: 'Getting Started', url: '/docs/guides/getting-started' },
|
||||
]);
|
||||
|
||||
expect(
|
||||
getBreadcrumbItems('/docs/guides/getting-started', treeWithRoot, {
|
||||
includeRoot: { url: '/custom-root' },
|
||||
includePage: true,
|
||||
}),
|
||||
).toStrictEqual([
|
||||
{ name: 'Docs', url: '/custom-root' },
|
||||
{ name: 'Guides', url: '/docs/guides' },
|
||||
{ name: 'Getting Started', url: '/docs/guides/getting-started' },
|
||||
]);
|
||||
});
|
||||
|
||||
test('I18n: Format URL', () => {
|
||||
|
||||
Reference in New Issue
Block a user