Lzh on GitHub

集合源

了解如何在 Nuxt Content 集合中导入你的文件。

Nuxt Content 提供了几种将内容文件导入到你的集合中的方法。你可以通过使用 defineCollection 中的 source 属性来配置源:

content.config.ts
import { defineCollection, defineContentConfig } from '@nuxt/content'

export default defineContentConfig({
  collections: {
    docs: defineCollection({
      source: '**',
      type: 'page'
    })
  }
})

source

source 属性可以定义为字符串(遵循 glob 模式)或对象,从而可以更详细地配置内容文件夹中目标目录和文件的源。

示例:

  • source: '**':包含 content 目录及其子目录中的所有文件。
  • source: '**/*.md':包含 content 目录及其子目录中的所有 Markdown 文件。
  • source: 'docs/**/*.yml':包含 content/docs 及其子目录中的所有 YML 文件。
  • source: '**/*.{json,yml}':包含 content 目录及其所有子目录中的 JSONYML 文件。
  • source: '*.json':仅包含直接位于 content 目录中的 JSON 文件,不包括任何子目录。

include (必需)

content 文件夹中目标仓库和文件的 Glob 模式。

exclude

从导入中排除内容的 Glob 模式。

prefix

此配置仅适用于 page 类型,该类型在内容文件和网站上的页面之间存在一对一的关系。

它表示网站上相应页面的路径前缀(基本 URL)。

prefix 必须以 / 开头。

默认情况下,模块提取 source(或 source.include)的静态前缀,并将其用作内容路径的前缀。例如,如果你定义了 /en/** 源,模块将自动填充 /en 作为 prefix。你可以手动提供前缀来覆盖此行为。可以通过在集合源中设置 prefix: '/' 来删除前缀。

defineCollection({
  type: "page",
  source: {
    include: "en/**",
    exclude: ["en/01.backface-visibility.md.filter.md"],
    prefix: '/'
  }
})

cwd

用于内容匹配的根目录。

示例:

如果你想包含 content 目录之外的文件夹中的文件,请将该文件夹的绝对路径设置为 cwd 属性。

source: {
  cwd: path.resolve('packages/my-pkg/docs'),
  include: '**/*.md',
}

repository

表示远程 Git 仓库 URL 的外部源(例如,https://github.com/nuxt/content)。

定义外部源时,还必须定义 include 选项。include 模式对于模块知道要使用哪些文件进行集合至关重要。

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

export default defineContentConfig({
  collections: {
    docs: defineCollection({
      type: 'page',
      source: {
        repository: 'https://github.com/nuxt/content',
        include: 'docs/content/**',
      },
    })
  }
})

authToken

私有仓库的身份验证令牌(例如,GitHub 个人访问令牌)。

切勿将身份验证令牌或凭据直接提交到你的代码中。使用环境变量或其他安全方法在运行时提供这些值。

authBasic

私有仓库的基本身份验证(例如,Bitbucket 用户名和密码)。

defineCollection({
  type: 'page',
  source: {
    repository: 'https://bitbucket.org/username/repo',
    authBasic: {
      username: 'username',
      password: 'password',
    },
  },
})