@kiln-js/md#
@kiln-js/md lets you write pages in Markdown. A .md file in the pages folder becomes a route, just like a .tsx file. The plugin parses the Markdown when Vite builds the page, and the runtime renders it with components you control, so every heading, link, code block, and table can have your own styling.
bun add -d @kiln-js/mdPeer dependencies: vite and react.
Usage#
// 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(),
],
});Put the md() plugin before kiln(). The generated module for a Markdown page is a .tsx file, and the Kiln plugin handles it as a page.
A page file looks like this:
---
title: Getting started
---
# Getting started
Install the CLI with `bun add -d @kiln-js/cli`.The title from the frontmatter sets the page title.
md(options?)#
Returns one Vite plugin. The default export is the same function.
interface MdOptions {
dir?: string; // default "app/pages"
components?: string; // path to a module with the tag components
html?: boolean; // default false
}| Option | Default | Meaning |
|---|---|---|
dir | app/pages | Folder whose .md files become pages. Relative to the Vite root. |
components | none | A module whose default export maps tag names to components. |
html | false | Allow raw HTML inside Markdown. |
With html: false, raw HTML in a Markdown file is shown as text. Turn it on only for content you trust. It is not sanitized.
How a page is built#
For each .md file under dir, the plugin:
- Splits off the frontmatter. Only flat
key: valuelines between---markers are read. Quotes around a value are removed. - Parses the body with
marked. - Gives every heading an id made from its text. Ids are lowercase, punctuation is removed, spaces become hyphens, and repeated headings get
-1,-2, and so on. - Records the headings in a
headingsexport, so a page can build a table of contents. - Removes the raw source text from each token to keep the module small.
- Generates a module that exports
frontmatter,headings, and a default page component.
The page component sets the title from frontmatter.title, when one exists, and renders the tokens.
The module also exports frontmatter and headings, so other code can read them:
import { headings, frontmatter } from "./guide.md";The components module#
The module you pass as components has a default export that maps tag names to React components or to plain HTML tag names:
// app/markdown.tsx
import type { ReactNode } from "react";
import type { MdCodeProps } from "@kiln-js/md/runtime";
export default {
h1: ({ children, id }: { children?: ReactNode; id?: string }) => (
<h1 id={id} className="text-4xl font-bold">{children}</h1>
),
h2: ({ children, id }: { children?: ReactNode; id?: string }) => (
<h2 id={id} className="mt-12 text-2xl font-semibold">{children}</h2>
),
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>
),
};Tags you do not override render as plain HTML.
Tag names#
| Tag | Markdown source |
|---|---|
h1 to h6 | Headings |
p | Paragraphs |
a | Links |
img | Images |
pre | Fenced code blocks. The component receives lang and code. |
code | Inline code |
strong, em, del, br | Bold, italic, strikethrough, line break |
ul, ol, li | Lists |
input | Task list checkboxes |
blockquote, hr | Quotes and horizontal rules |
table, thead, tbody, tr, th, td | Tables |
Props each component receives#
Heading components receive id, the slug generated for them. Link components receive href and title. Image components receive src, alt, and title. Code block components receive lang and code. All components receive children except pre, which gets lang and code.
The default code block renders <pre data-lang="…"><code>…</code></pre>.
Runtime#
The module @kiln-js/md/runtime exports the renderer used by generated pages:
import { Markdown } from "@kiln-js/md/runtime";Its props:
interface MarkdownProps {
tokens: Token[];
components?: MdComponents;
allowHtml?: boolean;
}You normally do not use Markdown yourself. It is useful when you render Markdown from a source that is not a file, such as a database, if you parse it with marked and pass the tokens.
The runtime decodes HTML entities in text, renders code spans and blocks, and renders tables. Nested inline formatting is supported.
Exports#
| Export | From | Purpose |
|---|---|---|
md (default and named) | @kiln-js/md | The Vite plugin. |
MdOptions | @kiln-js/md | Option type. |
Markdown | @kiln-js/md/runtime | The renderer. |
MdComponents, MdTag, MdCodeProps, MarkdownProps, Token | @kiln-js/md/runtime | Types. |
parseMarkdown(src) | internal | Returns frontmatter, tokens, and headings. |
splitFrontmatter(src) | internal | Splits the frontmatter from the body. |
slugify(text) | internal | Makes a heading id. |
Notes#
- The frontmatter parser reads flat keys only. Nested YAML and lists are not supported.
- Keys may contain letters, digits,
_, and-. - Images and links are passed through unchanged. Relative links are not rewritten.
- A Markdown file cannot be an API route. API routes must be
.tsor.jsfiles.

