Feed

为你的站点自动生成 RSS 或 JSON Feed

配置参数

output string string[]

The output filenames

Default:
"/feed.rss"
query string

The query to search the pages

Default:
""
sort string

The sort order

Default:
"date=desc"
limit number

The maximum number of items

Default:
10
info object

The feed info

title string

The feed title

Default:
"My RSS Feed"
subtitle string

The feed subtitle

published object

The feed published date

Default:
new Date()
description string

The feed description

Default:
""
lang string

The feed language

Default:
"en"
generator string boolean

The feed generator. Set true to generate automatically

Default:
true
authorName string

The feed author name

authorUrl string

The feed author URL

items object

The feed items configuration

title string function

The item title

Default:
"=title"
description string function

The item description

Default:
"=description"
published string function

The item published date

Default:
"=date"
updated string function

The item updated date

content string function

The item content

Default:
"=children"
lang string function

The item language

Default:
"=lang"
image string function

The item image

authorName string

The item author name

authorUrl string

The item author URL

描述

这个插件可以为你的站点自动生成 RSS 或 JSON Feed,支持任何页面集合 (例如文章、新闻、事件等)。

安装

在你的 _config.ts 文件中导入这个插件来使用它:

import lume from "lume/mod.ts";
import feed from "lume/plugins/feed.ts";

const site = lume();

site.use(feed({
  output: ["/posts.rss", "/posts.json"],
  query: "type=post",
  info: {
    title: "=site.title",
    description: "=site.description",
  },
  items: {
    title: "=title",
    description: "=excerpt",
  },
}));

export default site;

配置

在内部,这个插件使用 Search 来搜索并返回页面,因此你必须提供一个 query 来搜索页面,并可选择性地提供 sort 和 limit 参数。

你还需要配置 feed 的通用数据 (在 info 键中) 和 item 的信息 (在 items 键中)。 这是一个包含所有可用选项的示例:

site.use(feed({
  output: ["/posts.rss", "/posts.json"], // 需要生成的文件
  query: "type=post", // 仅选择 type=post 的页面
  sort: "date=desc", // 按日期降序排序
  limit: 10, // 仅显示前 10 个结果
  info: {
    title: "我的博客", // Feed 标题
    description: "我放置想法的地方", // Feed 副标题
    published: new Date(), // 发布日期
    lang: "en", // Feed 的语言
    generator: true, // 设置为 `true` 以自动生成 "Lume {version}"
    authorName: "Óscar Otero", // 站点的作者
    authorUrl: "https://oscarotero.com", // 作者的 URL
  },
  items: {
    title: "=title", // 每个 item 的标题
    description: "=excerpt", // 每个 item 的描述
    published: "=date", // 每个 item 的发布日期
    updated: undefined, // 每个 item 的最后更新
    content: "=children", // 每个 item 的内容
    lang: "=lang", // 每个 item 的语言
    image: "=cover", // item 的图片
    authorName: "=author.name", // 文章的作者
    authorUrl: "=author.url", // 作者的 URL
  },
}));

infoitems 选项使用与 metas 插件 相同的别名: 任何以 = 开头的值都表示一个变量名,该变量名将用于提取此信息。 例如,item 的描述的值为 =excerpt,这意味着每个 item 将使用变量 excerpt 的值作为描述。

也可以使用 CSS 选择器提取信息。 例如,假设我们要生成一个 RSS,其内容与 div .post-content 相同。 我们只需要用 $ 开头代码的值:

site.use(feed({
  // 通用配置
  info: {
    // info 配置
  },
  items: {
    title: "=title",
    description: "=excerpt",
    published: "=date",
    content: "$.post-content", // 使用 .post-content 元素的内容
    lang: "=lang",
  },
}));

从 Lume 2.4 开始,可以为每个值定义回退。 例如: =title || $h1 || Default title 将尝试从 title 变量中获取值,如果不存在,则从 h1 CSS 选择器中获取值,如果仍然不存在,则使用 Default title 值。

如果你想创建多个 feed,只需每个 feed 使用一次插件即可:

site.use(feed({
  output: "/posts.rss",
  // 文章 feed 配置
}));

site.use(feed({
  output: "/articles.rss",
  // 新闻 feed 配置
}));