From 1b9ebb37ad2d616ed65714c1a485bba466f265f6 Mon Sep 17 00:00:00 2001 From: Kuba_Z2 <77853483+KubaZ2@users.noreply.github.com> Date: Fri, 30 Aug 2024 19:35:18 +0200 Subject: [PATCH] Fix docs preview (#38) * Fix index buttons * Fix nav --- .../NetCord/partials/root.tmpl.partial | 6 ++-- .../templates-src/NetCord/src/nav.ts | 34 ++++++------------- 2 files changed, 14 insertions(+), 26 deletions(-) diff --git a/Documentation/templates-src/NetCord/partials/root.tmpl.partial b/Documentation/templates-src/NetCord/partials/root.tmpl.partial index 15f3b177..44223a5e 100644 --- a/Documentation/templates-src/NetCord/partials/root.tmpl.partial +++ b/Documentation/templates-src/NetCord/partials/root.tmpl.partial @@ -2,8 +2,8 @@

NetCord

The modern and fully customizable C# Discord library

- Get Started - Guides - Docs + Get Started + Guides + Docs
\ No newline at end of file diff --git a/Documentation/templates-src/NetCord/src/nav.ts b/Documentation/templates-src/NetCord/src/nav.ts index bb6379a6..678f030f 100644 --- a/Documentation/templates-src/NetCord/src/nav.ts +++ b/Documentation/templates-src/NetCord/src/nav.ts @@ -188,31 +188,19 @@ 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()) { - if (isExternalHref(item.href)) { - continue; - } - const prefix = commonUrlPrefix(url, item.href); - if (prefix > maxPrefix) { - maxPrefix = prefix; - activeItem = item; - } + const href = item.href; + if (!isExternalHref(href) && commonUrl(url, href)) + return item; } - return activeItem; } -function commonUrlPrefix(url: URL, base: URL): number { - const urlSegments = url.pathname.split("/"); - const baseSegments = base.pathname.split("/"); - let i = 1; - while ( - i < urlSegments.length && - i < baseSegments.length && - urlSegments[i] === baseSegments[i] - ) { - i++; - } - return i; +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)?$/, "/"); }