Skip to content

@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.

bash
bun add -d @kiln-js/md

Peer dependencies: vite and react.


Usage

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(),
  ],
});

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:

markdown
---
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.

ts
interface MdOptions {
  dir?: string;        // default "app/pages"
  components?: string; // path to a module with the tag components
  html?: boolean;      // default false
}
OptionDefaultMeaning
dirapp/pagesFolder whose .md files become pages. Relative to the Vite root.
componentsnoneA module whose default export maps tag names to components.
htmlfalseAllow 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:

  1. Splits off the frontmatter. Only flat key: value lines between --- markers are read. Quotes around a value are removed.
  2. Parses the body with marked.
  3. 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.
  4. Records the headings in a headings export, so a page can build a table of contents.
  5. Removes the raw source text from each token to keep the module small.
  6. 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:

tsx
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:

tsx
// 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

TagMarkdown source
h1 to h6Headings
pParagraphs
aLinks
imgImages
preFenced code blocks. The component receives lang and code.
codeInline code
strong, em, del, brBold, italic, strikethrough, line break
ul, ol, liLists
inputTask list checkboxes
blockquote, hrQuotes and horizontal rules
table, thead, tbody, tr, th, tdTables

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:

ts
import { Markdown } from "@kiln-js/md/runtime";

Its props:

ts
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

ExportFromPurpose
md (default and named)@kiln-js/mdThe Vite plugin.
MdOptions@kiln-js/mdOption type.
Markdown@kiln-js/md/runtimeThe renderer.
MdComponents, MdTag, MdCodeProps, MarkdownProps, Token@kiln-js/md/runtimeTypes.
parseMarkdown(src)internalReturns frontmatter, tokens, and headings.
splitFrontmatter(src)internalSplits the frontmatter from the body.
slugify(text)internalMakes 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 .ts or .js files.