defineDocumentType defines the schema for one particular document type, often referred to as a model or content type.

Document type definitions are used within the options for makeSource, which is how the definition is passed onto Contentlayer.

Usage

const Doc = defineDocumentType(() => ({
  name: 'Doc',
  filePathPattern: '**/*.md',
  fields: {
    title: {
      type: 'string',
    },
  },
}))

Options

name (required)

Name of the document. This defines the types and functions that are generated for documents of this type.

const Doc = defineDocumentType(() => ({
  name: "Doc",
  // ...
})

In the usage example, the name was Doc, which would generate a data object allDocs representing the collection of all matching documents (see filePathPattern). And it would add a type definition for Doc, representing the shape of the data object created from your content.

fields

Field definitions determine the data shape for the document type. See the field types reference for more information.

description

Provides a description for the generated type definition.

computedFields

Computed fields can be calculated on the fly rather than being read directly from the content source.

They follow a similar pattern to fields (see above), except that each field takes a required resolve option, which is a function that resolves the value of the field. The function receives a single argument, the document object.

Here's an example that introspects the document for its path and applies a url_path property to the document.

import { urlFromFilePath } from '../utils'

export const Page = defineDocumentType(() => ({
  // ...
  fields: {
    // ...
  },
  computedFields: {
    url_path: {
      type: 'string',
      resolve: (doc) => doc._raw.flattenedPath.replace(/pages\/?/, ''),
    },
  },
}))

filePathPattern

Path is relative to the contentDirPath specified in the makeSource options. Use glob patterns to target a specific subset of documents.

This can also point to an individual file, which would create a singleton document type. See the isSingleton option for more information.

Example:

This would recursively retrieve all files with a .md extension in the content directory of your project.

const Doc = defineDocumentType(() => ({
  filePathPattern: '**/*.md',
  // ...
}))

export default makeSource({
  contentDirPath: 'content',
  documentTypes: [Doc],
})

contentType (default: markdown)

How the main content area of the file should be parsed. It is treated as markdown by default.

Options:

  • markdown: Parse the content area of the file as markdown.
  • mdx: Allows use of MDX in the body of the file.
  • data: Ignores the content area of the file and gets only the frontmatter from JSON or YAML files.

isSingleton (default: false)

By default, Contentlayer will grab any files matching the filePathPattern. Setting the document type to be a singleton means that Contentlayer will grab a single file and export a single object for use in your project.

Example:

In this example, Contentlayer would export a globalConfig data object with the content from config/global.yaml (inside the main content directory). The type definition for the object would match the name (GlobalConfig), as in non-singleton types.

defineDocumentType(() => ({
  name: 'GlobalConfig',
  filePathPattern: `config/global.yaml`,
  isSingleton: true,
  // ...
}))

Was this article helpful to you?
Provide feedback

Last edited on September 20, 2022.
Edit this page