diff --git a/src/content/docs/zh-cn/guides/content-collections.mdx b/src/content/docs/zh-cn/guides/content-collections.mdx index 7fca0b5b029c9..9c343f3c9212e 100644 --- a/src/content/docs/zh-cn/guides/content-collections.mdx +++ b/src/content/docs/zh-cn/guides/content-collections.mdx @@ -311,7 +311,7 @@ relatedPosts: ### 定义自定义 slugs -当使用 `type: 'content'` 时,每个内容条目从它的[文件 `id`](/zh-cn/reference/api-reference/#id) 生成一个 URL 友好的 `slug` 属性。slug 用于直接从集合中查询条目。在根据内容创建新页面和 URL 时,它也很有用。 +当使用 `type: 'content'` 时,每个内容条目从它的[文件 `id`](/zh-cn/reference/modules/astro-content/#id) 生成一个 URL 友好的 `slug` 属性。slug 用于直接从集合中查询条目。在根据内容创建新页面和 URL 时,它也很有用。 你可以通过将自己的 `slug` 属性添加到文件 frontmatter 来覆盖条目生成的 slug。这类似于其他 Web 框架的 "permalink" 特性。`"slug"` 是一个特殊的保留属性名称,不允许出现在自定义集合 `schema` 中,也不会出现在条目的 `data` 属性中。 @@ -325,7 +325,7 @@ slug: my-custom-slug/supports/slashes ## 查询集合 -Astro 提供了两个函数来查询一个集合并返回一个(或多个)内容条目:[`getCollection()`](/zh-cn/reference/api-reference/#getcollection) 和 [`getEntry()`](/zh-cn/reference/api-reference/#getentry)。 +Astro 提供了两个函数来查询一个集合并返回一个(或多个)内容条目:[`getCollection()`](/zh-cn/reference/modules/astro-content/#getcollection) 和 [`getEntry()`](/zh-cn/reference/modules/astro-content/#getentry)。 ```js import { getCollection, getEntry } from 'astro:content'; @@ -342,7 +342,7 @@ const allBlogPosts = await getCollection('blog'); const graceHopperProfile = await getEntry('authors', 'grace-hopper'); ``` -这两个函数都返回由 [`CollectionEntry`](/zh-cn/reference/api-reference/#集合条目类型) 类型定义的内容条目。 +这两个函数都返回由 [`CollectionEntry`](/zh-cn/reference/modules/astro-content/#collectionentry) 类型定义的内容条目。 ### 使用引用的数据 @@ -437,7 +437,7 @@ const blogEntries = await getCollection('blog'); 组件还可以将整个内容条目作为属性传递。 -如果这样做,你可以使用 [`CollectionEntry`](/zh-cn/reference/api-reference/#集合条目类型) 实用工具使用 TypeScript 正确地输入组件属性。此实用工具接受与集合 schema 名称匹配的字符串参数,并将继承该集合 schema 的所有属性。 +如果这样做,你可以使用 [`CollectionEntry`](/zh-cn/reference/modules/astro-content/#collectionentry) 实用工具使用 TypeScript 正确地输入组件属性。此实用工具接受与集合 schema 名称匹配的字符串参数,并将继承该集合 schema 的所有属性。 ```astro /CollectionEntry(?:<.+>)?/ --- @@ -507,7 +507,7 @@ const { Content } = await entry.render(); ### 构建为服务器输出(SSR) -如果你正在构建一个动态网站(使用 Astro 的 SSR 支持),则不需要在构建期间提前生成任何路径。相反,你的页面应该检查请求(使用 `Astro.request` 或 `Astro.params`)以按需找到 `slug`,然后使用 [`getEntry()`](/zh-cn/reference/api-reference/#getentry) 获取它。 +如果你正在构建一个动态网站(使用 Astro 的 SSR 支持),则不需要在构建期间提前生成任何路径。相反,你的页面应该检查请求(使用 `Astro.request` 或 `Astro.params`)以按需找到 `slug`,然后使用 [`getEntry()`](/zh-cn/reference/modules/astro-content/#getentry) 获取它。 ```astro diff --git a/src/content/docs/zh-cn/guides/imports.mdx b/src/content/docs/zh-cn/guides/imports.mdx index ae345bc4dd897..bd7d84d683b73 100644 --- a/src/content/docs/zh-cn/guides/imports.mdx +++ b/src/content/docs/zh-cn/guides/imports.mdx @@ -244,9 +244,7 @@ glob 模式是一个支持特殊通配符的文件路径。用于一次引用项 #### `Astro.glob()` vs `getCollection()` -[内容集合](/zh-cn/guides/content-collections/) 提供用于加载多个文件的 [`getCollection()` API](/zh-cn/reference/api-reference/#getcollection) 代替 `Astro.glob()`。如果你的内容文件 (例如 Markdown, MDX, Markdoc) 位于`src/content/` 目录内的集合中,使用 `getCollection()` [查询集合](/zh-cn/guides/content-collections/#查询集合)并返回内容条目。 - - +[内容集合](/zh-cn/guides/content-collections/) 提供用于加载多个文件的 [`getCollection()` API](/zh-cn/reference/modules/astro-content/#getcollection) 代替 `Astro.glob()`。如果你的内容文件 (例如 Markdown, MDX, Markdoc) 位于`src/content/` 目录内的集合中,使用 `getCollection()` [查询集合](/zh-cn/guides/content-collections/#查询集合)并返回内容条目。 ## WASM diff --git a/src/content/docs/zh-cn/guides/markdown-content.mdx b/src/content/docs/zh-cn/guides/markdown-content.mdx index c3a974814f0e0..c00392ccf5879 100644 --- a/src/content/docs/zh-cn/guides/markdown-content.mdx +++ b/src/content/docs/zh-cn/guides/markdown-content.mdx @@ -62,7 +62,7 @@ const posts = Object.values(await import.meta.glob('../posts/*.md', { eager: tru 当通过辅助函数从集合中获取数据时,Markdown 的 frontmatter 属性在 `data` 对象(例如 `post.data.title`)上可用。此外,`body` 包含了 Markdown 的正文内容,这部分内容是原始的、未编译的字符串类型。 -请参阅完整的 [集合条目类型](/zh-cn/reference/api-reference/#集合条目类型)。 +请参阅完整的 [集合条目类型](/zh-cn/reference/modules/astro-content/#collectionentry)。 #### 导入 Markdown diff --git a/src/content/docs/zh-cn/reference/modules/astro-content.mdx b/src/content/docs/zh-cn/reference/modules/astro-content.mdx new file mode 100644 index 0000000000000..c4b8f22ff290c --- /dev/null +++ b/src/content/docs/zh-cn/reference/modules/astro-content.mdx @@ -0,0 +1,378 @@ +--- +title: 内容集合 API 参考 +i18nReady: true +tableOfContents: + minHeadingLevel: 2 + maxHeadingLevel: 6 +--- +import Since from '~/components/Since.astro'; +import ReadMore from '~/components/ReadMore.astro'; + +

