styledsea

Collections

Collection of content data for your app

Define Collections

Define a collection to parse a certain set of files.

import { defineCollections } from 'styledsea-docs-mdx/config';
import { z } from 'zod';
 
export const blog = defineCollections({
  type: 'doc',
  dir: './content/blog',
  schema: z.object({
    // allowed...
  }),
});
PropTypeDefault
dir
string | string[]
-
files
string[]
-
schema
Schema | ((ctx: TransformContext) => Schema)
-
type
Type
-
transform
(entry: CollectionEntry<Type, output<Schema>>, globalConfig?: GlobalConfig | undefined) => Output | Promise<Output>
-
mdxOptions
Type extends "doc" ? MDXOptions : never
-

Options

dir

Directories to scan input files.

Schema

The Zod schema to validate file data (frontmatter on doc type, content on meta type).

import { defineCollections } from 'styledsea-docs-mdx/config';
import { z } from 'zod';
 
export const blog = defineCollections({
  type: 'doc',
  dir: './content/blog',
  schema: z.object({
    name: z.string(),
  }),
});

You can add additional properties to the output. Note that the validation is done by build time, hence the output must be serializable.

You can also pass a function and receives the transform context.

import { defineCollections } from 'styledsea-docs-mdx/config';
import { z } from 'zod';
 
export const blog = defineCollections({
  type: 'doc',
  dir: './content/blog',
  schema: (ctx) => {
    return z.object({
      name: z.string(),
      testPath: z.string().default(ctx.path),
    });
  },
});

Type

The accepted type of collection.

TypeDescription
metaJSON/YAML File
docMarkdown/MDX Documents

MDX Options

You can also customise MDX options from collections. Notice that passing mdxOptions to collection overrides all defaults from global config.

import {
  defineCollections,
  getDefaultMDXOptions,
} from 'styledsea-docs-mdx/config';
 
export const blog = defineCollections({
  type: 'doc',
  mdxOptions: getDefaultMDXOptions({
    // mdx options
  }),
});

We use getDefaultMDXOptions to apply default MDX options, it accepts the Default MDX Options.

For full control over MDX options, you can pass MDX options without getDefaultMDXOptions, which means no defaults will be applied (except the ones from MDX.js).

This API only available on doc type.

Transform

A function to perform runtime transformation on collection entries.

See Transform.

Define Docs

You can use defineDocs to define the required collections to work with Adalt. It offers the same API as defineCollections.

import { defineDocs } from 'styledsea-docs-mdx/config';
 
export const { docs, meta } = defineDocs({
  docs: {
    // optional, you can pass options to each collection
  },
  meta: {
    // optional, you can pass options to each collection
  },
});

The docs and meta are collections on their own. You can pass collection options to them as shown above.

Extend schema

You can extend the default Zod schema of docs and meta.

import {
  frontmatterSchema,
  metaSchema,
  defineDocs,
} from 'styledsea-docs-mdx/config';
import { z } from 'zod';
 
export const { docs, meta } = defineDocs({
  docs: {
    schema: frontmatterSchema.extend({
      index: z.boolean().default(false),
    }),
  },
  meta: {
    schema: metaSchema.extend({
      // other props
    }),
  },
});

Last updated on

On this page

Edit on GitHub