+ {frontmatter.pubDate.toString().slice(0,10)}
+ {frontmatter.description}
+ Written by: {frontmatter.author}
+
+ ...
+ ```
+
+### 更新 RSS 函数
+
+14. 最后,教程博客项目包括一个 RSS 提要。该函数必须使用 `getCollection()` 来返回博客文章中的信息。然后,你可以使用返回的 `data` 对象生成 RSS 项。
+
+ ```js title="src/pages/rss.xml.js" del={2,11} ins={3,6,12-17}
+ import rss from '@astrojs/rss';
+ import { pagesGlobToRssItems } from '@astrojs/rss';
+ import { getCollection } from 'astro:content';
+
+ export async function GET(context) {
+ const posts = await getCollection("posts");
+ return rss({
+ title: 'Astro Learner | Blog',
+ description: 'My journey learning Astro',
+ site: context.site,
+ items: await pagesGlobToRssItems(import.meta.glob('./**/*.md')),
+ items: posts.map((post) => ({
+ title: post.data.title,
+ pubDate: post.data.pubDate,
+ description: post.data.description,
+ link: `/posts/${post.slug}/`,
+ })),
+ customData: `en-us`,
+ })
+ }
+ ```
+
+要查看使用内容集合的博客教程的完整示例,请访问教程存储库的 [Content Collections 分支](https://github.com/withastro/blog-tutorial-demo/tree/content-collections)。
From ddd48f011b0693692c67730cc1c50c9e8ff1d818 Mon Sep 17 00:00:00 2001
From: Flame-Y <94735231+Flame-Y@users.noreply.github.com>
Date: Thu, 26 Oct 2023 15:09:04 +0800
Subject: [PATCH 2/3] Update add-content-collections.mdx
---
.../docs/zh-cn/tutorials/add-content-collections.mdx | 12 ++++++------
1 file changed, 6 insertions(+), 6 deletions(-)
diff --git a/src/content/docs/zh-cn/tutorials/add-content-collections.mdx b/src/content/docs/zh-cn/tutorials/add-content-collections.mdx
index aa096b6d167fd..7fce963b91775 100644
--- a/src/content/docs/zh-cn/tutorials/add-content-collections.mdx
+++ b/src/content/docs/zh-cn/tutorials/add-content-collections.mdx
@@ -17,9 +17,9 @@ import Option from '~/components/tutorial/Option.astro';
- 使用 `getCollection()` 获取博客文章内容和元数据
-## Prerequisites
+## 先决条件
-你需要准备一个现成的 Astro 项目,里面存在包含 Markdown 或者 MDX 类型文件的 `src/pages/` 文件夹.
+你需要准备一个现成的 Astro 项目,里面存在包含 Markdown 或者 MDX 类型文件的 `src/pages/` 文件夹。
本教程使用先前[搭建博客教程完成后的项目代码](https://github.com/withastro/blog-tutorial-demo)来演示如何将博客转化为内容集合。你可以在本地克隆并使用该代码库,或者通过于 [StackBlitz 上编辑教程的代码](https://stackblitz.com/github/withastro/blog-tutorial-demo/tree/complete?file=src%2Fpages%2Findex.astro)在浏览器中完成该教程。
@@ -34,11 +34,11 @@ import Option from '~/components/tutorial/Option.astro';
为了在 `https://example.com/posts/post-1/` 创建第一篇博客文章, 你创建了 `/posts/` 文件夹并添加了 `post-1.md` 文件。然后,每当你想要添加新的博客文章到网站时,都要向该文件夹添加一个新的 Markdown 文件。
-## 页面(Pages) vs 集合(Collections)
+## 页面(Pages)vs 集合(Collections)
即使使用内容集合, 你仍然可以使用 `src/pages/` 文件夹来存放独立的页面, 比如关于页面。但是, 将你的博客文章迁移到 `src/content/` 文件夹将允许你使用更强大和性能更好的 API 来生成你的博客文章索引和展示你的个人博客文章。
-同时, 你将在代码编辑器中获得更好的引导和自动完成功能,因为你将会有一个**[模式](/zh-cn/guides/content-collections/#定义集合模式)** 来为每篇文章定义一个通用的结构,Astro将帮助你执行该结构。 In your schema, you can specify when frontmatter properties are required, such as a description or an author, and which data type each property must be, such as a string or an array. This leads to catching many mistakes sooner, with descriptive error messages telling you exactly what the problem is.
+同时, 你将在代码编辑器中获得更好的引导和自动完成功能,因为你将会有一个**[模式](/zh-cn/guides/content-collections/#定义集合模式)** 来为每篇文章定义一个通用的结构,Astro将帮助你执行该结构。在模式中,你可以指定什么时候需要 fronmatter 属性(例如说明或作者),以及每个属性必须是哪种数据类型(例如字符串或数组)。这样做可以更早地发现许多错误,并提供详细的错误信息,准确地告诉你问题所在。
在我们的指南中阅读更多关于 [Astro 的内容集合](/zh-cn/guides/content-collections/) 的信息,或者按照下面的说明将基础博客从 `src/pages/posts/` 转换为 `src/content/posts/` 来开始操作。
@@ -249,7 +249,7 @@ import Option from '~/components/tutorial/Option.astro';
---
```
-11. 你还需要更新对每个 `post` 返回值的引用。现在,你能够在每个对象的 `data` 属性中获得 frontmatter 值。此外,当使用集合时,每个 `post` 对象将有一个 `slug` 页面,而不是完整的 URL.
+11. 你还需要更新对每个 `post` 返回值的引用。现在,你能够在每个对象的 `data` 属性中获得 frontmatter 值。此外,当使用集合时,每个 `post` 对象将有一个 `slug` 页面,而不是完整的 URL。
```astro title="src/pages/blog.astro" "data" "/posts/$\{post.slug\}/" del={14} ins={15}
---
@@ -322,7 +322,7 @@ import Option from '~/components/tutorial/Option.astro';
按照[上述相同的步骤](#使用-getcollection-替换-astroglob)导入并使用 `getCollection` 来获取在博客文章中使用过的标签以展示在 `src/pages/tags/index.astro` 页面。
- Show me the code.
+ 给我看看代码!
```astro title="src/pages/tags/index.astro" "post.data" "getCollection(\"posts\")" ins={2}
---
import { getCollection } from "astro:content";
From ca865dd4724955ba294546a83029bcdaca3b2acf Mon Sep 17 00:00:00 2001
From: Flame-Y <94735231+Flame-Y@users.noreply.github.com>
Date: Thu, 26 Oct 2023 15:25:17 +0800
Subject: [PATCH 3/3] Update add-content-collections.mdx
fix some format problem
---
.../docs/zh-cn/tutorials/add-content-collections.mdx | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/src/content/docs/zh-cn/tutorials/add-content-collections.mdx b/src/content/docs/zh-cn/tutorials/add-content-collections.mdx
index 7fce963b91775..3f7f6ac72f76b 100644
--- a/src/content/docs/zh-cn/tutorials/add-content-collections.mdx
+++ b/src/content/docs/zh-cn/tutorials/add-content-collections.mdx
@@ -38,7 +38,7 @@ import Option from '~/components/tutorial/Option.astro';
即使使用内容集合, 你仍然可以使用 `src/pages/` 文件夹来存放独立的页面, 比如关于页面。但是, 将你的博客文章迁移到 `src/content/` 文件夹将允许你使用更强大和性能更好的 API 来生成你的博客文章索引和展示你的个人博客文章。
-同时, 你将在代码编辑器中获得更好的引导和自动完成功能,因为你将会有一个**[模式](/zh-cn/guides/content-collections/#定义集合模式)** 来为每篇文章定义一个通用的结构,Astro将帮助你执行该结构。在模式中,你可以指定什么时候需要 fronmatter 属性(例如说明或作者),以及每个属性必须是哪种数据类型(例如字符串或数组)。这样做可以更早地发现许多错误,并提供详细的错误信息,准确地告诉你问题所在。
+同时, 你将在代码编辑器中获得更好的引导和自动完成功能,因为你将会有一个[模式](/zh-cn/guides/content-collections/#定义集合模式)来为每篇文章定义一个通用的结构,Astro将帮助你执行该结构。在模式中,你可以指定什么时候需要 fronmatter 属性(例如说明或作者),以及每个属性必须是哪种数据类型(例如字符串或数组)。这样做可以更早地发现许多错误,并提供详细的错误信息,准确地告诉你问题所在。
在我们的指南中阅读更多关于 [Astro 的内容集合](/zh-cn/guides/content-collections/) 的信息,或者按照下面的说明将基础博客从 `src/pages/posts/` 转换为 `src/content/posts/` 来开始操作。
@@ -130,7 +130,7 @@ import Option from '~/components/tutorial/Option.astro';
如果你正在使用自己的项目,请确保你已更新所有已安装的依赖项。搭建博客教程的示例代码库仅使用了 Preact 集成。
:::
-2. 博客教程使用 `base` (最不严格的) TypeScript 设置。为了使用内容集合, 你必须 [设置 TypeScript](/zh-cn/guides/content-collections/#设置-typescript) 使用 `strict` 或 `strictest` 的设置, **或者** 在 `tsconfig.json` 添加两个选项。
+2. 博客教程使用 `base` (最不严格的) TypeScript 设置。为了使用内容集合,你必须 [设置 TypeScript](/zh-cn/guides/content-collections/#设置-typescript) 使用 `strict` 或 `strictest` 的设置,**或者** 在 `tsconfig.json` 添加两个选项。
为了在博客教程示例的其余部分使用内容集合而不编写 TypeScript,请在配置文件中添加以下两个 TypeScript 配置选项:
@@ -179,9 +179,9 @@ import Option from '~/components/tutorial/Option.astro';
### 从集合中生成页面
-6. 创建一个名为 `src/pages/posts/[...slug].astro` 的页面文件。 当Markdown和MDX文件位于集合中时,它们不再使用Astro基于文件的路由自动成为页面,因此必须创建一个用来生成每个独立的博客文章的页面。
+6. 创建一个名为 `src/pages/posts/[...slug].astro` 的页面文件。当Markdown和MDX文件位于集合中时,它们不再使用Astro基于文件的路由自动成为页面,因此必须创建一个用来生成每个独立的博客文章的页面。
-7. 添加以下代码到 [查询集合](/zh-cn/guides/content-collections/#查询集合) 中,以使每篇博客文章的 slug 和页面内容在生成的每个页面中都可用:
+7. 添加以下代码到[查询集合](/zh-cn/guides/content-collections/#查询集合)中,以使每篇博客文章的 slug 和页面内容在生成的每个页面中都可用:
```astro title="src/pages/posts/[...slug].astro"
---