# Collections

Configure page folders and frontmatter schemas.

A collection is a folder of Markdown pages and meta files, which Vuesho validates and compiles when the site builds. Without a config, a site has one collection, `content/`, as if it had this config:

```ts title="vuesho.config.ts"
import { createSite } from "vuesho/config";
import { defineDocs } from "vuesho/content";

const docs = defineDocs(); // [!code highlight]

export default createSite(docs.toSource()); // [!code highlight]
```

## Defining Collections [#defining-collections]

`defineDocs()` reads pages and meta files. Pass the page options in `docs`, and the meta file options in `meta`:

```ts title="vuesho.config.ts"
const docs = defineDocs({
  // The folder, from the project root
  dir: "docs",
  // The options of the Markdown pages
  docs: {
    // The pages in `dir`, where `!` leaves files out
    files: ["**/*.md", "!archive/**"],
  },
  // The options of the meta files, like `meta.json`
  meta: {
    files: ["**/meta.json"],
  },
});
```

A collection skips `node_modules`, and the files and folders whose names start with `_` or `.`. A pattern can't hold `(`, `)` or `\`. A file can be in several collections, which validate and compile it with their own options.

## Frontmatter Schema [#frontmatter-schema]

A page accepts Vuesho's [frontmatter fields](/markdown#frontmatter), and drops unknown keys. To add custom fields, extend `pageSchema`, a [Zod](https://zod.dev) object from `vuesho/schema`:

```npm
npm i zod
```

```ts title="vuesho.config.ts"
import { defineDocs } from "vuesho/content";
import { pageSchema } from "vuesho/schema";
import { z } from "zod";

const docs = defineDocs({
  docs: { schema: pageSchema.extend({ author: z.string() }) }, // [!code highlight]
});
```

`page.data.author` is then a `string` in layouts and plugins, and a page without it fails the build. For meta files, extend `metaSchema` in `meta.schema`.

