refactor: organize and categorize functions

This commit is contained in:
radishzzz 2025-03-14 16:18:17 +00:00
parent f5526f8622
commit d352b6fb65
13 changed files with 150 additions and 176 deletions

View file

@ -1,6 +1,6 @@
---
import themeConfig from '@/config'
import { getPageInfo } from '@/i18n/path'
import { getPageInfo } from '@/utils/page'
const { title, subtitle } = themeConfig.site
const { titleSpace } = themeConfig.global

View file

@ -1,6 +1,6 @@
---
import themeConfig from '@/config'
import { getPageInfo } from '@/i18n/path'
import { getPageInfo } from '@/utils/page'
const { title, subtitle } = themeConfig.site
const { titleSpace } = themeConfig.global

View file

@ -1,6 +1,6 @@
---
import { getPageInfo } from '@/i18n/path'
import { ui } from '@/i18n/ui'
import { getPageInfo } from '@/utils/page'
const { currentLang, isHome, isPost, isTag, isAbout, getLocalizedPath }
= getPageInfo(Astro.url.pathname)

View file

@ -1,6 +1,6 @@
---
import { themeConfig } from '@/config'
import { isPostPage } from '@/i18n/path'
import { isPostPage } from '@/utils/page'
interface Props {
date: Date

View file

@ -1,6 +1,6 @@
---
import { getNextLangUrl, getPostNextLangUrl } from '@/i18n/lang'
import { isPostPage } from '@/i18n/path'
import { getNextLangUrl, getPostNextLangUrl } from '@/i18n/path'
import { isPostPage } from '@/utils/page'
interface Props {
supportedLangs: string[]

View file

@ -1,5 +1,13 @@
import { allLocales, defaultLocale, moreLocales } from '@/i18n/config'
import { walineLocaleMap } from '@/i18n/ui'
// Gets the language code from the current path
export function getLangFromPath(path: string) {
const secondaryLang = moreLocales.find(
lang =>
path.startsWith(`/${lang}/`),
)
return secondaryLang || defaultLocale
}
/**
*
@ -25,70 +33,6 @@ export function getNextLang(currentLang: string): string {
return allLocales[nextIndex]
}
/**
* URL
* @param currentPath
* @param currentLang
* @param nextLang
* @returns URL
*/
export function buildNextLangUrl(currentPath: string, currentLang: string, nextLang: string): string {
// 直接使用导入的变量
let nextUrl = ''
if (nextLang === defaultLocale) {
// 如果下一个是默认语言,移除语言代码
nextUrl = currentPath.replace(`/${currentLang}`, '') || '/'
}
else {
// 如果当前是默认语言(没有语言代码在路径中)
if (currentLang === defaultLocale) {
// 在路径前添加新的语言代码
nextUrl = `/${nextLang}${currentPath}`
}
else {
// 替换当前语言代码为新的语言代码
nextUrl = currentPath.replace(`/${currentLang}`, `/${nextLang}`)
}
}
// 确保URL格式正确
if (nextUrl === '')
nextUrl = '/'
// 确保非根路径的URL末尾有斜杠
if (nextUrl !== '/' && !nextUrl.endsWith('/')) {
nextUrl = `${nextUrl}/`
}
return nextUrl
}
/**
*
* @param currentPath
* @returns
*/
export function getLangFromPath(currentPath: string): string {
// 直接使用导入的变量
let currentLang = ''
// 检查路径是否以/xx/开始其中xx是支持的语言代码
for (const lang of moreLocales) {
if (currentPath.startsWith(`/${lang}/`) || currentPath === `/${lang}`) {
currentLang = lang
break
}
}
// 如果没有找到语言代码,则认为是默认语言
if (!currentLang) {
currentLang = defaultLocale
}
return currentLang
}
/**
*
* @param lang
@ -105,52 +49,3 @@ export function getSupportedLangs(lang?: string): string[] {
// 否则返回所有支持的语言
return allLocales
}
/**
* URL
* @param currentPath
* @returns URL
*/
export function getNextLangUrl(currentPath: string): string {
// 从路径提取当前语言
const currentLang = getLangFromPath(currentPath)
// 获取下一个语言
const nextLang = getNextLang(currentLang)
// 构建下一个语言的URL
return buildNextLangUrl(currentPath, currentLang, nextLang)
}
/**
* URL
* @param currentPath
* @param supportedLangs
* @returns URL
*/
export function getPostNextLangUrl(currentPath: string, supportedLangs: string[]): string {
// 从路径提取当前语言
const currentLang = getLangFromPath(currentPath)
// 如果没有提供支持的语言或列表为空,使用普通的语言切换
if (!supportedLangs || supportedLangs.length === 0) {
return getNextLangUrl(currentPath)
}
// 找到当前语言在支持的语言中的索引
const currentIndex = supportedLangs.indexOf(currentLang)
// 如果当前语言不在支持的语言中,或者路径是根路径,返回第一个支持的语言
if (currentIndex === -1 || currentPath === '/') {
const nextLang = supportedLangs[0]
// 如果下一个语言是默认语言,返回根路径
return nextLang === defaultLocale ? '/' : `/${nextLang}/`
}
// 计算下一个语言的索引
const nextIndex = (currentIndex + 1) % supportedLangs.length
const nextLang = supportedLangs[nextIndex]
// 构建下一个语言的URL
return buildNextLangUrl(currentPath, currentLang, nextLang)
}

View file

@ -1,18 +1,6 @@
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 =>
path.startsWith(`/${lang}/`),
)
return secondaryLang || defaultLocale
}
import { defaultLocale } from '@/i18n/config'
import { getLangFromPath, getNextLang } from '@/i18n/lang'
import { cleanPath } from '@/utils/page'
// Generates a localized path based on current language
export function getLocalizedPath(path: string, currentLang?: string) {
@ -26,34 +14,90 @@ 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)
/**
* URL
* @param currentPath
* @param currentLang
* @param nextLang
* @returns URL
*/
export function buildNextLangUrl(currentPath: string, currentLang: string, nextLang: string): string {
// 直接使用导入的变量
let nextUrl = ''
if (nextLang === defaultLocale) {
// 如果下一个是默认语言,移除语言代码
nextUrl = currentPath.replace(`/${currentLang}`, '') || '/'
}
export function isPostPage(path: string) {
const clean = cleanPath(path)
return clean.startsWith('posts') || moreLocales.some(lang => clean.startsWith(`${lang}/posts`))
else {
// 如果当前是默认语言(没有语言代码在路径中)
if (currentLang === defaultLocale) {
// 在路径前添加新的语言代码
nextUrl = `/${nextLang}${currentPath}`
}
export function isTagPage(path: string) {
const clean = cleanPath(path)
return clean.startsWith('tags') || moreLocales.some(lang => clean.startsWith(`${lang}/tags`))
else {
// 替换当前语言代码为新的语言代码
nextUrl = currentPath.replace(`/${currentLang}`, `/${nextLang}`)
}
export function isAboutPage(path: string) {
const clean = cleanPath(path)
return clean.startsWith('about') || moreLocales.some(lang => clean.startsWith(`${lang}/about`))
}
// Returns page context including language and page type information
export function getPageInfo(path: string) {
const currentLang = getLangFromPath(path)
// 确保URL格式正确
if (nextUrl === '')
nextUrl = '/'
return {
currentLang,
isHome: isHomePage(path),
isPost: isPostPage(path),
isTag: isTagPage(path),
isAbout: isAboutPage(path),
getLocalizedPath: (targetPath: string) => getLocalizedPath(targetPath, currentLang),
// 确保非根路径的URL末尾有斜杠
if (nextUrl !== '/' && !nextUrl.endsWith('/')) {
nextUrl = `${nextUrl}/`
}
return nextUrl
}
/**
* URL
* @param currentPath
* @returns URL
*/
export function getNextLangUrl(currentPath: string): string {
// 从路径提取当前语言
const currentLang = getLangFromPath(currentPath)
// 获取下一个语言
const nextLang = getNextLang(currentLang)
// 构建下一个语言的URL
return buildNextLangUrl(currentPath, currentLang, nextLang)
}
/**
* URL
* @param currentPath
* @param supportedLangs
* @returns URL
*/
export function getPostNextLangUrl(currentPath: string, supportedLangs: string[]): string {
// 从路径提取当前语言
const currentLang = getLangFromPath(currentPath)
// 如果没有提供支持的语言或列表为空,使用普通的语言切换
if (!supportedLangs || supportedLangs.length === 0) {
return getNextLangUrl(currentPath)
}
// 找到当前语言在支持的语言中的索引
const currentIndex = supportedLangs.indexOf(currentLang)
// 如果当前语言不在支持的语言中,或者路径是根路径,返回第一个支持的语言
if (currentIndex === -1 || currentPath === '/') {
const nextLang = supportedLangs[0]
// 如果下一个语言是默认语言,返回根路径
return nextLang === defaultLocale ? '/' : `/${nextLang}/`
}
// 计算下一个语言的索引
const nextIndex = (currentIndex + 1) % supportedLangs.length
const nextLang = supportedLangs[nextIndex]
// 构建下一个语言的URL
return buildNextLangUrl(currentPath, currentLang, nextLang)
}

View file

@ -19,13 +19,9 @@ export const walineLocaleMap: Record<string, string> = {
'ru': 'ru-RU',
}
// Standard Language Code (Unused)
export const langCode = Object.values(langMap).flat()
// Supported Languages
export const supportedLangs = Object.keys(langMap).flat()
// Abbreviated Language Code
export const langPath = Object.keys(langMap).flat()
// UI Translation
export const ui = {
'zh': {
posts: '文章',

View file

@ -9,8 +9,8 @@ import LanguageSwitcher from '@/components/Widgets/LanguageSwitcher.astro'
// import Scrollbar from '@/components/Scrollbar.astro'
import ThemeToggle from '@/components/Widgets/ThemeToggle.astro'
import themeConfig from '@/config'
import { getPageInfo } from '@/i18n/path'
import Head from '@/layouts/Head.astro'
import { getPageInfo } from '@/utils/page'
import '@/styles/font.css'
import '@/styles/global.css'

View file

@ -11,7 +11,7 @@ export async function getStaticPaths() {
const { lang } = Astro.params
const { tag } = Astro.props
const posts = await getPostsByTag(tag)
const posts = await getPostsByTag(tag, lang)
const allTags = await getAllTags()
---

View file

@ -1,4 +1,4 @@
import type { langPath } from '@/i18n/ui'
import type { supportedLangs } from '@/i18n/ui'
type Exclude<T, U> = T extends U ? never : T
@ -30,8 +30,8 @@ export interface ThemeConfig {
}
global: {
locale: typeof langPath[number]
moreLocales: typeof langPath[number][]
locale: typeof supportedLangs[number]
moreLocales: typeof supportedLangs[number][]
fontStyle: 'sans' | 'serif'
dateFormat: 'YYYY-MM-DD' | 'MM-DD-YYYY' | 'DD-MM-YYYY' | 'MONTH DAY YYYY' | 'DAY MONTH YYYY'
titleSpace: 1 | 2 | 3

View file

@ -1,6 +1,6 @@
import type { CollectionEntry } from 'astro:content'
import themeConfig from '@/config'
import { langPath } from '@/i18n/ui'
import { supportedLangs } from '@/i18n/ui'
import { getCollection } from 'astro:content'
// Type definitions
@ -52,7 +52,7 @@ export async function getPosts(lang?: string) {
'posts',
({ data }: Post) => {
const shouldInclude = import.meta.env.DEV || !data.draft
if (!langPath.includes(currentLang)) {
if (!supportedLangs.includes(currentLang)) {
return shouldInclude && data.lang === ''
}
return shouldInclude && (data.lang === currentLang || data.lang === '')

39
src/utils/page.ts Normal file
View file

@ -0,0 +1,39 @@
import { moreLocales } from '@/i18n/config'
import { getLangFromPath } from '@/i18n/lang'
import { getLocalizedPath } from '@/i18n/path'
// Removes leading and trailing slashes from a path
export function cleanPath(path: string) {
return path.replace(/^\/|\/$/g, '')
}
// 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)
}
export function isPostPage(path: string) {
const clean = cleanPath(path)
return clean.startsWith('posts') || moreLocales.some(lang => clean.startsWith(`${lang}/posts`))
}
export function isTagPage(path: string) {
const clean = cleanPath(path)
return clean.startsWith('tags') || moreLocales.some(lang => clean.startsWith(`${lang}/tags`))
}
export function isAboutPage(path: string) {
const clean = cleanPath(path)
return clean.startsWith('about') || moreLocales.some(lang => clean.startsWith(`${lang}/about`))
}
// Returns page context including language and page type information
export function getPageInfo(path: string) {
const currentLang = getLangFromPath(path)
return {
currentLang,
isHome: isHomePage(path),
isPost: isPostPage(path),
isTag: isTagPage(path),
isAbout: isAboutPage(path),
getLocalizedPath: (targetPath: string) => getLocalizedPath(targetPath, currentLang),
}
}