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)
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.
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:
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 element | HTML 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> |
Returned book object
| Property / method | Description |
|---|---|
.sections | One 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). |
.toc | Derived from section titles (<title> and <subtitle> elements). Nested TOC items correspond to sub-sections within each top-level section. |
.metadata | Extracted 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. |