自从 Astro 内容层 API 的实验性支持被添加以来,我们已经看到社区为各种类型的内容构建了多个加载器。我们已经重点介绍了一些已构建加载器的 CMS。在这篇文章中,我们将介绍其他类型的加载器,以展示内容层的多样化用例。
我们在这里将要介绍的加载器有:
Notion
Notion 是一个用于各类文档的协作平台。虽然它通常不被认为是一个 CMS,但很多人确实把它当作 CMS 来使用。
使用 notion-astro-loader,你可以将 Notion 文档同步到你项目的一个集合中。该加载器会根据 Notion 数据库的结构自动推断出 schema。
安装 notion-astro-loader
并进行如下配置:
import { defineCollection } from "astro:content";import { notionLoader } from "notion-astro-loader";
const database = defineCollection({ loader: notionLoader({ auth: import.meta.env.NOTION_TOKEN, database_id: import.meta.env.NOTION_DATABASE_ID, filter: { property: "Hidden", checkbox: { equals: false }, }, }),});
export const collections = { database };
Feed 加载器
Astro feed 加载器允许你加载 RSS、RDF 和 Atom feed,并在你的 Astro 网站中使用这些数据。由于 RSS 是一种广泛使用的发布格式,因此它有很多用例。例如,你可以使用 RSS 在你的网站上显示 GitHub 项目的最近发布版本。
要使用该加载器,请安装 @ascorbic/feed-loader
包,并定义一个指向 feed URL 的集合,如下所示:
import { defineCollection } from "astro:content";import { feedLoader } from "@ascorbic/feed-loader";
const releases = defineCollection({ loader: feedLoader({ url: "https://github.com/withastro/astro/releases.atom", }),})
const podcasts = defineCollection({ loader: feedLoader({ url: "https://feeds.99percentinvisible.org/99percentinvisible", }),})
export const collections = { releases, podcasts }
Stripe
使用 Stripe 加载器,你可以从 Stripe API 中拉取产品和价格信息。
要使用它,你需要 stripe-astro-loader
和 stripe
包。使用你的 API 密钥创建一个 Stripe
实例,然后将其传递给加载器,就完成了。
import { defineCollection } from "astro:content";import { stripePriceLoader, stripeProductLoader } from "stripe-astro-loader";import Stripe from "stripe";
const stripe = new Stripe("<API_KEY>");
const products = defineCollection({ loader: stripeProductLoader(stripe),})
const prices = defineCollection({ loader: stripePriceLoader(stripe),})
export const collections = { products, prices }
Airtable
Airtable 是一种组织数据和构建无代码应用的方式。使用 Airtable 加载器,你可以从你的 Airtable base 加载记录,并在 Astro 项目中使用它们。
Airtable 加载器可通过 @ascorbic/airtable-loader
包获得。通过环境变量提供你的 API 密钥,并使用加载器加载一个表。
import { defineCollection } from "astro:content";import { airtableLoader } from "@ascorbic/airtable-loader";
const launches = defineCollection({ loader: airtableLoader({ base: import.meta.env.AIRTABLE_BASE, table: "Product Launches", }),})
export const collections = { launches }
Strapi
Strapi 是一个开源的无头 CMS,可以自托管或使用 Strapi Cloud。使用 Strapi,你可以创建集合和自定义内容类型。
由社区新开发的 Strapi 加载器可以让你将内容拉取到本地集合中。要使用它,请从 npm 安装 strapi-community-astro-loader
。在你的内容集合配置(src/content/config.ts
)中,创建一个集合,并使用你想要同步的 contentType
来调用加载器。在这个例子中,我们正在抓取所有的远程文章,并将它们同步到一个名为 posts
的集合中。
import { defineCollection } from "astro:content";import { strapiLoader } from "strapi-community-astro-loader";
const posts = defineCollection({ loader: strapiLoader({ contentType: "article" }),})
export const collections = { posts }
另请参阅 Strapi 的博客文章《如何使用内容层 API 为 Strapi 创建一个自定义 Astro 加载器》。