@@ -567,6 +567,67 @@ const { Content } = await render(post);
567567探索 [ GitHub 上博客教程演示代码的 ` src/pages/ ` 文件夹] ( https://github.com/withastro/blog-tutorial-demo/tree/content-collections/src/pages ) ,查看如何从你的集合创建页面,例如博客文章列表、标签页面等的完整示例!
568568:::
569569
570+ ## 内容集合的 JSON 模式(Schema)
571+
572+ <p ><Since v = " 4.13.0" /></p >
573+
574+ Astro 会为内容集合自动生成 [ JSON 模式] ( https://json-schema.org/ ) 文件,你可以在编辑器中利用这些文件获取数据文件的智能提示和类型检查功能。
575+
576+ 项目中每个内容集合都会生成对应的 JSON 模式文件,并输出至 ` .astro/collections/ ` 目录。例如,若你拥有两个分别名为 ` authors ` 和 ` posts ` 的集合,那么 Astro 将会生成:` .astro/collections/authors.schema.json ` 和 ` .astro/collections/posts.schema.json ` 。
577+
578+ ### 在 JSON 文件中使用 JSON 模式
579+
580+ 你可以通过在 JSON 文件中设置 ` $schema ` 字段来手动指向 Astro 生成的模式文件。该值应为从数据文件到模式文件的相对路径。在以下示例中,位于 ` src/data/authors/ ` 目录下的数据文件使用了为 ` authors ` 集合生成的模式:
581+
582+ ``` json title="src/data/authors/armand.json" ins={2}
583+ {
584+ "$schema" : " ../../../.astro/collections/authors.schema.json" ,
585+ "name" : " Armand" ,
586+ "skills" : [" Astro" , " Starlight" ]
587+ }
588+ ```
589+
590+ #### 在 VS Code 中为一组 JSON 文件配置统一模式
591+
592+ 在 VS Code 中,你可以通过 [ ` json.schemas ` 设置] ( https://code.visualstudio.com/docs/languages/json#_json-schemas-and-settings ) 配置模式文件,使其自动应用于集合中的所有文件。在以下示例中,` src/data/authors/ ` 目录下的所有文件都将使用为 ` authors ` 集合生成的模式文件:
593+
594+ ``` json
595+ {
596+ "json.schemas" : [
597+ {
598+ "fileMatch" : [" /src/data/authors/**" ],
599+ "url" : " ./.astro/collections/authors.schema.json"
600+ }
601+ ]
602+ }
603+ ```
604+
605+ ### 在 VS Code 中为 YAML 文件配置模式
606+
607+ 在 VS Code 中,你可以通过安装 [ Red Hat YAML] ( https://marketplace.visualstudio.com/items?itemName=redhat.vscode-yaml ) 扩展来实现在 YAML 文件中使用 JSON 模式。安装此扩展后,你可以使用特殊的注释语法在 YAML 文件中引用模式:
608+
609+ ``` yaml title="src/data/authors/armand.yml" ins={1}
610+ # yaml-language-server: $schema=../../../.astro/collections/authors.schema.json
611+ name : Armand
612+ skills :
613+ - Astro
614+ - Starlight
615+ ` ` `
616+
617+ #### 在 VS Code 中为一组 YAML 文件配置统一模式
618+
619+ 通过 Red Hat YAML 扩展,你可以使用 ` yaml.schemas` 设置配置模式文件,使其自动应用于集合中的所有 YAML 文件。在以下示例中,`src/data/authors/` 目录下的所有 YAML 文件都将使用为 `authors` 集合生成的模式文件:
620+
621+ ` ` ` json
622+ {
623+ "yaml.schemas": {
624+ "./.astro/collections/authors.schema.json": ["/src/content/authors/*.yml"]
625+ }
626+ }
627+ ` ` `
628+
629+ 更多细节可参阅 Red Hat YAML 扩展文档的 ["Associating schemas"](https://marketplace.visualstudio.com/items?itemName=redhat.vscode-yaml#associating-schemas) 部分。
630+
570631# # 何时创建集合
571632
572633只要你拥有一组共享相同结构的相关数据或内容,你就可以[创建一个集合](#定义集合)。
0 commit comments