# Fumadocs (Framework Mode, Astro): Local Markdown
URL: /docs/astro/integrations/content/local-md
Source: https://raw.githubusercontent.com/fuma-nama/fumadocs/refs/heads/main/apps/docs/content/docs/(framework)/[framework]/integrations/content/local-md.mdx

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 Astro content collections, 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 [#setup]

```package-install
@fumadocs/local-md shiki
```

Create the source, and get its [loader](/docs/headless/source-api) from `getSource()`:

```ts title="lib/source.ts"
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 [#rendering-pages]

`page.data.load()` compiles a page and returns its renderer.

Serialize it in the page, and render it in a React island:

```astro tab="src/pages/docs/[...slug].astro"
---
import type { GetStaticPaths, InferGetStaticPropsType } from 'astro';
import Layout from '@/layouts/Layout.astro';
import { Docs } from '@/components/docs';
import { getSource } from '@/lib/source';

export const prerender = true;

export const getStaticPaths = (async () => {
  const source = await getSource();
  return source.getPages().map((page) => ({
    params: { slug: decodeURI(page.slugs.join('/')) || undefined },
    props: { page },
  }));
}) satisfies GetStaticPaths;

type Props = InferGetStaticPropsType<typeof getStaticPaths>;

const { page } = Astro.props;
const source = await getSource();
const pageTree = await source.serializePageTree(source.getPageTree());
const { serialize } = await page.data.load();
---

<Layout title={page.data.title}>
  <Docs
    client:load
    pathname={Astro.url.pathname}
    params={Astro.params}
    tree={pageTree}
    title={page.data.title}
    render={serialize()}
  />
</Layout>
```

```tsx tab="src/components/docs.tsx"
import { useMemo } from 'react';
import { type SerializedPageTree, useFumadocsLoader } from 'fumadocs-core/source/client';
import { DocsLayout } from 'fumadocs-ui/layouts/docs';
import { DocsBody, DocsPage, DocsTitle } from 'fumadocs-ui/layouts/docs/page';
import { rendererFromSerialized } from '@fumadocs/local-md/client';
import type { MarkdownRendererSerializedOptions } from '@fumadocs/local-md';
import { Provider, type ProviderProps } from '@/components/provider';
import { getMDXComponents } from '@/components/mdx';

export interface DocsProps extends ProviderProps {
  tree: SerializedPageTree;
  title: string;
  // the syntax tree, rendered in the island
  render: MarkdownRendererSerializedOptions;
}

export function Docs({ tree, title, render, ...props }: DocsProps) {
  const data = useFumadocsLoader({ tree });
  const renderer = useMemo(() => rendererFromSerialized(render), [render]);
  const { body, toc } = renderer.renderSync(getMDXComponents());

  return (
    <Provider {...props}>
      <DocsLayout tree={data.tree}>
        <DocsPage toc={toc}>
          <DocsTitle>{title}</DocsTitle>
          <DocsBody>{body}</DocsBody>
        </DocsPage>
      </DocsLayout>
    </Provider>
  );
}
```

### Hot Reload [#hot-reload]

During development, a watcher tells the source when content files change.

Add the Vite plugin, and connect your source to it:

```js tab="astro.config.mjs"
import { defineConfig } from 'astro/config';
import { localContentPlugin } from '@fumadocs/local-md/dev/vite';

export default defineConfig({
  vite: {
    // reloads the browser after content changes, `{ reload: false }` to opt out
    plugins: [localContentPlugin()],
  },
});
```

```ts tab="lib/source.ts"
import { watchWithVite } from '@fumadocs/local-md/dev/vite';

if (import.meta.env.DEV) {
  watchWithVite(docs);
}
```

## Configuration [#configuration]

Markdown and MDX files compile with separate plugins, so each format has its own options:

```ts title="lib/source.ts"
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 [#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.

### Static Source [#static-source]

`staticSource()` reads the files once, without hot reload, so a plain `loader()` works:

```ts title="lib/source.ts"
import { loader } from 'fumadocs-core/source';

export const source = loader(await docs.staticSource(), {
  baseUrl: '/docs',
});
```