+ +内容集合提供了 API 来配置和查询 `src/content/` 中的 Markdown 或 MDX 文档。有关功能和用法示例,请参考[内容集合指南](/zh-cn/guides/content-collections/)。 + +## 从 `astro:content` 导入 + +```js +import { + z, + defineCollection, + getCollection, + getEntry, + getEntries, + reference, + } from 'astro:content'; +``` + +### `defineCollection()` + +

+ +**类型:**`(input: CollectionConfig) => CollectionConfig` +

+ +`defineCollection()` 是一个用于在 `src/content/config.*` 文件中配置集合的工具函数。 + +```ts +// src/content/config.ts +import { z, defineCollection } from 'astro:content'; +const blog = defineCollection({ + type: 'content', + schema: z.object({ + title: z.string(), + permalink: z.string().optional(), + }), +}); + +// 将定义的集合公开给 Astro +// 通过 `collections` 导出 +export const collections = { blog }; +``` + +这个函数接受以下属性: + +#### `type` + +

+ +**类型:**`'content' | 'data'`
+**默认:**`'content'`
+ +

+ +`type` 是一个字符串,用于定义存储在集合中的条目的类型: + +- `'content'` - 用于内容创作格式,如 Markdown (`.md`)、MDX (`.mdx`) 或 Markdoc (`.mdoc`) +- `'data'` - 用于 JSON (`.json`) 或 YAML (`.yaml`) 等纯数据格式 + +:::tip +这意味着集合**不能**存储内容和数据格式的混合。你必须按类型将这些条目分割成单独的集合。 +::: + +#### `schema` + +

