Lzh on GitHub

原始内容

在应用程序中访问内容的原始数据。

在 Content v2 中,有很多关于在生产环境中访问内容原始数据的请求。在 Content v3 中,可以将内容的原始数据发布到生产环境。

要将原始内容发布到生产环境,你需要在你的集合 schema 中定义 rawbody 字段。就这么简单。

Nuxt Content 将检测到你 schema 中的这个神奇字段,并用原始内容填充它。

content.config.ts

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

export default defineContentConfig({
  collections: {
    docs: defineCollection({
      source: '**',
      type: 'page',
      schema: z.object({
        rawbody: z.string()
      })
    })
  }
})

你可以使用 queryCollection() 来获取原始内容。

pages/index.vue
<script setup lang="ts">
const route = useRoute()
const { data } = useAsyncData('page-' + route.path, () => queryCollection('docs').path(route.path).first())
</script>

<template>
  <pre>{{ data.rawbody }}</pre>
</template>

如果你不想发布特定文件的原始内容,可以在该文件的 frontmatter 中添加 rawbody: ''。自动填充的 rawbody 值就像默认值一样,当你在 frontmatter 中定义 rawbody 时,它将被覆盖。

content.md
---
title: My page
rawbody: ''
---
重要的是使用与集合 schema 中定义的数据类型相同的数据类型填充 frontmatter 字段。在本例中,rawbody 是一个字符串,你应该考虑传递一个空字符串。不要使用 boolean 或其他类型的值。