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 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),
}
}