Skip to content

Commit b0f59ea

Browse files
authored
fix: active menu item (#30)
Co-authored-by: Andrey <[email protected]>
1 parent e791f80 commit b0f59ea

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

src/components/Navigation.astro

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
---
2+
import { stripPath } from "../utils";
3+
24
const pathname = new URL(Astro.request.url).pathname;
3-
const currentPath = pathname.slice(1); // remove the first "/"
4-
const isActive = (path: string) => currentPath === path ? 'active' : '';
5+
const isActive = (path: string) => stripPath(pathname) === path ? 'active' : '';
56
---
67

78
<nav>

src/utils/index.ts

+10
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,13 @@ export function toDate(date?: Date, month: 'long' | 'short' = 'long') {
99
year: "numeric",
1010
}) || '';
1111
}
12+
13+
export function stripPath(path: string) {
14+
if (path.startsWith('/')) {
15+
path = path.substring(1);
16+
}
17+
if (path.endsWith('/')) {
18+
path = path.slice(0, -1);
19+
}
20+
return path;
21+
}

0 commit comments

Comments
 (0)