
slugtree integrates with Next.js through a plugin and a React provider. The plugin runs at build time to generate content data, and the provider makes that data available to all components in your app.

## Plugin

Add `withSlugtree` to your `next.config.ts`:

```ts
import type { NextConfig } from 'next'
import { withSlugtree } from 'slugtree/next'

const nextConfig: NextConfig = {}

export default withSlugtree(nextConfig)
```

The plugin scans your content directory and writes generated files into the package. It also starts a file watcher in development mode so the tree stays in sync as you add or edit files.

### Plugin options

| Option       | Type     | Default                              | Description                                                                                      |
| ------------ | -------- | ------------------------------------ | ------------------------------------------------------------------------------------------------ |
| `basePath`   | `string` | `'/'`                                | The URL prefix for all documentation routes. All node `href` values are prefixed with this path. |
| `contentDir` | `string` | `'./src/content'` (or `'./content'`) | Path to your content directory, relative to the project root.                                    |
| `outputDir`  | `string` | `'.slugtree'`                        | Path to the output directory where generated metadata files are saved.                           |

## Provider

Wrap your root layout with `SlugtreeProvider` so all client hooks can access the data:

```tsx
import { SlugtreeProvider } from 'slugtree/react'

export default function RootLayout({
  children
}: {
  children: React.ReactNode
}) {
  return (
    <SlugtreeProvider>
      <html lang="en">
        <body>{children}</body>
      </html>
    </SlugtreeProvider>
  )
}
```

`SlugtreeProvider` reads the generated data and injects it into React Context. It also supports hot reloading in development.

## Catch-all route

Create a catch-all route at `app/docs/[[...slug]]/page.tsx` to render any documentation page:

```tsx
import { getNodeData, getSlugs } from 'slugtree'
import { notFound } from 'next/navigation'
import { MDXRemote } from 'next-mdx-remote/rsc'

interface PageProps {
  params: Promise<{ slug?: string[] }>
}

export default async function DocPage({ params }: PageProps) {
  const { slug = [] } = await params
  const data = getNodeData(slug)

  if (!data) notFound()

  return (
    <article>
      <h1>{data.frontMatter.title}</h1>
      <MDXRemote source={data.rawContent} />
    </article>
  )
}

export function generateStaticParams() {
  return getSlugs().map((slug) => ({ slug }))
}
```

`getNodeData` returns the full `NodeData` object including `rawContent`, `frontMatter`, and `toc`. Pass `rawContent` to your MDX renderer of choice (`next-mdx-remote`, `@mdx-js/react`, etc.).

## Static exports

When using `output: 'export'` in Next.js, `getSlugs()` returns all valid route arrays:

```ts
import { getSlugs } from 'slugtree'

export function generateStaticParams() {
  return getSlugs().map((slug) => ({ slug }))
}
```

## Generating metadata

Use `getNodeData` or `getNode` inside `generateMetadata` to produce per-page `<title>` and `<meta>` tags automatically:

```tsx
import { getNodeData } from 'slugtree'
import type { Metadata } from 'next'

interface PageProps {
  params: Promise<{ slug?: string[] }>
}

export async function generateMetadata({
  params
}: PageProps): Promise<Metadata> {
  const { slug = [] } = await params
  const data = getNodeData(slug)

  if (!data) {
    return {
      title: 'Documentation'
    }
  }

  return {
    title: data.frontMatter.title,
    description: data.frontMatter.description
  }
}
```
