导航菜单

内容集合

Content Collections

src/content/config.ts 定义的集合与 Schema,用于校验 frontmatter,并提供类型提示与查询能力。

定义集合

// src/content/config.ts
import { defineCollection, z } from 'astro:content';

const docs = defineCollection({
  schema: z.object({
    title: z.string(),
    description: z.string().optional(),
    date: z.date().optional(),
  }),
});

export const collections = { docs };

使用集合

---
import { getCollection } from 'astro:content';
const docs = await getCollection('docs');
---
<ul>
  {docs.map((item) => <li>{item.data.title}</li>)}
</ul>

好处

  • frontmatter 校验防止漏写字段。
  • 自动生成类型,IDE 补全。
  • 便于按标签/日期过滤、生成导航与列表。

搜索