A schema can be any [Standard Schema](https://standardschema.dev), like Valibot or ArkType. Don't declare fields that a plugin adds, like the [blog plugin's](/plugins/blog) `tags`.

### Computed Defaults [#computed-defaults]

To compute a default from the file, like a post's date from its file name, make `schema` a function. It receives each file's absolute `path` and `source`:

```ts title="vuesho.config.ts"
import { basename } from "node:path";
import { defineCollections } from "vuesho/content";
import { pageSchema } from "vuesho/schema";
import { z } from "zod";

const blog = defineCollections({
  type: "doc",
  dir: "blog",
  // [!code highlight:4]
  schema: ({ path }) =>
    pageSchema.extend({
      date: z.coerce.date().default(new Date(basename(path).slice(0, 10))),
    }),
});
```

The URL keeps the date, like `/blog/2026-01-31-hello`, unless `slugs` in [`createSite()`](/config#createsite) removes it:

```ts title="vuesho.config.ts"
createSite(
  { docs: docs.toSource(), blog: blog.toSource({ baseDir: "blog" }) },
  {
    // `blog/2026-01-31-hello.md` is served at `/blog/hello`
    // [!code highlight:4]
    slugs: (file, next) =>
      file.path.startsWith("blog/")
        ? next().map((slug) => slug.replace(/^\d{4}-\d{2}-\d{2}-/, ""))
        : undefined,
  },
);
```

### JSON Schema [#json-schema]

`vuesho dev`, `vuesho build` and `vuesho prepare` write a JSON Schema for each collection to `node_modules/.vuesho/json-schema`, so your editor can check frontmatter and meta files:

- `content.docs.json` and `content.meta.json` for `defineDocs()` in `content/`
- `blog.json` for a `defineCollections()` in `blog/`, and `content-blog.json` for one in `content/blog/`

Point to a schema with `$schema`, or in your editor's settings:

```json tab="meta.json" title="content/guide/meta.json"
{
  "$schema": "../../node_modules/.vuesho/json-schema/content.meta.json",
  "title": "Guide"
}
```

```json tab="VS Code" title=".vscode/settings.json"
{
  "json.schemas": [
    {
      "fileMatch": ["/content/**/meta*.json"],
      "url": "./node_modules/.vuesho/json-schema/content.meta.json"
    }
  ]
}
```

There's no JSON Schema for a schema written as a function, or one from a library without [Standard JSON Schema](https://standardschema.dev/json-schema) support, like Zod before 4.2.

## Multiple Collections [#multiple-collections]

`defineCollections()` defines a collection of one file type, and takes the page options at its top level. With `type: "doc"`, it reads pages without meta files, like blog posts. Pass your collections to `createSite()` by key, and serve each under a folder with `baseDir`:

```ts title="vuesho.config.ts"
import { createSite } from "vuesho/config";
import { defineCollections, defineDocs } from "vuesho/content";

const docs = defineDocs({ dir: "content/docs" });
const blog = defineCollections({ type: "doc", dir: "content/blog" });

export default createSite({
  docs: docs.toSource(), // [!code highlight]
  blog: blog.toSource({ baseDir: "blog" }), // [!code highlight]
});
```

```files
content/
├── docs/
│   ├── index.md
│   └── install.md
└── blog/
    └── hello.md
```

The docs are at `/` and `/install`, and the post is at `/blog/hello`. Give every source except one a `baseDir`. Two sources with a file at the same path fail the build.

A source's key is the `type` of its pages. Once a layout or a plugin checks `page.type`, TypeScript narrows `page.data` to that source's fields:

```ts
if (page.type === "blog") console.log(page.data.title);
```

On a site with one source, the `type` of every page is `undefined`.

### Sidebar [#sidebar]

The sources share one sidebar, where each `baseDir` is a folder. A [meta file](/navigation#folders) can hide that folder. It can also make the docs a [root folder](/navigation#root-folders), so the sidebar shows only the docs pages:

```json tab="Hide the blog" title="content/docs/meta.json"
{ "pages": ["...", "!blog"] }
```

```json tab="Docs as a root folder" title="content/docs/meta.json"
{ "title": "Docs", "root": true }
```

The [blog plugin](/plugins/blog) adds a post list and a post layout.

## Data Collections [#data-collections]

`type: "meta"` reads JSON, YAML or `meta.ts` files that aren't pages, like the authors of a blog:

```ts title="vuesho.config.ts"
import { defineCollections } from "vuesho/content";
import { z } from "zod";

const authors = defineCollections({
  type: "meta",
  dir: "authors",
  schema: z.object({ name: z.string(), url: z.string().optional() }),
});
```

```yaml title="authors/fuma.yaml"
name: Fuma Nama
url: https://fuma-nama.dev
```

At the config's top level, `toSource().files` works only with YAML or JSON files and a synchronous schema. For a `meta.ts` collection or an async schema, read the entries in a plugin's hook instead. This config passes the authors to the [blog plugin](/plugins/blog):

```ts title="vuesho.config.ts"
const authorsById = Object.fromEntries(
  authors.toSource().files.map((file) => [file.path.replace(/\.yaml$/, ""), file.data]),
);

authorsById.fuma?.name; // "Fuma Nama"
```

## Per-Collection Options [#per-collection-options]

These options apply to one collection's pages. Set them in `docs` of `defineDocs()`, or at the top level of `defineCollections()`:

```ts tab="defineDocs()" title="vuesho.config.ts"
const docs = defineDocs({
  docs: { lastModified: true },
});
```

```ts tab="defineCollections()" title="vuesho.config.ts"
const blog = defineCollections({
  type: "doc",
  dir: "blog",
  lastModified: true,
});
```

### Markdown Options [#markdown-options]

`markdown` replaces the config's [Markdown options](/config#markdown) for the collection's pages. It receives the config's options, so spread them to change only some. The built-in syntax and the plugins' [Markdown extensions](/markdown-extensions) still apply:

```ts title="vuesho.config.ts"
const blog = defineCollections({
  type: "doc",
  dir: "blog",
  markdown: (site) => ({ ...site, imageSize: { external: false } }), // [!code highlight]
});
```

### Components [#components]

`components` sets a separate [components folder](/markdown/components#custom-components) for a collection, so components that only its pages use don't load elsewhere:

```ts title="vuesho.config.ts"
const blog = defineCollections({
  type: "doc",
  dir: "blog",
  components: "src/components/blog", // [!code highlight]
});
```

A file in that folder takes priority over one in the site's `src/components/content/` for the collection's pages. An [included](/markdown#includes) file compiles with the `markdown` options and components of the page that includes it.

### Last Modified Dates [#last-modified-dates]

`lastModified: true` sets `page.data.lastModified` to the date of each file's last commit. The docs layout, the [sitemap](/seo#sitemap) and the RSS feed show it:

```ts title="vuesho.config.ts"
const docs = defineDocs({ docs: { lastModified: true } });
```

A file that Git doesn't track has no date, and a build without `.git` logs a warning.

<Callout type="warn" title="Shallow Clones">
In a shallow clone, like in CI, older pages show the latest commit's date. Fetch the full history, like with `fetch-depth: 0` in `actions/checkout`.
</Callout>

<Accordions>
<Accordion title="Dates from the GitHub API">
When the build has no Git history, a plugin's `getPageLastModified` hook can fetch the dates from the GitHub API:

```ts title="vuesho.config.ts"
import type { Plugin } from "vuesho/config";

const dates = new Map<string, Promise<Date | undefined>>();

async function fetchLastCommit(file: string): Promise<Date | undefined> {
  const url = `https://api.github.com/repos/acme/docs/commits?path=${encodeURIComponent(file)}&per_page=1`;
  const headers = process.env.GITHUB_TOKEN
    ? { Authorization: `Bearer ${process.env.GITHUB_TOKEN}` }
    : undefined;
  const res = await fetch(url, { headers });
  if (!res.ok) return;
  const [commit] = (await res.json()) as { commit: { committer: { date: string } } }[];
  return commit && new Date(commit.commit.committer.date);
}

const githubDates: Plugin = {
  name: "site:github-dates",
  async getPageLastModified(page) {
    const file = await this.getPageFile(page);
    if (!file) return;
    if (!dates.has(file)) dates.set(file, fetchLastCommit(file));
    return dates.get(file);
  },
};

createSite(docs.toSource()).plugins(githubDates);
```

`getPageFile()` returns the path from the project root, so in a monorepo, add the package's folder in front. Without `GITHUB_TOKEN`, GitHub allows 60 requests an hour.
</Accordion>
</Accordions>

## Options [#options]

### DocsOptions

| Prop    | Type                                                            | Description                                             |
| ------- | --------------------------------------------------------------- | ------------------------------------------------------- |
| `dir?`  | `string \| undefined`                                           | The folder, from the project root. Default: `"content"` |
| `docs?` | `DocOptions<PageSchema, boolean> \| undefined`                  |                                                         |
| `meta?` | `MetaOptions<StandardSchemaV1<unknown, MetaData>> \| undefined` |                                                         |

### DocOptions

| Prop            | Type                                                        | Description                                                                                                                                                                                                                                            |
| --------------- | ----------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `files?`        | `string[] \| undefined`                                     | Patterns for the pages, from `dir`. `!` leaves files out. Default: `["**\/*.md"]`                                                                                                                                                                      |
| `schema?`       | `CollectionSchema<PageSchema> \| undefined`                 | The frontmatter schema: a Standard Schema, or a function of the file. Default: `` the fields of `pageSchema` ``                                                                                                                                        |
| `markdown?`     | `((site: MarkdownOptions) => MarkdownOptions) \| undefined` | How the pages compile, in place of `markdown` of the config, which it receives: spread it to build on it, like `(site) => ({ ...site, imageSize: false })`. The plugins' Markdown extensions apply either way. Default: `` `markdown` of the config `` |
| `components?`   | `string \| undefined`                                       | A folder of components of the pages, from the project root, like `src/components/content/`, whose components win over the site's in the collection's pages.                                                                                            |
| `lastModified?` | `boolean \| undefined`                                      | Sets `page.data.lastModified` to each file's last commit date.                                                                                                                                                                                         |
