# Content: collections, Markdown, MDX and Markdoc Content collections keep Markdown, MDX, Markdoc and data files under `content/`, validate their frontmatter with a schema, and give loaders typed entries. Markdown is rendered at build time, in Rust, with heading anchors and code highlighting, so no Markdown library runs on the server or in the browser. ## Define a collection Each directory under `content/` is a collection, declared as a named export of `content/config.ts` (or `.js` / `.mjs` / `.mts`): ``` content/ config.ts blog/ hello.md deep-dive.mdx authors/ ada.json ``` ```ts // content/config.ts import { z } from "zod"; import { defineCollection } from "@nowaki-dev/runtime/content"; export const blog = defineCollection({ schema: z.object({ title: z.string(), date: z.coerce.date(), draft: z.boolean().default(false), }), }); export const authors = defineCollection({ type: "data", schema: z.object({ name: z.string(), url: z.string().url().optional() }), }); ``` - `type: "markdown"` (the default) reads `.md` and `.mdx` files (and `.mdoc` with the Markdoc plugin) with YAML (`---`) or TOML (`+++`) frontmatter. `type: "data"` reads `.json` and `.yaml` files. - `schema` is any Standard Schema (Zod, Valibot, ArkType, or a hand-written one as in `examples/hello/content/config.ts`). An invalid entry fails `nowaki build`, listing each problem as `content/blog/x.md: title: ...`. In `nowaki dev` the error shows in the overlay and the server keeps running. - A directory under `content/` without an export in `content/config.ts` is an error naming the export to add. - TOML datetimes and YAML dates arrive as strings: coerce them in the schema (`z.coerce.date()`) to get `Date`s. - `nowaki typegen` (also run by `dev` and `build`) types each collection by its schema's output, so `entry.data` is typed. ## Read entries ```tsx // routes/blog/index.tsx import { getCollection } from "@nowaki-dev/runtime/content"; import type { PageProps } from "@nowaki-dev/runtime"; export async function loader() { const posts = await getCollection("blog", (p) => !p.data.draft); return posts .sort((a, b) => b.data.date.getTime() - a.data.date.getTime()) .map((p) => ({ slug: p.slug, title: p.data.title })); } export default function Blog({ data }: PageProps) { return ; } ``` ```tsx // routes/blog/[slug].tsx import { h } from "preact"; import { getCollection, getEntry } from "@nowaki-dev/runtime/content"; import type { LoaderContext } from "@nowaki-dev/runtime"; type Data = { title: string; html: string; Content?: (props?: Record) => unknown }; export const staticPaths = async () => (await getCollection("blog")).map((p) => ({ slug: p.slug })); export async function loader(ctx: LoaderContext) { const post = await getEntry("blog", String(ctx.params.slug)); if (!post) return ctx.redirect("/blog"); const mod = post.render ? await post.render() : null; // MDX / Markdoc return { title: post.data.title, html: post.html ?? "", Content: mod?.Content } satisfies Data; } // The loader can also return a redirect, so the page types `data` itself. export default function Post({ data }: { data: Data }) { return (

{data.title}

{data.Content ? h(data.Content as never, null) :
}
); } ``` - `getCollection(name, filter?)` returns every entry sorted by slug. `filter` is a function or `{ filter, locale }`. - `getEntry(name, slug)` returns one entry or `undefined`. - Both throw for an unknown collection name. - An entry has `collection`, `slug` (its path under the collection without the extension: `posts/hello`), `file`, `data`, and for Markdown `body`, `html` and `headings`. MDX and Markdoc entries have `render()`, which returns `{ Content, headings }`. Working examples: `examples/hello/content/`, `examples/hello/routes/docs/index.tsx` and `[slug].tsx`, `examples/hello/routes/feed.xml.ts`. ## Markdown Markdown entries are rendered by comrak with GitHub Flavored Markdown: tables, task lists, strikethrough, footnotes and autolinks. - Every heading gets an anchor id, and `entry.headings` (`{ depth, text, id }[]`) lists them for a table of contents. - Fenced code with a language (` ```tsx `) is highlighted at build time with inline styles, so it needs no client JS or theme CSS and works under an enforced CSP. A block without a language, or with an unknown one, stays plain text. - The highlight colors follow the page's color scheme: each one is written as the light color (GitHub light) followed by `light-dark(, )` with its GitHub-dark counterpart. For dark code blocks, set `color-scheme: dark` on the page (or `color-scheme: light dark` to follow the OS setting), e.g. `:root { color-scheme: light dark; }`. A page without `color-scheme` renders light code blocks, as before. `light-dark()` is Baseline 2024 (Chrome and Edge 123, Firefox 120, Safari 17.5); an older browser ignores it and keeps the light colors. - `![alt](./hero.jpg "title")` with a local file becomes a responsive `` like [``](./images-and-fonts.md); a missing file fails the build. Root-relative (`/logo.svg`) and remote URLs stay as written, except remote ones allowed by `images.remotePatterns`, which are optimized too. - **Raw HTML in Markdown is passed through.** Treat content files as code you trust. An inline `