refactor: restructure project configuration and utility modules

- Move configuration and utility files to more organized locations
- Update import paths across the project to reflect new file structure
- Simplify content and internationalization utilities
- Remove redundant configuration files and consolidate logic
- Add prefetch configuration to Astro config for improved performance
This commit is contained in:
radishzzz 2025-01-25 08:19:31 +00:00
parent a26031d490
commit fc1daf4335
23 changed files with 380 additions and 393 deletions

View file

@ -1,11 +1,9 @@
---
import { themeConfig } from '@/config'
import Layout from '@/layouts/Layout.astro'
import { generateLanguagePaths } from '@/utils/i18n'
export function getStaticPaths() {
return themeConfig.global.moreLocale.map(lang => ({
params: { lang },
}))
return generateLanguagePaths()
}
---

View file

@ -1,12 +1,10 @@
---
import { themeConfig } from '@/config'
import Layout from '@/layouts/Layout.astro'
import { getPinnedPosts, getPosts } from '@/utils/content.config'
import { getPinnedPosts, getPosts } from '@/utils/content'
import { generateLanguagePaths } from '@/utils/i18n'
export function getStaticPaths() {
return themeConfig.global.moreLocale.map(lang => ({
params: { lang },
}))
return generateLanguagePaths()
}
const { lang } = Astro.params

View file

@ -1,26 +1,18 @@
---
import { themeConfig } from '@/config'
import Layout from '@/layouts/Layout.astro'
import { checkSlugDuplication } from '@/utils/content.config'
import { checkSlugDuplication } from '@/utils/content'
import { generateMultiLangPostPaths } from '@/utils/i18n'
import { getCollection } from 'astro:content'
export async function getStaticPaths() {
const posts = await getCollection('posts')
const duplicates = await checkSlugDuplication(posts)
if (duplicates.length > 0) {
throw new Error(`Slug conflicts found:\n${duplicates.join('\n')}`)
}
return themeConfig.global.moreLocale.flatMap(lang =>
posts.map(post => ({
params: {
lang,
slug: post.data.slug || post.slug,
},
props: { post },
})),
)
return generateMultiLangPostPaths(posts)
}
const { post } = Astro.props

View file

@ -1,38 +1,8 @@
import type { APIContext } from 'astro'
import type { CollectionEntry } from 'astro:content'
import themeConfig from '@/config'
import rss from '@astrojs/rss'
import { getCollection } from 'astro:content'
import MarkdownIt from 'markdown-it'
import sanitizeHtml from 'sanitize-html'
import { generateRSS } from '@/utils/rss'
const parser = new MarkdownIt()
const { title, description, url } = themeConfig.site
const { moreLocale } = themeConfig.global
const followConfig = themeConfig.seo?.follow
// Returns first 200 chars with proper truncation
function getExcerpt(content: string): string {
if (!content)
return ''
// Convert markdown to plain text by removing all HTML tags
const plainText = sanitizeHtml(parser.render(content), {
allowedTags: [],
allowedAttributes: {},
})
const excerpt = plainText.slice(0, 200).trim()
return excerpt.length === 200 ? `${excerpt}...` : excerpt
}
// Return 404 for invalid language paths
function return404() {
return new Response(null, {
status: 404,
statusText: 'Not found',
})
}
// Type for supported non-default languages
type SupportedLanguage = typeof moreLocale[number]
@ -46,43 +16,12 @@ export async function GET({ params }: APIContext) {
const lang = params.lang as SupportedLanguage
// Return 404 if language is not supported
if (!moreLocale.includes(lang))
return return404()
if (!moreLocale.includes(lang)) {
return new Response(null, {
status: 404,
statusText: 'Not found',
})
}
// Get posts for specific language (including universal posts)
const posts = await getCollection(
'posts',
({ data }: { data: CollectionEntry<'posts'>['data'] }) =>
(!data.draft && (data.lang === lang || data.lang === '')),
)
return rss({
title: `${title} (${lang})`,
description,
site: url,
items: posts.map((post: CollectionEntry<'posts'>) => ({
title: post.data.title,
pubDate: post.data.published,
description: post.data.description || getExcerpt(post.body),
// Generate absolute URL with language prefix
link: new URL(`${lang}/posts/${post.data.slug || post.slug}/`, url).toString(),
// Convert markdown content to HTML, allowing img tags
content: post.body
? sanitizeHtml(parser.render(post.body), {
allowedTags: sanitizeHtml.defaults.allowedTags.concat(['img']),
})
: '',
})),
// Add XML namespaces for language and follow challenge
customData: `
<language>${lang}</language>
${followConfig?.feedID && followConfig?.userID
? `<follow_challenge>
<feedId>${followConfig.feedID}</feedId>
<userId>${followConfig.userID}</userId>
</follow_challenge>`
: ''
}
`.trim(),
})
return generateRSS({ lang })
}

View file

@ -1,16 +1,11 @@
---
import { themeConfig } from '@/config'
import Layout from '@/layouts/Layout.astro'
import { getAllTags, getPostsByTag } from '@/utils/content.config'
import { getAllTags, getPostsByTag } from '@/utils/content'
import { generateMultiLangTagPaths } from '@/utils/i18n'
export async function getStaticPaths() {
const tags = await getAllTags()
return themeConfig.global.moreLocale.flatMap(lang =>
tags.map(tag => ({
params: { lang, tags: tag },
props: { tags: tag },
})),
)
return generateMultiLangTagPaths(tags)
}
const { lang } = Astro.params

View file

@ -1,12 +1,10 @@
---
import { themeConfig } from '@/config'
import Layout from '@/layouts/Layout.astro'
import { getAllTags } from '@/utils/content.config'
import { getAllTags } from '@/utils/content'
import { generateLanguagePaths } from '@/utils/i18n'
export function getStaticPaths() {
return themeConfig.global.moreLocale.map(lang => ({
params: { lang },
}))
return generateLanguagePaths()
}
const { lang } = Astro.params
@ -21,3 +19,4 @@ const allTags = await getAllTags()
</a>
))}
</div>
</Layout>