From a86cf3830d35f2f75d5ba8d5959827d66ba3e718 Mon Sep 17 00:00:00 2001 From: David Mosberger-Tang Date: Thu, 19 Dec 2024 14:53:38 -0700 Subject: [PATCH] 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. --- src/services/MenuBuilder.ts | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/services/MenuBuilder.ts b/src/services/MenuBuilder.ts index bbf7bb9cb..d10d4e5a0 100644 --- a/src/services/MenuBuilder.ts +++ b/src/services/MenuBuilder.ts @@ -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; }