Local Markdown
Load Markdown and MDX files from a folder at runtime.
@fumadocs/local-md reads Markdown and MDX files from a folder and compiles them at runtime, with frontmatter, meta files and search data like other content sources.
Unlike Fumadocs MDX, it doesn't go through the bundler, so:
- Images aren't optimized at build time.
- MDX files can't import modules. Pass components when rendering instead.
Setup
npm install @fumadocs/local-md shikiCreate the source, and get its loader from getSource():
import { dynamicLoader } from 'fumadocs-core/source';
import { localMd } from '@fumadocs/local-md';
const docs = localMd({
dir: 'content/docs',
});
const docsLoader = dynamicLoader(docs.dynamicSource(), {
baseUrl: '/docs',
});
export async function getSource() {
return docsLoader.get();
}Rendering Pages
page.data.load() compiles a page and returns its renderer.
Render it in your page:
import { getSource } from '@/lib/source';
import { DocsLayout } from 'fumadocs-ui/layouts/docs';
export default async function Layout({ children }: LayoutProps<'/docs'>) {
const source = await getSource();
return <DocsLayout tree={source.getPageTree()}>{children}</DocsLayout>;
}Hot Reload
During development, a watcher tells the source when content files change.
Run the local-md dev server alongside Next.js, and connect your source to it:
{
"scripts": {
"dev": "local-md dev -- next dev"
}
}import { watchWithDevServer } from '@fumadocs/local-md/dev/ws';
if (process.env.NODE_ENV === 'development') {
void watchWithDevServer(docs);
}Render <DevClient /> in your root layout, so the browser refreshes the page too:
import { RootProvider } from 'fumadocs-ui/provider/next';
import { DevClient } from '@fumadocs/local-md/dev/react-client';
export default function RootLayout({ children }: LayoutProps<'/'>) {
return (
<html lang="en" suppressHydrationWarning>
<body>
<RootProvider>
<DevClient />
{children}
</RootProvider>
</body>
</html>
);
}Configuration
Markdown and MDX files compile with separate plugins, so each format has its own options:
import { localMd } from '@fumadocs/local-md';
import { pageSchema, metaSchema } from 'fumadocs-core/source/schema';
import remarkDirective from 'remark-directive';
const docs = localMd({
dir: 'content/docs',
// validate frontmatter and meta files
frontmatterSchema: pageSchema.extend({
// ...
}),
metaSchema: metaSchema.extend({
// ...
}),
// `.md` files
mdOptions: {
// `<include>` only works in MDX, Markdown uses `::include[./file.md]`
remarkPlugins: [remarkDirective],
},
// `.mdx` files
mdxOptions: {
rehypeCodeOptions: {
themes: { light: 'github-light', dark: 'github-dark' },
},
},
});Code Execution
MDX files run as JavaScript with new Function. Don't put untrusted MDX files in dir. On runtimes without eval(), like Cloudflare Workers, use Markdown files instead.
Markdown files render from the syntax tree, and a virtual JavaScript engine evaluates the JSX that plugins generate. rendererOptions.executor replaces the engine.
Static Source
staticSource() reads the files once, without hot reload, so a plain loader() works:
import { loader } from 'fumadocs-core/source';
export const source = loader(await docs.staticSource(), {
baseUrl: '/docs',
});Migrating from Fumadocs MDX
Replace your lib/source.ts with the one from Setup, then:
- Delete
source.config.ts, and removecreateMDX()fromnext.config.mjs.
- Replace the
sourceobject withawait getSource(). - Set up hot reload.
The page data changes:
const source = await getSource();
const page = source.getPage(slugs);
// custom frontmatter properties
page.data.full;
page.data.frontmatter.full;
// the Markdown source
await page.data.getText('raw');
page.data.content;
// structured data for search
page.data.structuredData;
await page.data.structuredData();
// the rendered content
<page.data.body components={getMDXComponents()} />;
const { body, toc } = await (await page.data.load()).render(getMDXComponents()); How is this guide?
Last updated on
