跳至內容
Fumadocs

手動安裝

將 Fumadocs 加入現有的 Next.js 專案。

您也可以使用 Fumadocs CLI 自動完成設定:

npx @fumadocs/cli init

先決條件

繼續之前,請確認您已設定好:

  • Next.js 16。
  • Tailwind CSS 4。

本指南使用 Fumadocs MDX 作為內容來源,請先完成設定:

Installation

npm i fumadocs-mdx fumadocs-core @types/mdx

Add the plugin to Next.js config:

next.config.mjs
import { createMDX } from 'fumadocs-mdx/next';

/** @type {import('next').NextConfig} */
const config = {
  reactStrictMode: true,
};

const withMDX = createMDX();

export default withMDX(config);

ESM Only

Fumadocs MDX is ESM-only, it's recommended to use next.config.mjs for accurate ESM resolution.

For TypeScript config file, it requires Native Node.js TypeScript Resolver, you can see Next.js docs for details.

Integrate with Fumadocs

Create a lib/source.ts file:

lib/source.ts
import { defineDocs } from 'fumadocs-mdx/macro';
import { loader } from 'fumadocs-core/source';

const docs = defineDocs({
  dir: 'content/docs',
});

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

Done

You can now write content in content/docs folder.

小提示

Fumadocs 也支援其他內容來源,包括 Content Collections 與無頭式 CMS。

開始使用

安裝套件:

npm i fumadocs-core fumadocs-ui@npm:@fumadocs/base-ui

與範本相同,此指令會將 fumadocs-ui 安裝為 @fumadocs/base-ui 的別名,也就是 Fumadocs UI 的 Base UI 版本。若要使用 Radix UI 版本,請直接安裝 fumadocs-ui,不要使用別名。

樣式

請將以下內容加入您的 Tailwind CSS 檔案:

@import 'tailwindcss';
@import 'fumadocs-ui/css/neutral.css';
@import 'fumadocs-ui/css/preset.css';

Fumadocs UI 沒有預設字型,請從 next/font 中選擇一款。

Root Provider

請用 Root Provider 包住整個應用程式,並為 body 加上必要的樣式。

app/layout.tsx
import { RootProvider } from 'fumadocs-ui/provider/next';
import type { ReactNode } from 'react';

export default function Layout({ children }: { children: ReactNode }) {
  return (
    <html lang="en" suppressHydrationWarning>
      <body
        // 必要的樣式
        className="flex flex-col min-h-screen"
      >
        <RootProvider>{children}</RootProvider>
      </body>
    </html>
  );
}

路由

請將各版面配置共用的選項放在同一個檔案中:

lib/layout.shared.tsx
import type { BaseLayoutProps } from 'fumadocs-ui/layouts/shared';

export function baseOptions(): BaseLayoutProps {
  return {
    nav: {
      title: 'My App',
    },
  };
}

請建立下列檔案:

import defaultMdxComponents from 'fumadocs-ui/mdx';
import type { MDXComponents } from 'mdx/types';

export function getMDXComponents(components?: MDXComponents) {
  return {
    ...defaultMdxComponents,
    ...components,
  } satisfies MDXComponents;
}

export const useMDXComponents = getMDXComponents;

declare global {
  type MDXProvidedComponents = ReturnType<typeof getMDXComponents>;
}

搜尋路由採用 ZBSearch,設定方式請參閱文件搜尋。

完成

啟動開發伺服器,即可在 content/docs 中撰寫您的文件:

content/docs/index.mdx
---
title: Hello World
---

## Introduction

I love Fumadocs.

How is this guide?

最後更新於