文章元数据分两层:content.config.ts 定义 schema,Markdown 的文件头填值。

定义 collection

import { defineCollection, defineContentConfig, z } from '@nuxt/content'

export default defineContentConfig({
  collections: {
    posts: defineCollection({
      type: 'page',
      source: 'posts/*.md',
      schema: z.object({
        date: z.date(),
        tags: z.array(z.string()).default([]),
      }),
    }),
  },
})

写 frontmatter

---
title: 文章标题
description: 列表页与搜索引擎用的摘要
date: 2026-09-25
tags:
  - Nuxt
---

在页面里查询

const { data: posts } = await useAsyncData('posts', () =>
  queryCollection('posts').order('date', 'DESC').all(),
)

单篇用 .path(route.path).first(),分页用 .skip(n).limit(m) 配合 .count()。

常见坑

  1. date 用 z.date() 才能正确排序,写字符串会按字典序比。
  2. 文件名决定 URL:content/posts/hello.md 对应 /posts/hello。
  3. 定好 schema 后,frontmatter 里多写字段不会报错,少写必填字段会在构建时失败。