Fondu

MDX pages

View as Markdown

A page can be an .mdx file: Markdown, with JSX and imported React components. It suits docs and articles; this site's docs are MDX pages.

Setup

Add @mdx-js/rollup and put it first in vite.config.ts:

npm install -D @mdx-js/rollup
// vite.config.ts
import mdx from '@mdx-js/rollup'

export default defineConfig({
  plugins: [{ enforce: 'pre', ...mdx() }, fondu({ target: 'node' })],
})

A page

{/* app/routes/guide/intro.mdx: /guide/intro */}
import { Counter } from '#/components/counter.tsx'

# Introduction

Some **Markdown**, and a component: <Counter />

An MDX file in app/routes/ is a page at its URL, rendered on GET. It has no handlers: a _middleware.ts around it can share props, which are in props ({props.title} in the Markdown), and set prerender.

Layouts and components

The page renders inside the _layout.tsx files above it, as any page does (Layouts). To change what the Markdown becomes (a link that opens another site in a new tab, a styled table), use MDX's providerImportSource with @mdx-js/react's MDXProvider in that layout.

In development, an edit to an MDX page updates it in place (React Fast Refresh), as for a .tsx page. An MDX page can't export handlers (loader, action, actions, the HTTP methods), prerender or params: the app stops, and a prerender goes in a .tsx route or a _middleware.ts. Any other export is allowed (a table of contents a rehype plugin adds, say), but Fondu may reserve new export names in a minor release.

Remark and rehype plugins go in mdx()'s options: this site uses remark-gfm for tables, rehype-slug for heading ids and @shikijs/rehype to highlight code at build time.

Last updated