Files
fuwari/src/utils/url-utils.ts
saicaca 559a324ae5
Some checks failed
Code quality / quality (push) Failing after 4s
Build and Check / Astro Check for Node.js 22 (push) Failing after 4s
Build and Check / Astro Check for Node.js 23 (push) Failing after 4s
Build and Check / Astro Build for Node.js 22 (push) Failing after 5s
Build and Check / Astro Build for Node.js 23 (push) Failing after 4s
fix: fix type errors
2025-05-30 20:34:35 +08:00

45 lines
1.3 KiB
TypeScript

import I18nKey from "@i18n/i18nKey";
import { i18n } from "@i18n/translation";
export function pathsEqual(path1: string, path2: string) {
const normalizedPath1 = path1.replace(/^\/|\/$/g, "").toLowerCase();
const normalizedPath2 = path2.replace(/^\/|\/$/g, "").toLowerCase();
return normalizedPath1 === normalizedPath2;
}
function joinUrl(...parts: string[]): string {
const joined = parts.join("/");
return joined.replace(/\/+/g, "/");
}
export function getPostUrlBySlug(slug: string): string {
return url(`/posts/${slug}/`);
}
export function getTagUrl(tag: string): string {
if (!tag) return url("/archive/");
return url(`/archive/?tag=${encodeURIComponent(tag.trim())}`);
}
export function getCategoryUrl(category: string | null): string {
if (
!category ||
category.trim() === "" ||
category.trim().toLowerCase() === i18n(I18nKey.uncategorized).toLowerCase()
)
return url("/archive/?uncategorized=true");
return url(`/archive/?category=${encodeURIComponent(category.trim())}`);
}
export function getDir(path: string): string {
const lastSlashIndex = path.lastIndexOf("/");
if (lastSlashIndex < 0) {
return "/";
}
return path.substring(0, lastSlashIndex + 1);
}
export function url(path: string) {
return joinUrl("", import.meta.env.BASE_URL, path);
}