Skip to main content

Documentation 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.

mobi.js reads both classic Mobipocket (MOBI) and Kindle Format 8 (KF8, commonly distributed as .azw3 or as the KF8 section inside a combo .mobi file) from a browser File or Blob object. Unlike the EPUB and CBZ parsers, it does not need a zip loader — it parses the PalmDB container format directly. The module exports two items: the isMOBI detection function and the MOBI class.

isMOBI(file)

const { isMOBI } = await import('./mobi.js')
const isSupported = await isMOBI(file)
Async function. Reads the first 68 bytes of the file to check for the BOOKMOBI magic string in the PalmDB record header. Returns a boolean. Use this before constructing a MOBI instance to avoid errors on unsupported file types.
file
File | Blob
required
The file to inspect. Only the first 68 bytes are read.

new MOBI(options).open(file)

const { isMOBI, MOBI } = await import('./mobi.js')
if (await isMOBI(file)) {
    const fflate = await import('./vendor/fflate.js')
    book = await new MOBI({ unzlib: fflate.unzlibSync }).open(file)
}
Constructs a MOBI parser instance and opens the file. Returns a Promise that resolves to the fully-initialised book object implementing the standard book interface.
options.unzlib
function
Synchronous decompression function with the signature (data: Uint8Array) => Uint8Array. Required when the KF8 file contains zlib-compressed fonts. view.js passes fflate.unzlibSync from the vendored fflate library. If omitted and the file contains compressed fonts, those fonts will not load correctly.
file
File | Blob
required
The Mobipocket or KF8 file to open.

MOBI vs KF8 behaviour

The open() method detects which format the file uses (including combo MOBI/KF8 files, where it prefers the KF8 section) and returns the appropriate book object. The two formats behave differently at runtime:
BehaviourMOBI (version < 8)KF8 (version ≥ 8 / AZW3)
Text decompressionAll text records decompressed eagerly at open time, then split into sections at every <mbp:pagebreak> tagLazy — only the records needed for the current section are decompressed when .load() is called
Decompression algorithmPalmDOC LZ77HUFF/CDIC (slower; current implementation is not optimised)
Images and resourcesLoaded on demand from PalmDB recordsLoaded on demand from PalmDB records
Font supportNo zlib-compressed fontsMay contain zlib-compressed fonts — requires unzlib option
For MOBI files, decompressing all text at once and splitting at <mbp:pagebreak> tags dramatically improves rendering performance compared to rendering the book as a single long page. For KF8, lazy section loading is used instead, though it can still be slow due to the HUFF/CDIC decompressor.

Returned book object

The resolved value of .open(file) is a book object that implements the standard foliate-js book interface:
Property / methodDescription
.sectionsArray of section objects, one per page-break-delimited chunk (MOBI) or KF8 fragment. Each has .load(), .unload(), .createDocument(), and .size.
.tocParsed table of contents derived from the NCX or INDX structures in the file.
.metadataBook metadata (title, author, language, description, etc.) extracted from the EXTH header.
.dirPage progression direction.
.resolveHref(href)Resolves an internal href to { index, anchor }.
.splitTOCHref(href)Splits a TOC href into [path, fragment].
.isExternal(uri)Returns true if the URI should be opened externally.
.getCover()Returns the cover image as a Blob.
.destroy()Frees any blob URLs created during loading.

Build docs developers (and LLMs) love