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 {

View file

@ -1,16 +1,16 @@
import type { CollectionEntry } from 'astro:content'
import { allLocales, defaultLocale, moreLocales } from '@/i18n/config'
// 生成默认语言标签页面的路配置
export function generateTagPaths(tags: string[]) {
// 生成默认语言标签页面的路配置
export function getTagRoutes(tags: string[]) {
return tags.map(tag => ({
params: { tag },
props: { tag },
}))
}
// 生成默认语言文章页面的路配置
export function generatePostPaths(posts: CollectionEntry<'posts'>[]) {
// 生成默认语言文章页面的路配置
export function getPostRoutes(posts: CollectionEntry<'posts'>[]) {
// 创建slug到语言的映射
const slugToLangs: Record<string, string[]> = {}
@ -45,15 +45,15 @@ export function generatePostPaths(posts: CollectionEntry<'posts'>[]) {
}))
}
// 生成更多语言静态路
export function generateMultiLangPaths() {
// 生成更多语言静态路
export function getMultiLangRoutes() {
return moreLocales.map(lang => ({
params: { lang },
}))
}
// 生成更多语言标签页面的路配置
export function generateMultiLangTagPaths(tags: string[]) {
// 生成更多语言标签页面的路配置
export function getMultiLangTagRoutes(tags: string[]) {
return moreLocales.flatMap(lang => (
tags.map(tag => ({
params: { lang, tag },
@ -62,8 +62,8 @@ export function generateMultiLangTagPaths(tags: string[]) {
))
}
// 生成更多语言文章页面的路配置
export function generateMultiLangPostPaths(posts: CollectionEntry<'posts'>[]) {
// 生成更多语言文章页面的路配置
export function getMultiLangPostRoutes(posts: CollectionEntry<'posts'>[]) {
// 创建slug到语言的映射
const slugToLangs: Record<string, string[]> = {}