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

38
src/utils/i18n.ts Normal file
View file

@ -0,0 +1,38 @@
import type { CollectionEntry } from 'astro:content'
import { themeConfig } from '@/config'
export function generateLanguagePaths() {
return themeConfig.global.moreLocale.map(lang => ({
params: { lang },
}))
}
export function generatePostPaths(posts: CollectionEntry<'posts'>[]) {
return posts.map(post => ({
params: {
slug: post.data.slug || post.slug,
},
props: { post },
}))
}
export function generateMultiLangPostPaths(posts: CollectionEntry<'posts'>[]) {
return themeConfig.global.moreLocale.flatMap(lang =>
posts.map(post => ({
params: {
lang,
slug: post.data.slug || post.slug,
},
props: { post },
})),
)
}
export function generateMultiLangTagPaths(tags: string[]) {
return themeConfig.global.moreLocale.flatMap(lang =>
tags.map(tag => ({
params: { lang, tags: tag },
props: { tags: tag },
})),
)
}

71
src/utils/rss.ts Normal file
View file

@ -0,0 +1,71 @@
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'
const parser = new MarkdownIt()
const { title, description, url } = themeConfig.site
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
}
interface GenerateRSSOptions {
lang?: string
}
export async function generateRSS({ lang }: GenerateRSSOptions = {}) {
// 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: lang ? `${title} (${lang})` : title,
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 optional language prefix
link: new URL(
`${lang ? `${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 || themeConfig.global.locale}</language>
${followConfig?.feedID && followConfig?.userID
? `<follow_challenge>
<feedId>${followConfig.feedID}</feedId>
<userId>${followConfig.userID}</userId>
</follow_challenge>`
: ''
}
`.trim(),
})
}