From 8d6f4c1c83102af5b408999a0641eaf50745df98 Mon Sep 17 00:00:00 2001 From: KubaZ2 Date: Fri, 30 Aug 2024 23:11:51 +0200 Subject: [PATCH 1/2] Try to fix --- .../templates-src/NetCord/src/nav.ts | 34 +++++++++++++------ 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/Documentation/templates-src/NetCord/src/nav.ts b/Documentation/templates-src/NetCord/src/nav.ts index 678f030fc..91c5ccb89 100644 --- a/Documentation/templates-src/NetCord/src/nav.ts +++ b/Documentation/templates-src/NetCord/src/nav.ts @@ -188,19 +188,31 @@ function inThisArticle(): TemplateResult { function findActiveItem(items: (NavItem | NavItemContainer)[]): NavItem { const url = new URL(window.location.href); + let activeItem: NavItem; + let maxPrefix = 1; 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) { + 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; } From 328bc4910e87239d4035186dc3764dd68006075f Mon Sep 17 00:00:00 2001 From: KubaZ2 Date: Fri, 30 Aug 2024 23:19:44 +0200 Subject: [PATCH 2/2] Another try --- Documentation/templates-src/NetCord/src/nav.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/Documentation/templates-src/NetCord/src/nav.ts b/Documentation/templates-src/NetCord/src/nav.ts index 91c5ccb89..ee16249cf 100644 --- a/Documentation/templates-src/NetCord/src/nav.ts +++ b/Documentation/templates-src/NetCord/src/nav.ts @@ -189,13 +189,16 @@ function inThisArticle(): TemplateResult { function findActiveItem(items: (NavItem | NavItemContainer)[]): NavItem { const url = new URL(window.location.href); let activeItem: NavItem; - let maxPrefix = 1; + let maxPrefix = 0; for (const item of items.map((i) => ("items" in i ? i.items : i)).flat()) { if (isExternalHref(item.href)) { continue; } const prefix = commonUrlPrefix(url, item.href); - if (prefix > maxPrefix) { + if (prefix == maxPrefix) { + activeItem = undefined; + } + else if (prefix > maxPrefix) { maxPrefix = prefix; activeItem = item; }