指南
导出 EPUB
将 Fumadocs 文档导出为 EPUB 格式
EPUB 集成允许将 Fumadocs 文档导出为 EPUB 格式,以便在电子阅读器上阅读。该系统将你的 MDX 内容转换为 HTML,解析图片,并生成符合标准的带有目录的 EPUB 文件。
设置
安装
npm install fumadocs-epub启用处理后的 Markdown
配置你的文档集合以包含处理后的 Markdown:
import { defineDocs } from 'fumadocs-mdx/config';
export const docs = defineDocs({
docs: {
postprocess: {
includeProcessedMarkdown: true,
},
},
});创建导出路由
为 Next.js 实现路由处理器来提供 EPUB 文件:
import { source } from '@/lib/source';
import { exportEpub } from 'fumadocs-epub';
export const revalidate = false;
export async function GET(): Promise<Response> {
const buffer = await exportEpub({
source,
title: 'My Documentation',
author: 'My Team',
description: 'Documentation for my project',
cover: '/cover.png',
});
return new Response(new Uint8Array(buffer), {
headers: {
'Content-Type': 'application/epub+zip',
'Content-Disposition': 'attachment; filename="docs.epub"',
},
});
}在生产环境中,请通过要求授权头和设置环境变量来保护 /export/epub 端点。
CLI 使用
通过命令行导出 EPUB:
fumadocs export epub --framework next此方法从你运行的服务器(Next.js)获取内容,或使用构建输出(其他框架)。非 Next.js 框架需要先进行生产构建。
配置选项
| 选项 | 类型 | 描述 |
|---|---|---|
title | string | 必填,书名 |
author | string | string[] | 作者名,默认为 'anonymous' |
description | string | 书籍描述 |
language | string | 语言代码(如 'en'),默认为 'en' |
publisher | string | 出版者名称,默认为 'anonymous' |
isbn | string | ISBN 编号 |
cover | string | 封面图片路径 |
outputPath | string | 将 EPUB 写入文件 |
includePages | (page) => boolean | 包含匹配条件的页面 |
excludePages | (page) => boolean | 排除匹配条件的页面 |
css | string | 自定义 CSS 样式 |
publicDir | string | 公共目录路径,默认为 ./public |
自定义 CSS
通过导入并扩展默认样式来覆盖 EPUB 样式:
import { exportEpub, defaultEpubStyles } from 'fumadocs-epub';
const buffer = await exportEpub({
source,
title: 'My Docs',
author: 'Me',
css: `${defaultEpubStyles}
/* 自定义覆盖 */
body { font-size: 1.1em; }
`,
});页面过滤
控制导出哪些文档页面:
const buffer = await exportEpub({
source,
title: 'My Docs',
author: 'Me',
includePages: (page) => page.path.startsWith('getting-started'),
excludePages: (page) => page.path === 'changelog',
});图片解析
系统自动处理图片:
- 相对路径 — 相对于页面文件位置解析
- 公共路径 — 从公共目录解析
- 远程 URL — 直接嵌入 EPUB