refactor: optimize rss generation logic

This commit is contained in:
radishzzz 2025-03-17 14:01:51 +00:00
parent f34b0cb46b
commit 0888b59c5f
7 changed files with 128 additions and 155 deletions

View file

@ -5,7 +5,7 @@ import sanitizeHtml from 'sanitize-html'
const parser = new MarkdownIt()
type ExcerptScene = 'list' | 'meta' | 'og'
type ExcerptScene = 'list' | 'meta' | 'og' | 'rss'
// Excerpt length in different scenarios
const EXCERPT_LENGTHS: Record<ExcerptScene, {
@ -24,6 +24,10 @@ const EXCERPT_LENGTHS: Record<ExcerptScene, {
cjk: 75,
other: 150,
},
rss: {
cjk: 120,
other: 240,
},
}
const isCJKLang = (lang: string) => ['zh', 'zh-tw', 'ja'].includes(lang)

View file

@ -1,5 +1,6 @@
import type { CollectionEntry } from 'astro:content'
import themeConfig, { defaultLocale } from '@/config'
import { defaultLocale, themeConfig } from '@/config'
import { generateDescription } from '@/utils/description'
import rss from '@astrojs/rss'
import { getCollection } from 'astro:content'
import MarkdownIt from 'markdown-it'
@ -9,19 +10,6 @@ const parser = new MarkdownIt()
const { title, description, url } = themeConfig.site
const followConfig = themeConfig.seo?.follow
// Returns first 98 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, 98).trim()
return excerpt.length === 98 ? `${excerpt}...` : excerpt
}
interface GenerateRSSOptions {
lang?: string
}
@ -36,34 +24,34 @@ export async function generateRSS({ lang }: GenerateRSSOptions = {}) {
return rss({
title: lang ? `${title}_${lang}` : title,
description,
site: url,
description,
customData: `
<copyright>Copyright © ${new Date().getFullYear()} ${themeConfig.site.author}</copyright>
<language>${lang || themeConfig.global.locale}</language>
<lastBuildDate>${new Date().toUTCString()}</lastBuildDate>
${followConfig?.feedID && followConfig?.userID
? `<follow_challenge>
<feedId>${followConfig.feedID}</feedId>
<userId>${followConfig.userID}</userId>
</follow_challenge>`
: ''
}
`.trim(),
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 correct language prefix based on post language
// Generate URL with language prefix and abbrlink/slug
link: new URL(
`${post.data.lang !== defaultLocale && post.data.lang !== '' ? `${post.data.lang}/` : ''}posts/${post.data.abbrlink || post.slug}/`,
url,
).toString(),
// Convert markdown content to HTML, allowing img tags
description: generateDescription(post, 'rss'),
pubDate: post.data.published,
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(),
})
}