From 8a3e01fb969526f411a99ed63ff42b1e73852333 Mon Sep 17 00:00:00 2001 From: radishzzz Date: Sat, 18 Jan 2025 03:18:19 +0000 Subject: [PATCH] refactor: update RSS feed generation --- astro.config.ts | 4 --- src/content/posts/rashomon.md | 2 -- src/pages/[lang]/rss.xml.ts | 68 ++++++++++++++++++----------------- src/pages/rss.xml.ts | 49 +++++++++++++------------ 4 files changed, 59 insertions(+), 64 deletions(-) diff --git a/astro.config.ts b/astro.config.ts index dcd7bde..981a056 100644 --- a/astro.config.ts +++ b/astro.config.ts @@ -36,16 +36,12 @@ const { locale }: { locale: ThemeConfig['global']['locale'] } = themeConfig.glob export default defineConfig({ site: url, base: '/', - trailingSlash: 'always', i18n: { locales: Object.entries(langMap).map(([path, codes]) => ({ path, codes, })), defaultLocale: locale, - routing: { - prefixDefaultLocale: false, - }, }, integrations: [ partytown({ diff --git a/src/content/posts/rashomon.md b/src/content/posts/rashomon.md index 06199a6..2259a78 100644 --- a/src/content/posts/rashomon.md +++ b/src/content/posts/rashomon.md @@ -4,8 +4,6 @@ published: 1984-09-17 tags: ["黑泽明"] --- -这里是文章内容... - 一天,天色已晚。 仆人纪宁正在罗生门下等待雨停。 除了他,宽大的门下空无一人。 只有一只蟋蟀栖息在一根大柱子上,柱子上有的地方已经脱漆。 罗生门位于铃作大寺街上,除了这个人之外,应该还有两三个人,包括避雨的一笠和搓着乌鸦帽的真江吉。 但是,除了这个人之外,一个人也没有。 diff --git a/src/pages/[lang]/rss.xml.ts b/src/pages/[lang]/rss.xml.ts index 987b1f4..b6e511e 100644 --- a/src/pages/[lang]/rss.xml.ts +++ b/src/pages/[lang]/rss.xml.ts @@ -8,23 +8,25 @@ import sanitizeHtml from 'sanitize-html' const parser = new MarkdownIt() const { title, description, url } = themeConfig.site -const { locale: defaultLocale, moreLocale } = themeConfig.global +const { moreLocale } = themeConfig.global const followConfig = themeConfig.seo?.follow -// Extract first 100 chars from content as description +// Returns first 200 chars with proper truncation function getExcerpt(content: string): string { - const plainText = sanitizeHtml( - parser.render(content), - { - allowedTags: [], - allowedAttributes: {}, - }, - ) + if (!content) + return '' - return `${plainText.slice(0, 100).trim()}...` + // 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 response for invalid language routes +// Return 404 for invalid language paths function return404() { return new Response(null, { status: 404, @@ -32,21 +34,22 @@ function return404() { }) } -// Add getStaticPaths for dynamic routes +// Type for supported non-default languages +type SupportedLanguage = typeof moreLocale[number] + +// Generate static paths for all supported languages export function getStaticPaths() { - return moreLocale.map(lang => ({ params: { lang } })) + return moreLocale.map((lang: SupportedLanguage) => ({ params: { lang } })) } -// Generate RSS feed for non-default languages export async function GET({ params }: APIContext) { - const lang = params.lang as string + const lang = params.lang as SupportedLanguage - // Only generate RSS for valid non-default languages - if (!moreLocale.includes(lang)) { + // Return 404 if language is not supported + if (!moreLocale.includes(lang)) return return404() - } - // Get posts for specific language (include universal posts) + // Get posts for specific language (including universal posts) const posts = await getCollection( 'posts', ({ data }: { data: CollectionEntry<'posts'>['data'] }) => @@ -57,28 +60,27 @@ export async function GET({ params }: APIContext) { title: `${title} (${lang})`, description, site: url, - stylesheet: '/rss/styles.xsl', - // Map posts to RSS items with language-specific URLs - items: posts.map(post => ({ + items: posts.map((post: CollectionEntry<'posts'>) => ({ title: post.data.title, pubDate: post.data.published, description: post.data.description || getExcerpt(post.body), - link: `/${lang}/posts/${post.slug}/`, - content: sanitizeHtml( - parser.render(post.body), - { - allowedTags: sanitizeHtml.defaults.allowedTags.concat(['img']), - }, - ), + // Generate absolute URL with language prefix + link: new URL(`${lang}/posts/${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 language and follow challenge info + // Add XML namespaces for language and follow challenge customData: ` ${lang} ${followConfig?.feedID && followConfig?.userID ? ` - ${followConfig.feedID} - ${followConfig.userID} - ` + ${followConfig.feedID} + ${followConfig.userID} + ` : '' } `.trim(), diff --git a/src/pages/rss.xml.ts b/src/pages/rss.xml.ts index 6ee9c62..7cde56d 100644 --- a/src/pages/rss.xml.ts +++ b/src/pages/rss.xml.ts @@ -1,4 +1,3 @@ -import type { APIContext } from 'astro' import type { CollectionEntry } from 'astro:content' import themeConfig from '@/config' import rss from '@astrojs/rss' @@ -11,22 +10,23 @@ const { title, description, url } = themeConfig.site const { locale } = themeConfig.global const followConfig = themeConfig.seo?.follow -// Extract first 100 chars from content as description +// Returns first 200 chars with proper truncation function getExcerpt(content: string): string { - const plainText = sanitizeHtml( - parser.render(content), - { - allowedTags: [], - allowedAttributes: {}, - }, - ) + if (!content) + return '' - return `${plainText.slice(0, 100).trim()}...` + // 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 } -// Generate RSS feed for default language export async function GET() { - // Only handle posts for default language + // Get all posts for default language (including universal posts) const posts = await getCollection( 'posts', ({ data }: { data: CollectionEntry<'posts'>['data'] }) => @@ -37,28 +37,27 @@ export async function GET() { title, description, site: url, - stylesheet: '/rss/styles.xsl', - // Map posts to RSS items items: posts.map((post: CollectionEntry<'posts'>) => ({ title: post.data.title, pubDate: post.data.published, description: post.data.description || getExcerpt(post.body), - link: `/posts/${post.slug}/`, - content: sanitizeHtml( - parser.render(post.body), - { - allowedTags: sanitizeHtml.defaults.allowedTags.concat(['img']), - }, - ), + // Generate absolute URL for post + link: new URL(`posts/${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 language and follow challenge info + // Add XML namespaces for language and follow challenge customData: ` ${locale} ${followConfig?.feedID && followConfig?.userID ? ` - ${followConfig.feedID} - ${followConfig.userID} - ` + ${followConfig.feedID} + ${followConfig.userID} + ` : '' } `.trim(),