Skip to content

Commit

Permalink
Fix docs preview (#38)
Browse files Browse the repository at this point in the history
* Fix index buttons

* Fix nav
  • Loading branch information
KubaZ2 committed Aug 30, 2024
1 parent d14b5d9 commit 1b9ebb3
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
<h1 class="title">NetCord</h1>
<p class="subtitle">The modern and fully customizable C# Discord library</p>
<section class="buttons">
<a href="/guides/getting-started/installation.html" class="button primary-button">Get Started</a>
<a href="/guides/getting-started/installation.html" class="button secondary-button">Guides</a>
<a href="/docs/index.html" class="button secondary-button">Docs</a>
<a href="guides/getting-started/installation.html" class="button primary-button">Get Started</a>
<a href="guides/getting-started/installation.html" class="button secondary-button">Guides</a>
<a href="docs/index.html" class="button secondary-button">Docs</a>
</section>
</div>
34 changes: 11 additions & 23 deletions Documentation/templates-src/NetCord/src/nav.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)?$/, "/");
}

0 comments on commit 1b9ebb3

Please sign in to comment.