Breadcrumbs: Only dedupe the lowest child node of sectionNav (#78279)

only dedupe sectionNav leaf node
This commit is contained in:
Ashley Harrison 2023-11-22 15:36:49 +00:00 committed by GitHub
parent 4290ed3d86
commit b5d1c8874b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 6 additions and 31 deletions

View File

@ -141,28 +141,5 @@ describe('breadcrumb utils', () => {
{ text: 'My page', href: '/my-page' }, { text: 'My page', href: '/my-page' },
]); ]);
}); });
it('does not ignore duplicates with different text', () => {
const pageNav: NavModelItem = {
text: 'My page',
url: '/my-page',
parentItem: {
text: 'Another section',
// same url as section nav, but this one should win/overwrite it
url: '/my-section?from=1h&to=now',
},
};
const sectionNav: NavModelItem = {
text: 'My section',
url: '/my-section',
};
expect(buildBreadcrumbs(sectionNav, pageNav, mockHomeNav)).toEqual([
{ text: 'My section', href: '/my-section' },
{ text: 'Another section', href: '/my-section?from=1h&to=now' },
{ text: 'My page', href: '/my-page' },
]);
});
}); });
}); });

View File

@ -7,9 +7,8 @@ export function buildBreadcrumbs(sectionNav: NavModelItem, pageNav?: NavModelIte
const crumbs: Breadcrumb[] = []; const crumbs: Breadcrumb[] = [];
let foundHome = false; let foundHome = false;
let lastPath: string | undefined = undefined; let lastPath: string | undefined = undefined;
let lastText: string | undefined = undefined;
function addCrumbs(node: NavModelItem) { function addCrumbs(node: NavModelItem, shouldDedupe = false) {
if (foundHome) { if (foundHome) {
return; return;
} }
@ -32,16 +31,14 @@ export function buildBreadcrumbs(sectionNav: NavModelItem, pageNav?: NavModelIte
return; return;
} }
// This enabled app plugins to control breadcrumbs of their root pages
const isSamePathAsLastBreadcrumb = urlToMatch.length > 0 && lastPath === urlToMatch; const isSamePathAsLastBreadcrumb = urlToMatch.length > 0 && lastPath === urlToMatch;
const isSameTextAsLastBreadcrumb = node.text === lastText;
// Remember this path for the next breadcrumb // Remember this path for the next breadcrumb
lastPath = urlToMatch; lastPath = urlToMatch;
lastText = node.text;
const shouldMergeBreadcrumb = isSamePathAsLastBreadcrumb && isSameTextAsLastBreadcrumb; const shouldAddCrumb = !node.hideFromBreadcrumbs && !(shouldDedupe && isSamePathAsLastBreadcrumb);
if (!node.hideFromBreadcrumbs && !shouldMergeBreadcrumb) {
if (shouldAddCrumb) {
crumbs.unshift({ text: node.text, href: node.url ?? '' }); crumbs.unshift({ text: node.text, href: node.url ?? '' });
} }
@ -54,7 +51,8 @@ export function buildBreadcrumbs(sectionNav: NavModelItem, pageNav?: NavModelIte
addCrumbs(pageNav); addCrumbs(pageNav);
} }
addCrumbs(sectionNav); // shouldDedupe = true enables app plugins to control breadcrumbs of their root pages
addCrumbs(sectionNav, true);
return crumbs; return crumbs;
} }