# Content Sources

Where your pages come from.

A content source holds your site's pages, and their folders' meta files. The default source is the Markdown in `content/`, which `defineDocs()` from `vuesho/content` reads:

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

const docs = defineDocs();

export default createSite(docs.toSource());
```

[`defineDocs()`](/markdown/collections) validates each page's frontmatter and compiles its Markdown when the site builds.

## Custom Sources [#custom-sources]

A source is an object with a `files` array. Each file is a page or a meta file, with a `path` and `data`. For example, a list of releases can become a changelog:

```ts title="vuesho.config.ts"
import { createSite, type Source } from "vuesho/config";

const releases = [
  { version: "0.2.0", notes: "<p>Custom content sources.</p>" },
  { version: "0.1.0", notes: "<p>The first release.</p>" },
];

const changelog: Source<{ pageData: { title: string; html: string }; metaData: {} }> = {
  files: releases.map(({ version, notes }) => ({
    // "page", or "meta" for a folder's meta file.
    type: "page",
    // The path sets the URL, like a file in `content/`: `/0.2.0`.
    path: `${version}.md`,
    // The page's frontmatter, which layouts read from `page.data`.
    data: { title: `v${version}`, html: notes },
  })),
};

export default createSite(changelog).plugins({
  name: "site:changelog",
  // Each page's body.
  getPageBody: (page) => ({ html: page.data.html }),
});
```

A page renders the body that a plugin's `getPageBody` hook returns, or nothing when no plugin returns one. The browser receives `data` as JSON, so keep functions and secrets out of it. Other [content hooks](/content-hooks) return a page's table of contents and search data.

### SourceFile

| Prop            | Type                                                                                                                                                 | Description                                                                                    |
| --------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------- |
| `path`          | `string`                                                                                                                                             | The path in the source, like `guide/install.md`, which sets a page's URL.                      |
| `absolutePath?` | `string \| undefined`                                                                                                                                | The file's absolute path on disk, when it comes from the file system.                          |
| `locale?`       | `string \| undefined`                                                                                                                                | The file's language with `i18n`, or `$` for every language. Default: `read from the path`      |
| `type`          | `"page"`                                                                                                                                             |                                                                                                |
| `slugs?`        | `string[] \| undefined`                                                                                                                              | Slugs that replace the ones from the path.                                                     |
| `data`          | `SourcePageData`                                                                                                                                     | The page's data. The browser receives it as JSON, except fields with symbol keys.              |
| `dataAt?`       | `((params: Record<string, string>, custom?: { props: Record<string, unknown>; data: SourcePageData; } \| undefined) => SourcePageData) \| undefined` | The data of a dynamic file's page at `params`, with the props from `getPage(path, { props })`. |

In every source, a page's `data` is validated against the [frontmatter fields](/content-hooks#frontmatter-fields) that plugins declare.

### Local Files [#local-files]

`vuesho.config.ts` runs through Vite, so `import.meta.glob()` reads local JSON files when the site builds:

```ts title="vuesho.config.ts"
import { createSite, type Source } from "vuesho/config";

interface Release {
  version: string;
  notes: string;
}

// [!code highlight:4]
const releases = import.meta.glob<Release>("./releases/*.json", {
  eager: true,
  import: "default",
});

const changelog: Source<{ pageData: { title: string; html: string }; metaData: {} }> = {
  files: Object.values(releases).map(({ version, notes }) => ({
    type: "page",
    path: `${version}.md`,
    data: { title: `v${version}`, html: notes },
  })),
};
```

To read a YAML file, import it with `query: "?raw"` and parse it with a library like `yaml`. A [data collection](/markdown/collections#data-collections) reads JSON and YAML files too, and validates them with a schema.

<Callout type="warn" title="Reading Files With node:fs">
A server runs `vuesho.config.ts` again when it starts, so it reads the files again. Import them with `import.meta.glob()`, which bundles them, or list them in the [`serverFiles`](/plugin-api#server-files) of a plugin, so the server's output gets them.
</Callout>

### Remote APIs [#remote-apis]

Top-level `await` fetches the data when the site builds, and a server fetches it again when it starts:

```ts title="vuesho.config.ts"
import { createSite, type Source } from "vuesho/config";

const response = await fetch("https://cms.example.com/posts"); // [!code highlight]
const posts: { slug: string; title: string; html: string }[] = await response.json();

const cms: Source<{ pageData: { title: string; html: string }; metaData: {} }> = {
  files: posts.map((post) => ({
    type: "page",
    path: `${post.slug}.md`,
    data: { title: post.title, html: post.html },
  })),
};

export default createSite(cms).plugins({
  name: "site:cms",
  getPageBody: (page) => ({ html: page.data.html }),
});
```

As a function, `files` runs once when the site starts if the source has `render: "static"`. Without it, the source is [dynamic](/dynamic-rendering#dynamic-sources): the server runs `files` again while the site runs, so the pages change without a new build.

When the API returns Markdown, a plugin renders it with markdown-it, as [Rendering Markdown](/content-hooks#rendering-markdown) shows.

## Integrations [#integrations]

A site can combine sources, like the Markdown in `content/` and a CMS's pages, each under its own URL prefix, as [Multiple Collections](/markdown/collections#multiple-collections) shows. These sources have integrations:

<Cards>
<Card title="Notion" href="/content-sources/notion">
The pages of a Notion database.
</Card>

<Card title="Sanity" href="/content-sources/sanity">
The documents of a Sanity dataset, with previews.
</Card>

<Card title="BaseHub" href="/content-sources/basehub">
The blocks of a BaseHub repository.
</Card>

<Card title="Obsidian" href="/content-sources/obsidian">
An Obsidian vault, as it is.
</Card>

<Card title="Local Files" href="/content-sources/local-files">
A folder on the server, like generated reports or HTML files.
</Card>
</Cards>
