diff --git a/src/content/docs/zh-cn/reference/modules/astro-content.mdx b/src/content/docs/zh-cn/reference/modules/astro-content.mdx index d8a385569c779..8e7205f8f2d87 100644 --- a/src/content/docs/zh-cn/reference/modules/astro-content.mdx +++ b/src/content/docs/zh-cn/reference/modules/astro-content.mdx @@ -67,7 +67,7 @@ export const collections = { blog }; `loader` 是一个对象或函数,允许你从任何源加载数据到内容集合中,无论是本地还是远程数据。 -更多有关示例请 [参考 `内容集合`指南](/zh-cn/guides/content-collections/#定义集合-loader)。 +更多有关示例请 [参考`内容集合`指南](/zh-cn/guides/content-collections/#定义集合-loader)。 #### `schema` @@ -79,7 +79,7 @@ export const collections = { blog }; `schema` 是一个可选的 Zod 对象,用于配置集合的文档 frontmatter 的类型和形状。每个值必须使用 [Zod 验证器](https://github.com/colinhacks/zod) -更多有关示例请 [参考 `内容集合`指南](/zh-cn/guides/content-collections/#定义集合模式schema)。 +更多有关示例请 [参考`内容集合`指南](/zh-cn/guides/content-collections/#定义集合模式schema)。 ### `reference()` @@ -89,9 +89,9 @@ export const collections = { blog };

-在内容配置中使用 `reference()` 函数来定义从一个集合到另一个集合的关系或 "引用"。该函数接受一个集合名称,并在将引用转换为包含集合名称和引用 id 的对象之前,验证内容 `frontmatter` 或数据文件中指定的条目标识符。 +内容配置中的 `reference()` 函数,用于定义一个集合与另一个集合之间的关联,或者说“引用”。该函数接受一个集合名称,并将其转换为一个包含集合名称与引用 id 的对象。 -此示例定义了从博客作者到 "作者 "集合的引用,以及到同一 "博客 "集合的相关文章数组的引用: +此示例定义了从博客作者到 `authors` 集合的引用,以及到同一 `blog` 集合的相关文章数组的引用: ```ts import { defineCollection, reference, z } from 'astro:content'; @@ -100,9 +100,9 @@ import { glob, file } from 'astro/loaders'; const blog = defineCollection({ loader: glob({ pattern: '**/*.md', base: './src/data/blog' }), schema: z.object({ - // 通过 "id "从 "作者 "集合中引用单个作者 + // 通过 `id` 从 `authors` 集合中,引用单个作者 author: reference('authors'), - // 按 "slug "从 "blog "集合中引用相关帖子数组 + // 通过 `slug` 从 `blog` 集合中,引用相关帖子数组 relatedPosts: z.array(reference('blog')), }) }); @@ -115,7 +115,14 @@ const authors = defineCollection({ export const collections = { blog, authors }; ``` -更多有关示例请 [参考 `内容集合`指南](/zh-cn/guides/content-collections/#定义集合引用)。 +在使用 `getEntry()` 或 `getEntries()` 的运行时环境下,对引用条目的有效性进行验证: + +```astro title="src/pages/[posts].astro" +// 如果引用条目是无效的,那么将会返回 undefined。 +const relatedPosts = await getEntries(blogPost.data.relatedPosts); +``` + +更多有关示例请 [参考`内容集合`指南](/zh-cn/guides/content-collections/#定义集合引用)。 ### `getCollection()` @@ -143,7 +150,7 @@ const draftBlogPosts = await getCollection('blog', ({ data }) => { --- ``` -更多有关示例请 [参考 `内容集合`指南](/zh-cn/guides/content-collections/#查询集合)。 +更多有关示例请 [参考`内容集合`指南](/zh-cn/guides/content-collections/#查询集合)。 ### `getEntry()` @@ -252,8 +259,7 @@ import type { CollectionEntry } from 'astro:content'; #### `id` -**适用于:** `type: 'content'` 和 `type: 'data'` 集合 -**示例类型:** `'author-1' | 'author-2' | ...` +**类型:** `string` 一个唯一的 ID。请注意,Astro 的内置 `glob()` 加载器中的所有 ID 都是 slug 化的。