可选的:制作内容集合
现在你有一个使用 Astro 的 内置基于文件的路由 的博客,你将更新它以使用 内容收集。内容集合是管理类似内容组(例如博客文章)的有效方法。
¥Now that you have a blog using Astro’s built-in file-based routing, you will update it to use a content collection. Content collections are a powerful way to manage groups of similar content, such as blog posts.
准备好……
-
将你的博客文章文件夹移至
src/blog/
-
创建一个架构来定义你的博客文章 frontmatter
-
使用
getCollection()
获取博客文章内容和元数据
学习:页面与集合
标题部分 学习:页面与集合¥Learn: Pages vs Collections
即使使用内容集合,你仍将使用 src/pages/
文件夹来表示各个页面,例如“关于我”页面。但是,将你的博客文章移出这个特殊文件夹将允许你使用更强大、性能更高的 API 来生成你的博客文章索引并显示你的个人博客文章。
¥Even when using content collections, you will still use the src/pages/
folder for individual pages, such as your About Me page. But, moving your blog posts outside of this special folder will allow you to use more powerful and performant APIs to generate your blog post index and display your individual blog posts.
同时,你将在代码编辑器中获得更好的指导和自动补齐功能,因为你将拥有一个 schema 来定义每个帖子的通用结构,Astro 将通过 佐德(TypeScript 的架构声明和验证库)帮助你执行该结构。在你的架构中,你可以指定何时需要 frontmatter 属性(例如描述或作者),以及每个属性必须是哪种数据类型(例如字符串或数组)。这可以更快地发现许多错误,并通过描述性错误消息准确告诉你问题是什么。
¥At the same time, you’ll receive better guidance and autocompletion in your code editor because you will have a schema to define a common structure for each post that Astro will help you enforce through Zod, a schema declaration and validation library for TypeScript. 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.
在我们的指南中阅读有关 Astro 的内容合集 的更多信息,或开始按照以下说明将基本博客从 src/pages/posts/
转换为 src/blog/
。
¥Read more about Astro’s content collections in our guide, or get started with the instructions below to convert a basic blog from src/pages/posts/
to src/blog/
.
测试你的知识
标题部分 测试你的知识¥Test your knowledge
- 你可能会在
src/pages/
中保留哪种类型的页面?
- 以下哪个不是将博客文章移动到内容集合的好处?
- 内容集合使用 TypeScript 。。。
以下步骤向你展示如何通过为博客文章创建内容集合来扩展构建博客教程的最终产品。
¥The steps below show you how to extend the final product of the Build a Blog tutorial by creating a content collection for the blog posts.
升级依赖
标题部分 升级依赖¥Upgrade dependencies
升级到 Astro 的最新版本,并通过在你的终端中运行以下命令将所有集成升级到最新版本:
¥Upgrade to the latest version of Astro, and upgrade all integrations to their latest versions by running the following commands in your terminal:
为你的帖子创建一个集合
标题部分 为你的帖子创建一个集合¥Create a collection for your posts
-
Create a new collection (folder) called
src/blog/
. -
Move all your existing blog posts (
.md
files) fromsrc/pages/posts/
into this new collection. -
Create a
src/content.config.ts
file to define a schema for yourpostsCollection
. For the existing blog tutorial code, add the following contents to the file to define all the frontmatter properties used in its blog posts: -
In order for Astro to recognize your schema, quit (
CTRL + C
) and restart the dev server to continue with the tutorial. This will define theastro:content
module.
从集合生成页面
标题部分 从集合生成页面¥Generate pages from a collection
-
Create a page file called
src/pages/posts/[...slug].astro
. Your Markdown and MDX files no longer automatically become pages using Astro’s file-based routing when they are inside a collection, so you must create a page responsible for generating each individual blog post. -
Add the following code to query your collection to make each blog post’s slug and page content available to each page it will generate:
-
Render your post
<Content />
within the layout for Markdown pages. This allows you to specify a common layout for all of your posts. -
Remove the
layout
definition in each individual post’s frontmatter. Your content is now wrapped in a layout when rendered, and this property is no longer needed.
将 import.meta.glob()
替换为 getCollection()
标题部分 将 import.meta.glob() 替换为 getCollection()¥Replace import.meta.glob()
with getCollection()
-
Anywhere you have a list of blog posts, like the tutorial’s Blog page (
src/pages/blog.astro/
), you will need to replaceimport.meta.glob()
withgetCollection()
as the way to fetch content and metadata from your Markdown files. -
You will also need to update references to the data returned for each
post
. You will now find your frontmatter values on thedata
property of each object. Also, when using collections eachpost
object will have a pageslug
, not a full URL. -
The tutorial blog project also dynamically generates a page for each tag using
src/pages/tags/[tag].astro
and displays a list of tags atsrc/pages/tags/index.astro
.Apply the same changes as above to these two files:
- fetch data about all your blog posts using
getCollection("blog")
instead of usingimport.meta.glob()
- access all frontmatter values using
data
instead offrontmatter
- create a page URL by adding the post’s
slug
to the/posts/
path
The page that generates individual tag pages now becomes:
Try it yourself - Update the query in the Tag Index page
标题部分 Try it yourself - Update the query in the Tag Index pageImport and use
getCollection
to fetch the tags used in the blog posts onsrc/pages/tags/index.astro
, following the same steps as above.Show me the code.
- fetch data about all your blog posts using
更新任何 frontmatter 值以匹配你的架构
标题部分 更新任何 frontmatter 值以匹配你的架构¥Update any frontmatter values to match your schema
如有必要,请更新整个项目(例如布局中)中与你的集合架构不匹配的任何前置值。
¥If necessary, update any frontmatter values throughout your project, such as in your layout, that do not match your collections schema.
在博客教程示例中,pubDate
是一个字符串。现在,根据定义帖子前言类型的模式,pubDate
将成为 Date
对象。你现在可以利用这一点,使用任何 Date
对象可用的方法来格式化日期。
¥In the blog tutorial example, pubDate
was a string. Now, according to the schema that defines types for the post frontmatter, pubDate
will be a Date
object. You can now take advantage of this to use the methods available for any Date
object to format the date.
要在博客文章布局中渲染日期,请使用 toLocaleDateString()
方法将其转换为字符串:
¥To render the date in the blog post layout, convert it to a string using toLocaleDateString()
method:
更新 RSS 功能
标题部分 更新 RSS 功能¥Update the RSS function
教程博客项目包含 RSS 提要。此函数还必须使用 getCollection()
返回博客文章中的信息。然后,你将使用返回的 data
对象生成 RSS 项目。
¥The tutorial blog project includes an RSS feed. This function must also use getCollection()
to return information from your blog posts. You will then generate the RSS items using the data
object returned.
有关使用内容集合的博客教程的完整示例,请参阅教程存储库的 内容馆藏分支。
¥For the full example of the blog tutorial using content collections, see the Content Collections branch of the tutorial repo.
Tutorials