diff --git a/Documentation/templates-src/NetCord/src/nav.ts b/Documentation/templates-src/NetCord/src/nav.ts index 678f030fc..ee16249cf 100644 --- a/Documentation/templates-src/NetCord/src/nav.ts +++ b/Documentation/templates-src/NetCord/src/nav.ts @@ -188,19 +188,34 @@ function inThisArticle(): TemplateResult { function findActiveItem(items: (NavItem | NavItemContainer)[]): NavItem { const url = new URL(window.location.href); + let activeItem: NavItem; + let maxPrefix = 0; for (const item of items.map((i) => ("items" in i ? i.items : i)).flat()) { - const href = item.href; - if (!isExternalHref(href) && commonUrl(url, href)) - return item; + if (isExternalHref(item.href)) { + continue; + } + const prefix = commonUrlPrefix(url, item.href); + if (prefix == maxPrefix) { + activeItem = undefined; + } + else if (prefix > maxPrefix) { + maxPrefix = prefix; + activeItem = item; + } } + return activeItem; } -function commonUrl(url: URL, base: URL): boolean { - const urlPath = normalizePath(url.pathname); - const basePath = normalizePath(base.pathname); - return urlPath === basePath; -} - -function normalizePath(url: string): string { - return url.replace(/\/(?:index.html)?$/, "/"); +function commonUrlPrefix(url: URL, base: URL): number { + const urlSegments = url.pathname.split("/"); + const baseSegments = base.pathname.split("/"); + let i = 0; + while ( + i < urlSegments.length && + i < baseSegments.length && + urlSegments[i] === baseSegments[i] + ) { + i++; + } + return i; }