Skip to Content
Vuesho

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:

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

const docs = defineDocs();

export default createSite(docs.toSource());

defineDocs() validates each page's frontmatter and compiles its Markdown when the site builds.

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:

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 return a page's table of contents and search data.

Prop

Type

In every source, a page's data is validated against the frontmatter fields that plugins declare.

Local Files

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

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

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

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 reads JSON and YAML files too, and validates them with a schema.

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 of a plugin, so the server's output gets them.

Remote APIs

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

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

const response = await fetch("https://cms.example.com/posts"); 
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: 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 shows.

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 shows. These sources have integrations: