main
1import { DEFAULT_LOCALE, SITE_CONFIG } from "@/config.ts";
2
3export function joinUrl(...parts: string[]): string {
4 const joined = parts.join("/");
5 return joined.replace(/\/+/g, "/");
6}
7
8export function pathsEqual(path1: string, path2: string) {
9 const normalizedPath1 = path1.replace(/^\/|\/$/g, "").toLowerCase();
10 const normalizedPath2 = path2.replace(/^\/|\/$/g, "").toLowerCase();
11 return normalizedPath1 === normalizedPath2;
12}
13
14export function pathMatch(regex: RegExp, path: string) {
15 return regex.test(path) || regex.test(getRelativeUrl(path));
16}
17
18export function getRelativeUrl(path: string) {
19 return joinUrl("/", path.replace(import.meta.env.BASE_URL, ""));
20}
21
22export function getLocaleRouteParam(locale: string): string | undefined {
23 return locale === DEFAULT_LOCALE ? undefined : locale;
24}
25
26export function getRelativeLocaleUrl(path: string, locale = DEFAULT_LOCALE) {
27 return url(joinUrl(getLocaleRouteParam(locale) ?? "", path, "/"));
28}
29
30export function getLocaleFeedUrl(locale = DEFAULT_LOCALE) {
31 return url(`/feed.${locale}.atom`);
32}
33
34export function getPathWithoutLocale(path: string): string {
35 const relative = getRelativeUrl(path);
36 const first = relative.split("/")[1] ?? "";
37 const isLocale = SITE_CONFIG.locales.some(
38 (locale) => locale.toLowerCase() === first.toLowerCase(),
39 );
40 if (!isLocale) {
41 return relative;
42 }
43 return joinUrl("/", relative.slice(first.length + 1));
44}
45
46export function url(path: string) {
47 return joinUrl("", import.meta.env.BASE_URL, path);
48}