+ +**类型:**ZodType | (context: SchemaContext) => ZodType +

+ +`schema` 是一个可选的 Zod 对象,用于配置集合的文档 frontmatter 的类型和形状。每个值必须使用 [Zod 验证器](https://github.com/colinhacks/zod) + +有关示例请 [参考 `内容集合`指南](/zh-cn/guides/content-collections/#定义集合模式)。 + +### `reference()` + +

+ +**类型:**`(collection: string) => ZodEffects`
+ +

+ +在内容配置中使用 `reference()` 函数来定义从一个集合到另一个集合的关系或 "引用"。该函数接受一个集合名称,并验证内容前置事项或数据文件中指定的条目标识符。 + +此示例定义了从博客作者到 "作者 "集合的引用,以及到同一 "博客 "集合的相关文章数组的引用: + +```ts +import { defineCollection, reference, z } from 'astro:content'; + +const blog = defineCollection({ + type: 'content', + schema: z.object({ + // 通过 "id "从 "作者 "集合中引用单个作者 + author: reference('authors'), + // 按 "slug "从 "blog "集合中引用相关帖子数组 + relatedPosts: z.array(reference('blog')), + }) +}); + +const authors = defineCollection({ + type: 'data', + schema: z.object({ /* ... */ }) +}); + +export const collections = { blog, authors }; +``` + +有关示例请 [参考 `内容集合`指南](/zh-cn/guides/content-collections/#定义集合引用)。 + +### `getCollection()` + +

+ +**类型:** `(collection: string, filter?: (entry: CollectionEntry) => boolean) => CollectionEntry[]` +

+ +`getCollection()` 是一个函数,用于通过集合名称检索内容集合条目列表。 + +默认情况下,它返回集合中的所有项目,并接受可选的 `filter` 函数来缩小条目属性。这允许你根据 `id`、`slug` 或 frontmatter 值(通过 `data` 对象)查询集合中的某些项目。 + +```astro +--- +import { getCollection } from 'astro:content'; + +// 获取 `src/content/blog/` 中的所有条目 +const allBlogPosts = await getCollection('blog'); + +// 仅返回 frontmatter 中 `draft: true` 的条目 +const draftBlogPosts = await getCollection('blog', ({ data }) => { + return data.draft === true; +}); +--- +``` + +有关示例请 [参考 `内容集合`指南](/zh-cn/guides/content-collections/#查询集合) 以获取示例用法。 + +### `getEntry()` + +

+ +**类型:** + +- `(collection: string, contentSlugOrDataId: string) => CollectionEntry` +- `({ collection: string, id: string }) => CollectionEntry` +- `({ collection: string, slug: string }) => CollectionEntry` + +`getEntry()` 是一个函数,可通过集合名称和条目 `id` (对于 `type: 'data'` 集合)或条目 `slug` (对于 `type: 'content'` 集合)检索单个集合条目。`getEntry()`也可用于获取引用条目,以访问`data`、`body`或`render()`属性: + +```astro +--- +import { getEntry } from 'astro:content'; + +// 得到 `src/content/blog/enterprise.md` +const enterprisePost = await getEntry('blog', 'enterprise'); + +// 得到 `src/content/captains/picard.yaml` +const picardProfile = await getEntry('captains', 'picard'); + +// 得到 the profile referenced by `data.captain` +const enterpriseCaptainProfile = await getEntry(enterprisePost.data.captain); +--- +``` + +有关`内容集合`的示例,请参考 [查询集合条目](/zh-cn/guides/content-collections/#查询集合)。 + +### `getEntries()` + +

+ +**类型:** + +- `(Array<{ collection: string, id: string }>) => CollectionEntry[]` +- `(Array<{ collection: string, slug: string }>) => CollectionEntry[]` + +`getEntries()` 是一个从同一集合中检索多个集合条目的函数。这对于[返回引用条目的数组](/zh-cn/guides/content-collections/#定义集合引用)访问其关联的`data`、`body`和`render()`属性非常有用。 + +```astro +--- +import { getEntries } from 'astro:content'; + +const enterprisePost = await getEntry('blog', 'enterprise'); + +// 获取由 `data.relatedPosts` 引用的相关帖子 +const enterpriseRelatedPosts = await getEntries(enterprisePost.data.relatedPosts); +--- +``` + +### `getEntryBySlug()` + +

+ +**类型:** `(collection: string, slug: string) => Promise>` +

+ +:::caution[已废弃] +使用 [`getEntry()`函数](#getentry) 查询内容条目。该函数接受与 `getEntryBySlug()` 相同的参数,并支持通过 `id` 查询 JSON 或 YAML 集合。 +::: + +`getEntryBySlug()` 是一个函数,用于通过集合名称和条目 `slug` 检索单个集合条目。 + +```astro +--- +import { getEntryBySlug } from 'astro:content'; + +const enterprise = await getEntryBySlug('blog', 'enterprise'); +--- +``` + +有关示例请 [参考`内容集合`指南](/zh-cn/guides/content-collections/#查询集合) 以获取示例用法。 + +### `getDataEntryById()` + +

+ +**类型:** `(collection: string, id: string) => Promise>`
+ +

+ +:::caution[已废弃] +使用 [`getEntry()`函数](#getentry) 查询数据条目。该函数接受与 `getDataEntryById()` 相同的参数,并支持通过 `slug` 查询 Markdown 等内容创作格式的条目。 +::: + +`getDataEntryById()` 是一个用于通过集合名称和条目 `id` 检索单个集合条目的函数。 + +```astro +--- +import { getDataEntryById } from 'astro:content'; + +const picardProfile = await getDataEntryById('captains', 'picard'); +--- +``` + +## `astro:content` 类型 + +```ts +import type { + CollectionEntry, + CollectionKey, + ContentCollectionKey, + DataCollectionKey, + SchemaContext, + } from 'astro:content'; +``` + +### `CollectionEntry` + +查询函数包括 [`getCollection()`](#getcollection)、[`getEntry()`](#getentry) 和 [`getEntries()`](#getentries),每个函数都会返回 `CollectionEntry` 类型的条目。这种类型可以作为 `astro:content` 中的实用程序使用: + +```ts +import type { CollectionEntry } from 'astro:content'; +``` + +`CollectionEntry` 是一个泛型类型。将其与你正在查询的集合的名称一起使用。 +例如,`blog` 集合中的条目的类型为 `CollectionEntry<'blog'>`。 + +每个 `CollectionEntry` 都是一个具有以下值的对象: + +#### `id` + +**适用于:** `type: 'content'` 和 `type: 'data'` 集合 +**示例类型:** + - 内容集合: `'entry-1.md' | 'entry-2.md' | ...` + - 数据集合: `'author-1' | 'author-2' | ...` + +一个使用相对于 `src/content/[collection]` 的文件路径的唯一 ID。根据集合条目文件路径枚举所有可能的字符串值。 +请注意,[`类型: 'content'`](#type) 的集合在其 ID 中包含文件扩展名,而定义为 `type: 'data'` 的集合则不包含。 + +#### `collection` + +**适用于:** `type: 'content'` 和 `type: 'data'` 集合 +**示例类型:** `'blog' | 'authors' | ...` + +`src/content/` 下顶级文件夹的名称,条目位于该文件夹中。该名称用于在 schema 和查询函数中引用集合。 + +#### `data` + +**适用于:** `type: 'content'` 和 `type: 'data'` 集合 +**类型:**`CollectionSchema` + +一个从集合模式推断出的 frontmatter 属性对象([参考 `defineCollection()`](#definecollection))。如果没有配置模式,则默认为 `any`。 + +#### `slug` + +**适用于:** 仅仅 `type: 'content'` 集合 +**示例类型:** `'entry-1' | 'entry-2' | ...` + +可用于 Markdown 或 MDX 文档的 URL 标头。默认为不含文件扩展名的 "id",但可以通过在文件的 frontmatter 中设置[`slug`属性](/zh-cn/guides/content-collections/#定义自定义-slugs)来覆盖。 + +#### `body` + +**适用于:** 仅 `type: 'content'` 集合 +**类型:**`string` + +一个包含 Markdown 或 MDX 文档原始未编译的 body 的字符串。 + +#### `render()` + +**适用于:** 仅 `type: 'content'` 集合 +**类型:**`() => Promise` + +一个用于编译给定的 Markdown 或 MDX 文档以进行渲染的函数。它返回以下属性: + +- `` - 用于在 Astro 文件中渲染文档内容的组件。 +- `headings` - 生成的标题列表,[与 Astro 的 `getHeadings()` 工具函数相同](/zh-cn/guides/markdown-content/#可用属性) 在 Markdown 和 MDX 导入中。 +- `remarkPluginFrontmatter ` - 在应用 [remark 或 rehype 插件](/zh-cn/guides/markdown-content/#以编程方式修改-frontmatter)后修改后的 frontmatter 对象。设置为类型 `any`。 + +```astro +--- +import { getEntryBySlug } from 'astro:content'; +const entry = await getEntryBySlug('blog', 'entry-1'); + +const { Content, headings, remarkPluginFrontmatter } = await entry.render(); +--- +``` + +有关示例请 [参考 `内容集合`指南](/zh-cn/guides/content-collections/#将内容渲染成-html) 以获取示例用法。 + +### `CollectionKey` + +

+ +这是一个在 `src/content/config.*` 文件中定义的所有集合名称的字符串联合类型。当定义一个可以接受任何集合名称的通用函数时,这个类型很有用。 + +```ts +import { type CollectionKey, getCollection } from 'astro:content'; + +async function getCollection(collection: CollectionKey) { + return getCollection(collection); +} +``` + +### `ContentCollectionKey` + +

+ +一个在 `src/content/config.*` 文件中定义的所有 `type: 'content'` 集合名称的字符串联合类型。 + +### `DataCollectionKey` + +

+ +一个在 `src/content/config.*` 文件中定义的所有 `type: 'data'` 集合名称的字符串联合类型。 + +### `SchemaContext` + +即 `defineCollection` 在 `schema` 函数形状中所使用的 `context` 对象。当为多个集合构建可重用的模式时,这个类型很有用。 + +它包括了以下属性: + +- `image` - `image()` schema 辅助工具允许你 [在内容集合中使用本地图片](/zh-cn/guides/images/#内容集合中的图像)。 + +```ts +import type { SchemaContext } from 'astro:content'; + +export const imageSchema = ({ image }: SchemaContext) => + z.object({ + image: image(), + description: z.string().optional(), + }); + +const blog = defineCollection({ + type: 'content', + schema: ({ image }) => z.object({ + title: z.string(), + permalink: z.string().optional(), + image: imageSchema({ image }) + }), +}); +``` 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 6884107efc30e..e3932e47ca6eb 100644 --- a/src/content/docs/zh-cn/tutorials/add-content-collections.mdx +++ b/src/content/docs/zh-cn/tutorials/add-content-collections.mdx @@ -246,7 +246,7 @@ import { Steps } from '@astrojs/starlight/components'; ### 用 `getCollection()` 代替 `Astro.glob()` -11. 在任何你有博客文章列表的地方,比如教程中的博客页面 (`src/pages/blog.astro/`),你需要将 `Astro.glob()` 替换为 [`getCollection()`](/zh-cn/reference/api-reference/#getcollection) 来获取 Markdown 文件的内容和元数据。 +11. 在任何你有博客文章列表的地方,比如教程中的博客页面 (`src/pages/blog.astro/`),你需要将 `Astro.glob()` 替换为 [`getCollection()`](/zh-cn/reference/modules/astro-content/#getcollection) 来获取 Markdown 文件的内容和元数据。 ```astro title="src/pages/blog.astro" "post.data" "getCollection(\"posts\")" "/posts/${post.slug}/" del={7} ins={2,8} ---