refactor: rename functions

This commit is contained in:
radishzzz 2025-03-14 14:23:35 +00:00
parent ca1abd28c1
commit f5526f8622
17 changed files with 46 additions and 49 deletions

View file

@ -1,9 +1,11 @@
import { defaultLocale, moreLocales } from '@/i18n/config'
// Removes leading and trailing slashes from a path
export function cleanPath(path: string) {
return path.replace(/^\/|\/$/g, '')
}
// Gets the language code from the current path
export function getLangFromPath(path: string) {
const secondaryLang = moreLocales.find(
lang =>
@ -12,6 +14,7 @@ export function getLangFromPath(path: string) {
return secondaryLang || defaultLocale
}
// Generates a localized path based on current language
export function getLocalizedPath(path: string, currentLang?: string) {
const clean = cleanPath(path)
const lang = currentLang || getLangFromPath(path)
@ -23,6 +26,7 @@ export function getLocalizedPath(path: string, currentLang?: string) {
return lang === defaultLocale ? `/${clean}/` : `/${lang}/${clean}/`
}
// Checks if the current path is the home/post/tag/about page
export function isHomePage(path: string) {
const clean = cleanPath(path)
return clean === '' || moreLocales.includes(clean)
@ -40,7 +44,8 @@ export function isAboutPage(path: string) {
return clean.startsWith('about') || moreLocales.some(lang => clean.startsWith(`${lang}/about`))
}
export function getPagePath(path: string) {
// Returns page context including language and page type information
export function getPageInfo(path: string) {
const currentLang = getLangFromPath(path)
return {