添加 RSS 摘要
Astro 支持为博客和其他内容网站快速,自动的 RSS 提要生成。RSS 源为用户提供了一种订阅内容的简单方法。
设置 @astrojs/rss
段落标题 设置 @astrojs/rss@astrojs/rss
包 提供了使用 API 端点 生成 RSS 摘要的帮助程序。这使得静态构建和使用 SSR 适配器 时的按需生成都成为可能。
-
使用你喜欢的包管理器安装
@astrojs/rss
:Terminal window npm install @astrojs/rssTerminal window pnpm install @astrojs/rssTerminal window yarn add @astrojs/rss确保已在项目的
astro.config
中配置了site
。这将用于生成指向你的 RSS 文章的链接。 -
在
src/pages/
中创建一个文件,其中包含你选择的名称和扩展名.xml.js
,以用作 Feed 的输出 URL。一些常见的 RSS 源 URL 名称是feed.xml
或rss.xml
。下面的示例文件
src/pages/rss.xml.js
将在site/rss.xml
上创建一个 RSS 提要。 -
将
rss()
帮助程序从@astrojs/rss
包导入到你的.xml.js
文件中,并导出一个使用以下参数返回它的函数:src/pages/rss.xml.js import rss from '@astrojs/rss';export function GET(context) {return rss({// 输出的 xml 中的`<title>`字段title: 'Buzz’s Blog',// 输出的 xml 中的`<description>`字段description: 'A humble Astronaut’s guide to the stars',// 从端点上下文获取项目“site”// https://docs.astro.build/zh-cn/reference/api-reference/#contextsitesite: context.site,// 输出的 xml 中的`<item>`数组// 有关使用内容集合和 glob 导入的示例,请参阅“生成`items`”部分items: [],// (可选) 注入自定义 xmlcustomData: `<language>en-us</language>`,});}
生成 items
段落标题 生成 itemsitems
字段接受 RSS 源对象列表,每个对象都有一个必需的 link
、title
和 pubDate
。还可以包含三个可选值 description
(简短摘录)、content
(帖子的完整内容)和一个 customData
字段,用于包含任何额外数据,例如博客文章中的其他 frontmatter 属性。
你可以从内容集合架构生成此数组,也可以使用 glob imports 为位于 src/pages/
中的博客文章生成此数组。
使用内容集合
段落标题 使用内容集合要创建内容集合中管理的页面的 RSS 摘要,你可以使用 getCollection()
函数来检索项目列表。
import rss from '@astrojs/rss';import { getCollection } from 'astro:content';
export async function GET(context) { const blog = await getCollection('blog'); return rss({ title: 'Buzz’s Blog', description: 'A humble Astronaut’s guide to the stars', site: context.site, items: blog.map((post) => ({ title: post.data.title, pubDate: post.data.pubDate, description: post.data.description, customData: post.data.customData, // 从 `slug` 属性计算出 RSS 链接 // 这个例子假设所有的文章都被渲染为 `/blog/[slug]` 路由 link: `/blog/${post.slug}/`, })), });}
可选:替换现有的博客集合架构以强制实施预期的 RSS 属性。
若要确保每个博客条目生成有效的 RSS 源项,可以选择导入并应用 rssSchema
,而不是定义架构的每个单独属性。
import { defineCollection } from 'astro:content';import { rssSchema } from '@astrojs/rss';
const blog = defineCollection({ schema: rssSchema,});
export const collections = { blog };
使用 glob 导入
段落标题 使用 glob 导入
添加于:
@astrojs/rss@2.1.0
要从 src/pages/
中的文档创建 RSS 摘要,请使用 pagesGlobToRssItems()
帮助程序。它接受一个 import.meta.glob
结果并输出一个有效 RSS 摘要项数组(请参阅有关编写 glob 模式的更多信息 以指定要包含哪些页面)。
此函数假定但不验证每个文档的 frontmatter 中都存在所有必需的 feed 属性。如果遇到错误,请手动验证每个页面的 frontmatter。
import rss, { pagesGlobToRssItems } from '@astrojs/rss';
export async function GET(context) { return rss({ title: 'Buzz’s Blog', description: 'A humble Astronaut’s guide to the stars', site: context.site, items: await pagesGlobToRssItems( import.meta.glob('./blog/*.{md,mdx}'), ), });}
在 @astrojs/rss
的旧版本中,直接将 glob 结果传递给 items
,而不是使用 pagesGlobToRssItems()
包装器:
items: import.meta.glob('./blog/*.{md,mdx}'),
包含完整的文章内容
段落标题 包含完整的文章内容
添加于:
astro@1.6.14
content
键包含文章的完整内容作为 HTML。这允许你将整个文章内容提供给 RSS 订阅阅读器。
无论何时在 XML 中使用 HTML 内容,我们建议使用像 sanitize-html
这样的包,以确保你的内容被正确地清理、转义和编码。
当使用内容集合时,使用标准的 Markdown 解析器如 markdown-it
渲染文章 body
并清理结果:
import rss from '@astrojs/rss';import { getCollection } from 'astro:content';import sanitizeHtml from 'sanitize-html';import MarkdownIt from 'markdown-it';const parser = new MarkdownIt();
export async function GET(context) { const blog = await getCollection('blog'); return rss({ title: 'Buzz’s Blog', description: 'A humble Astronaut’s guide to the stars', site: context.site, items: blog.map((post) => ({ link: `/blog/${post.slug}/`, // 注意:这不会处理 MDX 文件中的组件或 JSX 表达式。 content: sanitizeHtml(parser.render(post.body)), ...post.data, })), });}
当使用 glob 导入 Markdown 时,我们建议使用 compiledContent()
辅助函数来检索要进行清理的渲染 HTML。注意:此功能不支持 MDX 文件。
import rss from '@astrojs/rss';import sanitizeHtml from 'sanitize-html';
export function GET(context) { const postImportResult = import.meta.glob('../posts/**/*.md', { eager: true }); const posts = Object.values(postImportResult); return rss({ title: 'Buzz’s Blog', description: 'A humble Astronaut’s guide to the stars', site: context.site, items: posts.map((post) => ({ link: post.url, content: sanitizeHtml(post.compiledContent()), ...post.frontmatter, })), });}
添加样式表
段落标题 添加样式表你可以为 RSS 摘要添加样式表,以便在浏览器中查看文件时获得更好的用户体验。
使用 rss
函数的 stylesheet
选项指定到样式表的绝对路径。
rss({ // 例如,使用 "public/rss/styles.xsl" 中的样式表 stylesheet: '/rss/styles.xsl', // ...});
如果你还没有 RSS 样式表的想法,我们建议你使用 Pretty Feed v3 默认样式表,你可以从 GitHub 上下载并将其保存到项目的 public/
目录中。
后续步骤
段落标题 后续步骤在浏览器中的 your-domain.com/rss.xml
访问你的提要并确认你可以看到每个文章的数据后,你现在可以在你的网站上推广你的提要。将标准 RSS 图标添加到你的网站可以让你的读者知道他们可以在自己的提要阅读器中订阅你的文章。