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.

fb2.js parses FictionBook 2 (.fb2) documents and converts them into a book object compatible with the foliate-js reflowable renderer. It reads the XML structure of the FictionBook document, converts each <body> and top-level <section> element into a separate XHTML section served as a blob URL, inlines all embedded binary resources as data URIs, and derives the table of contents from section titles. Compressed .fb2.zip and .fbz archives are handled by view.js, which extracts the .fb2 file from the zip before calling makeFB2.

makeFB2(blob)

const { makeFB2 } = await import('./fb2.js')
book = await makeFB2(file)
Async function. Accepts a File or Blob containing either a plain .fb2 XML document or a raw .fb2 blob (after extraction from a zip archive). Returns a Promise that resolves to the book object.
blob
File | Blob
required
The FictionBook 2 document as a File or Blob. The function reads the XML declaration to detect encoding (e.g. Windows-1251) and re-decodes with TextDecoder if necessary.

Archive formats

view.js handles the two compressed variants before calling makeFB2:
// For .fbz / .fb2.zip: extract the .fb2 entry from the zip first
const { entries } = loader
const entry = entries.find(entry => entry.filename.endsWith('.fb2'))
const blob = await loader.loadBlob((entry ?? entries[0]).filename)
book = await makeFB2(blob)

// For plain .fb2 files: pass directly
const { makeFB2 } = await import('./fb2.js')
book = await makeFB2(file)
Detection is based on the file’s MIME type or name extension: application/x-fictionbook+xml / .fb2 for plain files, and application/x-zip-compressed-fb2 / .fb2.zip / .fbz for archives.

Document conversion

fb2.js uses FB2Converter, an internal class that walks the FictionBook XML tree and produces XHTML. The conversion maps FictionBook elements to HTML equivalents:
FictionBook elementHTML output
<p><p>
<section><section>
<title><header> containing <h1> elements
<subtitle><h2>
<emphasis><em>
<strong><strong>
<poem><blockquote>
<epigraph><blockquote>
<cite><blockquote>
<table><table>
<image><img> with inline data: URI from <binary>
<a><a> (notes get epub:type="noteref")
<empty-line><br>
Each converted section is serialised to XHTML and wrapped in a minimal template with a built-in stylesheet, then exposed as a blob URL.

Returned book object

Property / methodDescription
.sectionsOne section per top-level element in the first <body> (each becomes a separate page), plus one section per additional <body> (e.g. endnotes), which are marked linear: 'no'. Each section has .load(), .createDocument(), and .size (image data is excluded from the size calculation to avoid skewing reading progress).
.tocDerived from section titles (<title> and <subtitle> elements). Nested TOC items correspond to sub-sections within each top-level section.
.metadataExtracted from <title-info> and <document-info>: title, identifier, language, author (with sort key), translator, contributor, publisher, published date, modified date, description, and subject (genre tags).
.resolveHref(href)Handles both TOC-style hrefs (numeric section index with optional fragment) and in-page #id hrefs via an ID-to-section map.
.splitTOCHref(href)Splits a TOC href into [sectionIndex, fragmentIndex] as numbers.
.getTOCFragment(doc, id)Returns the element with the data-foliate-id attribute matching id.
.isExternal(uri)Returns true for URIs that start with a scheme (\w+:).
.getCover()Returns a Blob for the cover image found in <coverpage>, or null.
.destroy()Revokes all blob URLs created for section content.

Build docs developers (and LLMs) love