Commit 6e6aeb5
Changed files (3)
src
pages
utils
src/pages/posts/index.astro
@@ -1,5 +1,6 @@
---
import { getCollection } from "astro:content";
+import { url } from "@/utils/url";
import BaseLayout from "@/layouts/BaseLayout.astro";
@@ -9,6 +10,6 @@ const posts = await getCollection("posts");
<h1>Posts</h1>
{
- posts.map(post => (<li><a href={"/posts/" + post.id}>{post.data.title}</a></li>))
+ posts.map(post => (<li><a href={url("/posts/" + post.id)}>{post.data.title}</a></li>))
}
</BaseLayout>
src/pages/index.astro
@@ -1,7 +1,9 @@
---
+import { url } from "@/utils/url.ts";
+
import BaseLayout from "@/layouts/BaseLayout.astro";
---
<BaseLayout title="Astro">
<h1>Astro</h1>
- <a href="/posts">Posts</a>
+ <a href={url("/posts")}>Posts</a>
</BaseLayout>
src/utils/url.ts
@@ -0,0 +1,22 @@
+export function joinUrl(...parts: string[]): string {
+ const joined = parts.join("/");
+ return joined.replace(/\/+/g, "/");
+}
+
+export function pathsEqual(path1: string, path2: string) {
+ const normalizedPath1 = path1.replace(/^\/|\/$/g, "").toLowerCase();
+ const normalizedPath2 = path2.replace(/^\/|\/$/g, "").toLowerCase();
+ return normalizedPath1 === normalizedPath2;
+}
+
+export function pathMatch(regex: RegExp, path: string) {
+ return regex.test(path) || regex.test(getRelativeUrl(path));
+}
+
+export function getRelativeUrl(path: string) {
+ return joinUrl("/", path.replace(import.meta.env.BASE_URL, ""));
+}
+
+export function url(path: string) {
+ return joinUrl("", import.meta.env.BASE_URL, path);
+}