blog/src/utils/url-utils.ts

34 lines
1019 B
TypeScript
Raw Normal View History

2024-01-21 20:21:04 +08:00
import {i18n} from "@i18n/translation.ts";
import i18nKey from "@i18n/i18nKey.ts";
export function pathsEqual(path1: string, path2: string) {
const normalizedPath1 = path1.replace(/^\/|\/$/g, '').toLowerCase()
const normalizedPath2 = path2.replace(/^\/|\/$/g, '').toLowerCase()
return normalizedPath1 === normalizedPath2
}
2023-11-03 14:10:54 +08:00
function joinUrl(...parts: string[]): string {
const joined = parts.join('/')
return joined.replace(/([^:]\/)\/+/g, '$1')
2023-11-03 14:10:54 +08:00
}
export function getPostUrlBySlug(slug: string): string | null {
if (!slug) return null
return `/posts/${slug}`
}
export function getCategoryUrl(category: string): string | null {
2024-01-21 20:21:04 +08:00
if (!category)
return null
if (category === i18n(i18nKey.uncategorized))
return '/archive/category/uncategorized'
return `/archive/category/${category}`
}
export function getDir(path: string): string {
const lastSlashIndex = path.lastIndexOf('/')
if (lastSlashIndex < 0) {
return '/'
}
return path.substring(0, lastSlashIndex + 1)
}