How leopiccionia/epub-builder works
Purpose and public entry point
This TypeScript library builds reflowable EPUB 3.3 archives, with EPUB 2 compatibility artifacts. Consumers configure book metadata, reading order, navigation, and landmarks; add content/resources; then seal the result into a ZIP/blob. Public exports originate at src/index.ts; the primary orchestration API is src/builder.ts.
Builder lifecycle and state
EpubBuilder initializes normalized book configuration, accumulates supplied files/resources, and coordinates final package emission. Its lifecycle establishes the key control flow: init(...) → add/copy files → seal(). Configuration types/defaults and validation are centralized in src/config.ts; resource tracking and package-path decisions are in src/resources.ts.
EPUB package-document generation
The OPF package document is assembled by src/content-opf.ts, delegating standard sections to:
• src/opf/metadata.ts for bibliographic/package metadata,
• src/opf/manifest.ts for all archive resources and media types,
• src/opf/spine.ts for reading order,
• src/opf/guide.ts for EPUB 2 guide compatibility.
This is the boundary where user-facing configuration and collected resource state become EPUB-standard XML.
Navigation and compatibility documents
Navigation inputs produce both EPUB 3 and legacy EPUB 2 structures:
• src/nav-xhtml.ts emits the XHTML navigation document, including TOC and accessibility landmarks.
• src/ncx.ts emits the NCX machine-readable table of contents.
• src/apple-display-options-xml.ts adds Apple-specific display options where required.
Localized navigation labels are sourced through src/locales.ts and bundled translations under src/locales/.
EPUB container layout
EPUB-required root/container metadata is generated separately from content packaging:
• src/container-xml.ts creates META-INF/container.xml, pointing readers to the OPF package.
• src/content-opf.ts represents the package root referenced by that container.
• src/resources.ts manages the internal content/resource set that must be represented in the manifest and archive.
This separation preserves the EPUB boundary between archive-level container metadata and publication-level content metadata.
File ingestion, XML, and archive output
The builder accepts text content and copied external files, using src/utils/files.ts for file-oriented operations. XML serialization/escaping is centralized in src/utils/xml.ts; identifiers and MIME inference are handled by src/utils/ids.ts and src/utils/mime.ts. Final EPUB assembly and ZIP generation occur in src/zip.ts, producing the binary/blob artifact returned by sealing.
Runtime and external standards
The package is a TypeScript/JavaScript library distributed through npm (package.json) and uses no application server or hosted external service. Its principal external contract is EPUB 3.3, with EPUB 2-compatible NCX/guide output and Apple reader compatibility support. Build behavior is defined by build.config.ts; consumers supply runtime file/blob capabilities appropriate to their JavaScript environment.
Main entry points: content-opf.ts OPF package · builder.ts EpubBuilder · zip.ts EPUB ZIP output · resources.ts Resource registry
Components 18
Public API
- Library exports
src/index.ts - EpubBuilder · Runs init, file ingestion, and seal lifecycle.
src/builder.ts
Authoring state
- Book configuration
src/config.ts - Resource registry · Tracks archive resources and internal package paths.
src/resources.ts - File ingestion
src/utils/files.ts
EPUB documents
- Container XML · Creates META-INF/container.xml pointing to the OPF.
src/container-xml.ts - OPF package · Converts configuration and resources into publication metadata.
src/content-opf.ts - OPF metadata
src/opf/metadata.ts - OPF manifest
src/opf/manifest.ts - OPF spine
src/opf/spine.ts - EPUB 2 guide
src/opf/guide.ts - Navigation XHTML · Emits TOC and accessibility landmarks.
src/nav-xhtml.ts - NCX TOC
src/ncx.ts - Apple display options
src/apple-display-options-xml.ts - Navigation locales
src/locales.ts
Archive assembly
- XML serialization
src/utils/xml.ts - IDs and MIME types
src/utils/ids.ts - EPUB ZIP output · Produces the sealed binary or Blob artifact.
src/zip.ts
Connections 30
- Library exports → EpubBuilder: exports
- EpubBuilder → Book configuration: initializes
- EpubBuilder → File ingestion: ingests text and copies files
- File ingestion → Resource registry: registers files
- EpubBuilder → Resource registry: accumulates resources
- EpubBuilder → Navigation XHTML: generates navigation
- EpubBuilder → NCX TOC: generates legacy TOC
- EpubBuilder → Apple display options: adds compatibility file
- EpubBuilder → OPF package: generates package
- Book configuration → OPF package: metadata and reading order
- Resource registry → OPF package: manifest resources
- OPF package → OPF metadata: composes
- OPF package → OPF manifest: composes
- OPF package → OPF spine: composes
- OPF package → EPUB 2 guide: composes
- Resource registry → OPF manifest: resource paths and media
- Navigation locales → Navigation XHTML: localized labels
- XML serialization → Container XML: serializes
- XML serialization → OPF package: serializes
- XML serialization → Navigation XHTML: serializes
- XML serialization → NCX TOC: serializes
- IDs and MIME types → OPF manifest: identifiers and MIME
- Container XML → OPF package: references package root
- EpubBuilder → EPUB ZIP output: seal
- Resource registry → EPUB ZIP output: archive entries
- Container XML → EPUB ZIP output: META-INF entry
- OPF package → EPUB ZIP output: package entry
- Navigation XHTML → EPUB ZIP output: navigation entry
- NCX TOC → EPUB ZIP output: compatibility entry
- Apple display options → EPUB ZIP output: Apple entry