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

@ -17,9 +17,6 @@ const {
imageUploader = false,
} = themeConfig.comment?.waline ?? {}
// Get current path
const currentPath = Astro.url.pathname
// Get the language code of Waline
function getWalineLang(currentPath: string, defaultLocale: string): string {
// Extract language code from path
@ -32,7 +29,7 @@ function getWalineLang(currentPath: string, defaultLocale: string): string {
}
// Get Waline language and generate configuration
const walineLang = getWalineLang(currentPath, defaultLocale)
const walineLang = getWalineLang(Astro.url.pathname, defaultLocale)
const walineConfigJson = JSON.stringify({
serverURL,
lang: walineLang,
@ -49,8 +46,7 @@ const walineConfigJson = JSON.stringify({
>
</div>
<!-- >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> -->
<!-- Not use is:inline or define:vars -->
<!-- Not use is:inline or define:vars >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> -->
<script>
import { init } from '@waline/client'
import '@waline/client/style'
@ -76,7 +72,7 @@ initWaline()
document.addEventListener('astro:after-swap', initWaline)
</script>
<!-- Custom css styles >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> -->
<!-- Custom css styles >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> -->
<style is:global>
#waline .wl-login-info {
--at-apply: 'mt-0 mr-3'

View file

@ -1,11 +1,10 @@
---
import themeConfig from '@/config'
import { getPagePath } from '@/i18n/path'
import { getPageInfo } from '@/i18n/path'
const { title, subtitle } = themeConfig.site
const { titleSpace } = themeConfig.global
const currentPath = Astro.url.pathname
const { getLocalizedPath } = getPagePath(currentPath)
const { getLocalizedPath } = getPageInfo(Astro.url.pathname)
const marginBottom = {
1: 'mb-0.625',

View file

@ -1,11 +1,10 @@
---
import themeConfig from '@/config'
import { getPagePath } from '@/i18n/path'
import { getPageInfo } from '@/i18n/path'
const { title, subtitle } = themeConfig.site
const { titleSpace } = themeConfig.global
const currentPath = Astro.url.pathname
const { getLocalizedPath } = getPagePath(currentPath)
const { getLocalizedPath } = getPageInfo(Astro.url.pathname)
const marginBottom = {
1: 'mb-1.625',

View file

@ -1,10 +1,9 @@
---
import { getPagePath } from '@/i18n/path'
import { getPageInfo } from '@/i18n/path'
import { ui } from '@/i18n/ui'
const currentPath = Astro.url.pathname
const { currentLang, isHome, isPost, isTag, isAbout, getLocalizedPath }
= getPagePath(currentPath)
= getPageInfo(Astro.url.pathname)
const currentUI = ui[currentLang as keyof typeof ui]
const isPostActive = isHome || isPost

View file

@ -10,8 +10,7 @@ interface Props {
const { date, updatedDate, minutes } = Astro.props
const format = themeConfig.global.dateFormat
const currentPath = Astro.url.pathname
const isPost = isPostPage(currentPath)
const isPost = isPostPage(Astro.url.pathname)
const timeSpacingClass = isPost ? 'ml-1.75' : 'ml-1.5'
function formatDate(date: Date, format: 'YYYY-MM-DD' | 'MM-DD-YYYY' | 'DD-MM-YYYY' | 'MONTH DAY YYYY' | 'DAY MONTH YYYY') {

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[]> = {}

View file

@ -9,7 +9,7 @@ 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 { getPagePath } from '@/i18n/path'
import { getPageInfo } from '@/i18n/path'
import Head from '@/layouts/Head.astro'
import '@/styles/font.css'
@ -24,7 +24,7 @@ interface Props {
}
const { postTitle, postDescription, postSlug, supportedLangs = [] } = Astro.props
const { isHome, isPost } = getPagePath(Astro.url.pathname)
const { isHome, isPost } = getPageInfo(Astro.url.pathname)
const { light: { background: lightMode }, dark: { background: darkMode } } = themeConfig.color
const fontStyle = themeConfig.global.fontStyle === 'serif' ? 'font-serif' : 'font-sans'
const footerMarginClass = isPost && themeConfig.comment?.waline?.serverURL

View file

@ -1,9 +1,9 @@
---
import { generateMultiLangPaths } from '@/i18n/route'
import { getMultiLangRoutes } from '@/i18n/route'
import Layout from '@/layouts/Layout.astro'
export function getStaticPaths() {
return generateMultiLangPaths()
return getMultiLangRoutes()
}
---

View file

@ -1,6 +1,6 @@
import type { APIContext } from 'astro'
import themeConfig from '@/config'
import { generateMultiLangPaths } from '@/i18n/route'
import { getMultiLangRoutes } from '@/i18n/route'
import { generateRSS } from '@/utils/rss'
const { moreLocales } = themeConfig.global
@ -10,7 +10,7 @@ type SupportedLanguage = typeof moreLocales[number]
// Generate static paths for all supported languages
export function getStaticPaths() {
return generateMultiLangPaths()
return getMultiLangRoutes()
}
export async function GET({ params }: APIContext) {

View file

@ -1,11 +1,11 @@
---
import PostList from '@/components/PostList.astro'
import { generateMultiLangPaths } from '@/i18n/route'
import { getMultiLangRoutes } from '@/i18n/route'
import Layout from '@/layouts/Layout.astro'
import { getPinnedPosts, getPostsByYear } from '@/utils/content'
export function getStaticPaths() {
return generateMultiLangPaths()
return getMultiLangRoutes()
}
const { lang } = Astro.params

View file

@ -1,7 +1,7 @@
---
import Comments from '@/components/Comments/index.astro'
import PostTime from '@/components/PostTime.astro'
import { generateMultiLangPostPaths } from '@/i18n/route'
import { getMultiLangPostRoutes } from '@/i18n/route'
import Layout from '@/layouts/Layout.astro'
import { checkSlugDuplication } from '@/utils/content'
import { generateDescription } from '@/utils/description'
@ -15,7 +15,7 @@ export async function getStaticPaths() {
throw new Error(`Slug conflicts found:\n${duplicates.join('\n')}`)
}
return generateMultiLangPostPaths(posts)
return getMultiLangPostRoutes(posts)
}
const { lang } = Astro.params

View file

@ -1,6 +1,6 @@
import type { APIContext } from 'astro'
import themeConfig from '@/config'
import { generateMultiLangPaths } from '@/i18n/route'
import { getMultiLangRoutes } from '@/i18n/route'
import { generateRSS } from '@/utils/rss'
const { moreLocales } = themeConfig.global
@ -10,7 +10,7 @@ type SupportedLanguage = typeof moreLocales[number]
// Generate static paths for all supported languages
export function getStaticPaths() {
return generateMultiLangPaths()
return getMultiLangRoutes()
}
export async function GET({ params }: APIContext) {

View file

@ -1,12 +1,12 @@
---
import PostList from '@/components/PostList.astro'
import { generateMultiLangTagPaths } from '@/i18n/route'
import { getMultiLangTagRoutes } from '@/i18n/route'
import Layout from '@/layouts/Layout.astro'
import { getAllTags, getPostsByTag } from '@/utils/content'
export async function getStaticPaths() {
const tags = await getAllTags()
return generateMultiLangTagPaths(tags)
return getMultiLangTagRoutes(tags)
}
const { lang } = Astro.params

View file

@ -1,10 +1,10 @@
---
import { generateMultiLangPaths } from '@/i18n/route'
import { getMultiLangRoutes } from '@/i18n/route'
import Layout from '@/layouts/Layout.astro'
import { getAllTags } from '@/utils/content'
export function getStaticPaths() {
return generateMultiLangPaths()
return getMultiLangRoutes()
}
const { lang } = Astro.params

View file

@ -1,7 +1,7 @@
---
import Comments from '@/components/Comments/index.astro'
import PostTime from '@/components/PostTime.astro'
import { generatePostPaths } from '@/i18n/route'
import { getPostRoutes } from '@/i18n/route'
import Layout from '@/layouts/Layout.astro'
import { checkSlugDuplication } from '@/utils/content'
import { generateDescription } from '@/utils/description'
@ -15,7 +15,7 @@ export async function getStaticPaths() {
throw new Error(`Slug conflicts found:\n${duplicates.join('\n')}`)
}
return generatePostPaths(posts)
return getPostRoutes(posts)
}
const { post, supportedLangs = [] } = Astro.props

View file

@ -1,12 +1,12 @@
---
import PostList from '@/components/PostList.astro'
import { generateTagPaths } from '@/i18n/route'
import { getTagRoutes } from '@/i18n/route'
import Layout from '@/layouts/Layout.astro'
import { getAllTags, getPostsByTag } from '@/utils/content'
export async function getStaticPaths() {
const tags = await getAllTags()
return generateTagPaths(tags)
return getTagRoutes(tags)
}
const { tag } = Astro.props