All book parsers in foliate-js — whether for EPUB, MOBI, FictionBook, or comic archives — return an object that implements the same interface. This shared contract is what lets the renderers remain format-agnostic: they only need to know how to callDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/johnfactotum/foliate-js/llms.txt
Use this file to discover all available pages before exploring further.
.sections[n].load() and respond to events, not how ZIP decompression or package document parsing works. You can also implement this interface yourself to add support for custom formats without touching any renderer code.
The book object
At minimum, a book object needs.sections and a .load() method on each section item. Everything else is optional and progressively enables features like reading progress, TOC tracking, and CFI-based navigation.
.sections
An array of section objects representing the spine of the book. Each item in the array has the following properties:
Array of section objects. Each section represents one loadable unit of content (typically one HTML document in an EPUB spine).
Navigation and metadata
Page progression direction of the book. The view element uses this to determine which direction
.goLeft() and .goRight() should navigate.Table of contents as a flat or nested array. Each item has the following shape:
Same structure as
.toc, but represents the page list — the logical page numbers corresponding to a print edition.Metadata about the book. Follows the schema of Readium’s webpub manifest. String fields like
title, author, and language can be plain strings or localized objects:Corresponds to EPUB rendition properties. The most important property is
.layout:Resolution methods
resolveHref(href)
(href: string) => { index: number, anchor: (doc: Document) => Element | Range | null }
Given an href string (e.g., from a hyperlink inside the book content), returns an object describing the destination:
.index— the index of the target section in.sections.anchor(doc)— given the loadedDocument, returns the targetElementorRange, ornullif not found
resolveCFI(cfi)
(cfi: string) => { index: number, anchor: (doc: Document) => Element | Range | null }
Same return shape as
.resolveHref(), but accepts an EPUB CFI string instead of an href.Returns
true if the given href should be opened in an external browser rather than navigated to inside the reader. The view element uses this to decide whether to fire an external-link event instead of calling .goTo().Progress tracking methods
The following methods are consumed byprogress.js to track which TOC item corresponds to the current reading position. You only need to implement them if you want TOC and page-list progress reporting.
Given an href from the TOC, splits it into two parts:
- The section identifier (matching
.idon a section in.sections) - A fragment identifier used to locate the specific node within that section
Given a loaded
Document and the fragment identifier returned by .splitTOCHref(), returns the Node that the TOC item points to. Used to determine which TOC item is active based on the current scroll position.Content transformation
If present, an The paginator uses this internally to strip
EventTarget that fires "data" events as the book’s resources are loaded. Event handlers can intercept and transform content before it is rendered.-epub- prefixes and replace vw/vh units.Implementing a custom format
Because the book object is a plain JavaScript object (not a class instance), you can implement it yourself for any content source:Only
.sections and .load() are strictly required. All other properties are optional and unlock additional features like progress tracking, TOC navigation, and CFI-based bookmarks.The archived files loader interface
EPUB and CBZ formats are ZIP-based and need an external loader object to handle decompression. Bothepub.js and comic-book.js expect a loader argument that implements this interface:
Array of archive entry objects, each with a
.filename string property containing the full path within the archive. Used by comic-book.js to enumerate images.Given the full path within the archive, returns the file contents as a string. May be async.
Given the full path, returns the file as a
Blob object. May be async.Returns the uncompressed byte size of the file. Used to populate the
.size property on section objects.view.js implements this using zip.js, which supports random access for File objects and HTTP range requests. The same loader interface also works for unarchived (directory-based) EPUBs — view.js includes a makeDirectoryLoader that satisfies it using the File System Access API.