From 876ba3e9a2907508be820a71f2adc07f877f7e42 Mon Sep 17 00:00:00 2001 From: ccbikai Date: Wed, 1 Jan 2025 19:03:22 +0800 Subject: [PATCH] refactor: optimize RSS feed generation Switches from getRssString to modern rss helper function Moves XSL stylesheet to dedicated static file Improves code organization and maintainability Simplifies RSS feed configuration while maintaining beautification option --- public/rss.xsl | 37 +++++++++++++++++++++++++++++++++++++ src/assets/rss.xsl | 1 - src/pages/rss.xml.js | 19 ++++++------------- 3 files changed, 43 insertions(+), 14 deletions(-) create mode 100644 public/rss.xsl delete mode 100644 src/assets/rss.xsl diff --git a/public/rss.xsl b/public/rss.xsl new file mode 100644 index 0000000..3c8becf --- /dev/null +++ b/public/rss.xsl @@ -0,0 +1,37 @@ +<xsl:value-of select="$title"/>

+ This RSS feed for the + + website. +



\ No newline at end of file diff --git a/src/assets/rss.xsl b/src/assets/rss.xsl deleted file mode 100644 index fce8129..0000000 --- a/src/assets/rss.xsl +++ /dev/null @@ -1 +0,0 @@ - \ No newline at end of file diff --git a/src/pages/rss.xml.js b/src/pages/rss.xml.js index 26baee0..8c6bee1 100644 --- a/src/pages/rss.xml.js +++ b/src/pages/rss.xml.js @@ -1,8 +1,7 @@ -import { getRssString } from '@astrojs/rss' +import rss from '@astrojs/rss' import sanitizeHtml from 'sanitize-html' import { getChannelInfo } from '../lib/telegram' import { getEnv } from '../lib/env' -import style from '../assets/rss.xsl?raw' export async function GET(Astro) { const { SITE_URL } = Astro.locals @@ -17,11 +16,12 @@ export async function GET(Astro) { url.pathname = SITE_URL url.search = '' - let rssString = await getRssString({ + const response = await rss({ title: `${tag ? `${tag} | ` : ''}${channel.title}`, description: channel.description, site: url.origin, trailingSlash: false, + stylesheet: getEnv(import.meta.env, Astro, 'RSS_BEAUTIFY') ? '/rss.xsl' : undefined, items: posts.map(item => ({ link: `posts/${item.id}`, title: item.title, @@ -42,15 +42,8 @@ export async function GET(Astro) { })), }) - const enableBeautify = getEnv(import.meta.env, Astro, 'RSS_BEAUTIFY') - if (enableBeautify) { - rssString = rssString.replace(/^(<\?xml .*\?>)/i, style) - } + response.headers.set('Content-Type', 'text/xml') + response.headers.set('Cache-Control', 'public, max-age=3600') - return new Response(rssString, { - headers: { - 'Content-Type': 'text/xml', - 'Cache-Control': 'public, max-age=3600', - }, - }) + return response }