fix: when building factored SideNav menu, don't drop final children

At the end of the loop, we need to append the remaining new children,
if there are any.
This commit is contained in:
David Mosberger-Tang 2024-12-19 14:53:38 -07:00
parent b5c5436fac
commit a86cf3830d

View File

@ -8,6 +8,15 @@ import type { ContentItemModel, TagGroup, TagInfo, TagsInfoMap } from './types';
export const GROUP_DEPTH = 0;
function appendChildren(parent: ContentItemModel, children: ContentItemModel[], prefix: string) {
for (const child of MenuBuilder.factorByPrefix(children)) {
if (child.sidebarLabel.startsWith(prefix)) {
child.sidebarLabel = '…' + child.sidebarLabel.slice(prefix.length - 1);
}
parent.items.push(child);
}
}
export class MenuBuilder {
/**
* Builds page content structure based on tags
@ -46,12 +55,7 @@ export class MenuBuilder {
newChildren.push(item);
} else {
if (newChildren.length > 0) {
for (const child of MenuBuilder.factorByPrefix(newChildren)) {
if (child.sidebarLabel.startsWith(prefix)) {
child.sidebarLabel = '…' + child.sidebarLabel.slice(prefix.length - 1);
}
parent!.items.push(child);
}
appendChildren(parent!, newChildren, prefix);
newChildren = [];
}
@ -62,6 +66,7 @@ export class MenuBuilder {
} else parent = null;
}
}
if (newChildren.length > 0) appendChildren(parent!, newChildren, prefix);
return newItems;
}