From 0dbec343b4d9942e5c4bdb1f69f6bf6275e25636 Mon Sep 17 00:00:00 2001 From: ccbikai Date: Wed, 1 Jan 2025 18:07:00 +0800 Subject: [PATCH] feat: add RSS feed beautification option Introduces a new RSS_BEAUTIFY configuration flag to enable RSS feed styling - Adds XSL styling support for RSS feeds when enabled - Updates documentation in both English and Chinese - Modifies RSS generation to support the new styling option Migration from rss to getRssString for enhanced customization --- .env.example | 3 ++- README.md | 3 +++ README.zh-cn.md | 3 +++ src/assets/rss.xsl | 1 + src/pages/rss.xml.js | 18 ++++++++++++++++-- 5 files changed, 25 insertions(+), 3 deletions(-) create mode 100644 src/assets/rss.xsl diff --git a/.env.example b/.env.example index 59205c9..e7a6e80 100644 --- a/.env.example +++ b/.env.example @@ -23,4 +23,5 @@ GOOGLE_SEARCH_SITE="" TAGS="" COMMENTS="" LINKS="" -NAVS="" \ No newline at end of file +NAVS="" +RSS_BEAUTIFY=false \ No newline at end of file diff --git a/README.md b/README.md index 0141a61..88f8e3b 100644 --- a/README.md +++ b/README.md @@ -133,6 +133,9 @@ LINKS=Title1,URL1;Title2,URL3;Title3,URL3; ## Sidebar Navigation Item, Separate using commas and semicolons NAVS=Title1,URL1;Title2,URL3;Title3,URL3; + +## Enable RSS beautify +RSS_BEAUTIFY=true ``` ## 🙋🏻 FAQs diff --git a/README.zh-cn.md b/README.zh-cn.md index dd79dcf..fcaac6e 100644 --- a/README.zh-cn.md +++ b/README.zh-cn.md @@ -131,6 +131,9 @@ LINKS=Title1,URL1;Title2,URL3;Title3,URL3; ## 侧边栏导航项, 使用英文逗号和分号分割 NAVS=Title1,URL1;Title2,URL3;Title3,URL3; + +## 启用 RSS 美化 +RSS_BEAUTIFY=true ``` ## 🙋🏻 常问问题 diff --git a/src/assets/rss.xsl b/src/assets/rss.xsl new file mode 100644 index 0000000..fce8129 --- /dev/null +++ b/src/assets/rss.xsl @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/src/pages/rss.xml.js b/src/pages/rss.xml.js index ec650eb..26baee0 100644 --- a/src/pages/rss.xml.js +++ b/src/pages/rss.xml.js @@ -1,6 +1,8 @@ -import rss from '@astrojs/rss' +import { getRssString } 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 @@ -15,7 +17,7 @@ export async function GET(Astro) { url.pathname = SITE_URL url.search = '' - return rss({ + let rssString = await getRssString({ title: `${tag ? `${tag} | ` : ''}${channel.title}`, description: channel.description, site: url.origin, @@ -39,4 +41,16 @@ export async function GET(Astro) { }), })), }) + + const enableBeautify = getEnv(import.meta.env, Astro, 'RSS_BEAUTIFY') + if (enableBeautify) { + rssString = rssString.replace(/^(<\?xml .*\?>)/i, style) + } + + return new Response(rssString, { + headers: { + 'Content-Type': 'text/xml', + 'Cache-Control': 'public, max-age=3600', + }, + }) }