中文文档 | API Reference | Architecture | Experience & Lessons
rebook is a TypeScript e-book toolkit for building fast readers and book workflows across browser, Mini Program, Node.js, and worker-like runtimes.
It parses EPUB, MOBI/AZW3, FB2, and CBZ into a normalized Book contract. Browser and WeChat Mini Program renderers, exporters, search, plugins, and AI-oriented document workflows all build on that same contract.
- Fast page turns: rebook lays sections out into Pretext-backed line ranges, then renders only the visible lines. Normal page turns update a page index and a small DOM/snapshot window instead of reflowing a full chapter DOM or iframe.
- Cross-platform rendering: browser rendering uses lightweight DOM rows; WeChat Mini Program rendering emits serializable snapshots for WXML.
- Multi-format parsing: EPUB 2/3, MOBI/AZW/AZW3, FictionBook 2, and CBZ.
- Environment-agnostic parsers: parser adapters make the same parser code run in browsers, Node.js, Mini Programs, and workers.
- Modular architecture: parsers, renderers, exporters, plugins, and adapters are independent.
- AI-ready content model: sections can expose structured blocks, styled segments, searchable text, and a mutable document tree.
- Built-in workflow pieces: search, first-section export, trial-reading limits, TTS playback hooks, professional translation pipelines, and an MCP server.
npm install rebookimport { registry, createReader } from 'rebook'
import { epub } from 'rebook/parsers/epub'
import { mobi } from 'rebook/parsers/mobi'
import { fb2 } from 'rebook/parsers/fb2'
import { cbz } from 'rebook/parsers/cbz'
registry.register('epub', epub)
registry.register('mobi', mobi)
registry.register('fb2', fb2)
registry.register('cbz', cbz)
const reader = createReader({
container: document.getElementById('viewer')!,
layout: 'paginated',
maxColumnCount: 2,
styles: {
fontSize: '18px',
lineHeight: 1.7,
minColumnWidth: '320px',
maxColumnWidth: '720px',
margin: '32px',
},
})
const book = await reader.open(file)
await reader.next()
await reader.goTo('chapter.xhtml#section')Use the Mini Program reader when there is no DOM. It installs Mini Program parser adapters by default and can use wx.createOffscreenCanvas for text measurement.
import { registry } from 'rebook'
import { epub } from 'rebook/parsers/epub'
import { createWechatMiniProgramReader } from 'rebook/renderers/wechat-miniprogram'
registry.register('epub', epub)
const fs = wx.getFileSystemManager()
const arrayBuffer = fs.readFileSync(filePath) as ArrayBuffer
const unitlessStyles = new Set(['fontWeight', 'opacity', 'zIndex'])
const toStyleText = (style: Record<string, string | number> = {}) =>
Object.entries(style)
.map(([key, value]) => {
const cssKey = key.replace(/[A-Z]/g, match => `-${match.toLowerCase()}`)
const cssValue = typeof value === 'number' && !unitlessStyles.has(key)
? `${value}px`
: String(value)
return `${cssKey}:${cssValue}`
})
.join(';')
const reader = createWechatMiniProgramReader({
width: 375,
height: 667,
wx,
layout: 'paginated',
styles: { fontSize: '18px', lineHeight: 1.7 },
setData: snapshot => this.setData({
reader: {
...snapshot,
lines: snapshot.lines.map(line => ({
...line,
styleText: toStyleText(line.style),
fragments: 'fragments' in line
? line.fragments.map(fragment => ({
...fragment,
styleText: toStyleText(fragment.style),
}))
: undefined,
})),
},
}),
})
await reader.open(arrayBuffer)
await reader.next()Render reader.lines from the snapshot in WXML. Each line node includes layout style and text/image/table data. Convert style objects to CSS strings before passing them to WXML. See API Reference: WeChat Mini Program Reader for a fuller integration example.
rebook-mcp exposes a local book to AI assistants through the Model Context Protocol. It supports EPUB, MOBI/AZW3, FB2, and CBZ.
{
"mcpServers": {
"book": {
"command": "npx",
"args": ["-y", "--package", "rebook", "rebook-mcp", "/absolute/path/book.epub"]
}
}
}Built-in tools include chapter listing, chapter text reading, metadata lookup, and full-book or chapter-scoped search. See MCP Tools for embedding APIs.
| Format | Extensions |
|---|---|
| EPUB 2/3 | .epub |
| Mobipocket / Kindle | .mobi, .azw, .azw3 |
| FictionBook 2 | .fb2, .fbz, .fb2.zip |
| Comic Book Zip | .cbz |
- Trial reading:
withTrialLimit({ maxPages }), trial-aware TOC items, and guarded navigation. See Plugins. - Export:
exportFirstSections()andexportBook()support EPUB, CBZ, TXT, and HTML output. See First Sections Export. - Search:
searchBook(),searchChapters(),reader.search(), andreader.searchChapters(). See Search. - Document Model: query and mutate section trees for AI workflows, annotation, transformation, and serialization. See Document Model.
- Pretext layout: use
prepareBlocks(),layout(), andgetVisibleLines()directly for custom renderers. See Pretext Layout.
- API Reference - API details for readers, parsers, renderers, plugins, exporters, adapters, search, MCP, and document APIs.
- Extension Development - Manifest v1, Host API, permissions, packaging, examples, and publishing.
- Architecture - Parser/renderer separation, adapter design, rendering pipeline, and project layout.
- Experience & Lessons - Design rationale, performance notes, AI workflow ideas, and implementation lessons.
npm install
npm run dev
npm run typecheck
npm run build
npm testMIT
Based on the excellent foliate-js by John Factotum.