Skip to content

Markdown pages

Some pages are mostly prose: documentation, articles, and policies. Kiln lets you write those in Markdown. A .md file under app/pages becomes a route, and you control how every Markdown element is displayed through one components module.

Setup

Install the Markdown package and add it to Vite:

bash
bun add -d @kiln-js/md
ts
// vite.config.ts
import { defineConfig } from "vite";
import { md } from "@kiln-js/md";
import { kiln } from "@kiln-js/vite-plugin";

export default defineConfig({
  plugins: [
    md({ dir: "app/pages", components: "app/markdown.tsx" }),
    ...kiln(),
  ],
});

The dir option names the folder whose Markdown files become pages. The components option names the module that styles them. Keep the md() plugin before kiln().

Writing a page

markdown
---
title: Getting started
---

# Getting started

Install the CLI with `bun add -d @kiln-js/cli`, then run `bunx kiln create my-app`.

## Next steps

- Read the [routing guide](/guide/routing).
- Try the example in the quick start.

The file app/pages/getting-started.md is served at /getting-started.

Frontmatter

A block at the top of the file between --- lines holds page settings. Kiln reads flat key: value lines only:

markdown
---
title: "Deploying your app"
---
  • title sets the document title. Quotes around the value are removed.
  • Other keys are kept in the page module's frontmatter export and are not used by Kiln.

Nested keys and lists are not supported. Keys may contain letters, digits, _, and -.

Headings and anchors

Every heading gets an id made from its text. The id is lowercase, punctuation is removed, and spaces become hyphens:

markdown
## Install the CLI!

gets the id install-the-cli. A second heading with the same text gets install-the-cli-1. Links such as [install](#install-the-cli) jump to it.

Each page also exports its headings, so you can build a table of contents:

tsx
import { headings } from "./getting-started.md";

export function Toc() {
  return (
    <ul>
      {headings.map((h) => (
        <li key={h.id} style={{ marginLeft: (h.depth - 1) * 12 }}>
          <a href={`#${h.id}`}>{h.text}</a>
        </li>
      ))}
    </ul>
  );
}

Each heading entry has depth (1 to 6), text, and id.

Styling tags

The module named by components has a default export that maps tag names to components:

tsx
// app/markdown.tsx
import type { ReactNode } from "react";
import type { MdCodeProps } from "@kiln-js/md/runtime";

const Heading = (level: 1 | 2 | 3) => {
  const Tag = `h${level}` as const;
  return ({ id, children }: { id?: string; children?: ReactNode }) => (
    <Tag id={id} className={level === 1 ? "text-4xl font-bold" : "mt-10 text-2xl font-semibold"}>
      {children}
    </Tag>
  );
};

export default {
  h1: Heading(1),
  h2: Heading(2),
  h3: Heading(3),
  a: ({ href, children }: { href?: string; children?: ReactNode }) => (
    <a href={href} className="text-brand underline">{children}</a>
  ),
  pre: ({ lang, code }: MdCodeProps) => (
    <pre data-lang={lang}><code>{code}</code></pre>
  ),
};

Any tag you do not override renders as plain HTML. A value can be a component or a string. A string such as "section" replaces the tag name without changing its props.

The tags you can override are: h1 through h6, p, a, img, pre, code, strong, em, del, br, ul, ol, li, input, blockquote, hr, table, thead, tbody, tr, th, and td.

Props by tag

  • Headings receive id and children.
  • Links receive href, title, and children.
  • Images receive src, alt, and title.
  • Code blocks (pre) receive lang and code. lang is the word after the opening fence, such as ts, or undefined.
  • Other tags receive children.

Code blocks

Fenced code blocks are passed to the pre component as a string. Kiln does not highlight syntax. Add a highlighter in the component, or in a build step, if you want colors:

markdown
```ts
export const x = 1;
```

Inline code uses the code tag.

Raw HTML

By default raw HTML in a Markdown file is shown as text. To allow it, set html: true:

ts
md({ dir: "app/pages", components: "app/markdown.tsx", html: true })

Only enable this for files you control. Kiln does not sanitize HTML.

Layouts

Markdown pages use the same layouts as .tsx pages. A _layout.tsx in the same folder wraps them. This makes it easy to give a documentation folder a sidebar:

text
app/pages/docs/_layout.tsx
app/pages/docs/getting-started.md
app/pages/docs/routing.md

Render mode

Markdown pages are universal pages. They render on the server and hydrate in the browser. To render a Markdown page with no JavaScript, you would need a server page, which Markdown pages do not support in this version. Keep the components free of browser-only code so the server render succeeds.

Link to other pages with normal Markdown links: [routing](/guide/routing). Links are rendered as anchors. Client-side navigation uses the anchor's default behavior, so a plain <a> causes a full page load. To get client navigation, override the a tag and render Link from @kiln-js/client inside it.

tsx
import { Link } from "@kiln-js/client";

export default {
  a: ({ href, children }: { href?: string; children?: ReactNode }) =>
    href?.startsWith("/") ? <Link href={href}>{children}</Link> : <a href={href}>{children}</a>,
};

Limits

  • Frontmatter supports flat keys only.
  • Relative image and link paths are passed through as written. Put images in public or import them from a component.
  • Markdown files cannot be API routes.