This is how the converter (convert/) turns an EPUB into a web book, step by step. Each step links
to the code that does it, with the reasons behind it. The v0 contract
describes what the output contains, and The conversion report and log explains
the report each conversion writes.
It’s for people working on the converter. The line links point at the code as of 2026-11-17. If a line has moved, search the file for the function name.
Everything below follows these rules. If a change seems to need breaking one, ask first.
title attribute, whose title is shown as its text.javascript: links are
removed from every page and counted in the report (step 6). In the reader, they’d run with the
site’s access to readers’ settings and saved books, and in the browser converter, the converter
page’s.demos/kusamakura.--identifier). Readers’ saved positions and
bookmarks are kept under it. It’s the source EPUB’s ID, and we never present it as the web book’s
own.index.html is filled in as an
intermediate document, then book-shape.ts writes plain content pages and separate navigation
pages. copyUiAssets extracts the runtime shell. The template’s hooks are still listed in
template-contract.ts.convert() runs the steps below, in this order.
First, checkOutputPath refuses an output folder that is, contains
or is inside the EPUB or the template (through links too), since replacing it would delete them.
createOutputStaging makes a temporary folder, and everything is
written there. At the end, finalizeOutput moves it into place: an
earlier book in the output folder is moved aside first and deleted only once the new one is there.
If anything fails, discardOutputStaging deletes the temporary
folder, so a failed conversion never leaves a half-written book behind. The log (output.log) starts here
(logger/).
epubParser.parse unzips the EPUB if it’s a file
(unzip-epub.ts). A damaged archive gets repaired and
unzipped again. An EPUB with a symbolic link in it is refused. Then it finds the package through META-INF/container.xml and reads two things.
package-document.ts).nav-document.ts).Spine items marked linear="no" are left out, unless you use --include-non-linear
(parseSpine).
buildSiteModel works out the plan for the web book, without writing
anything yet.
.html, and the first one is index.html). Nothing is
renamed on disk.planDocumentSplit). Several
top-level entries always mean chapters. Nested entries only count in long documents (50 KiB or
more), so regular chapter files don’t get cut into their subsections. --no-split-chapters turns
this off.<title> is just the book’s title, each page gets its first other heading as its
title instead, so you can tell tabs and history entries apart. --no-heading-titles turns this
off.buildToc) keeps the nav document’s
nesting. The page list (buildPageList) has the print page
numbers. The metadata (buildMetadata) has the title, authors,
language (or --language) and cover.idToOutput), so links still work across
split pages.Back in convert(), a few things are set up for the whole book.
--no-remote-ui for a fixed reader, and --ui-base-url <url> picks a
different channel. The report records the choice, and the converter prints a beta warning (see
REMOTE_UI.md).webbook-source meta on every page.ConversionReport).readFontEncryption).readAccessibilityInfo). It uses the wording and
order of the W3C Accessibility Metadata Display Guide 2.0, and presents them as the publisher’s
claims.copyUiAssets,
combineStylesheets). Its scripts go in as a bundle: npm run
compile runs build-reader.mjs, which bundles the reader’s
70 or so modules into a few files, with the parts that load only when used (search, math copying,
the shortcut editor) in their own. Each module was a separate request, found only after the one
that imports it, so on a slow connection the reader took many round trips to arrive. The offline
workers stay separate files. Without a bundle, a book gets the separate modules
(useReaderBundle).sw.js, the fixed stub for offline reading, is written
(writeServiceWorkerStub).createBookFavicon).For each content document with a media overlay, getMediaSegments
reads its SMIL, with each phrase’s text target and audio clip. Each clip goes with the output page
that has its target, which matters when a document was split. This happens first so that every
page’s table of contents can show which pages have narration.
transformContent runs once per page, on one in-memory
DOM, in this order.
readPageStylesheets).applyWritingStyles). Vertical writing and -epub-
properties are kept here.PublisherSelectors).
If a later change would stop a publisher rule from matching, that rule is “frozen” first, so As
published still looks the way it was published.removeActiveContent), on
every page, whatever the cleanup option: script elements (SVG’s too), event handler
attributes (onclick and so on), srcdoc, SVG animations of an href, and href, src,
action and similar attributes that start with javascript: or vbscript:. The element a
link was on stays, with its words. SVG image files get the same treatment as they’re copied,
since an SVG opened by itself runs its scripts. The book’s body styles and stylesheet links go here too: the
rewritten head style is the only book CSS in the output.epub:type becomes epubtype_* classes and, where DPUB-ARIA has one, a role
(translateEpubTypes). Old rearnote(s) count as
endnote(s), and deprecated doc-endnote roles are dropped.audio, video, source and track, and a
video’s poster, as opposed to its narration) are registered for copying into images/, and their
references are rewritten. A book’s hrefs are URLs, so they’re decoded before they name a file
(chapter%20one.xhtml is chapter one.xhtml), here and wherever the converter reads one
(resolveHrefPath). The copies’ own names are encoded where a URL
needs it (a space, #, ? or %).extractDocumentPiece), and links are rewritten to the
final file names. A link to the book’s own navigation document goes to toc.html, the web
book’s table of contents.cleanPublicationMarkup). These are
the small mechanical fixes, and each one is counted in the report.
title attribute is shown as its text if the heading is otherwise empty, and
dropped if it repeats the text. An empty heading that only repeats the one next to it is
removed (repairHeadingTitles).xml:lang doesn’t do anything in HTML, so it’s copied to lang when the element’s text is in
the expected script, and then removed. If the script doesn’t match, it isn’t copied, and the
report says so (repairLanguages).markNumberedEquations).<span>s are unwrapped and empty paragraphs are removed. If the publisher’s CSS styles
them, they’re kept, and the paragraphs only show in As published (webbook-publisher-only),
since publishers use them for spacing.scope get one.<main> becomes a div, because the reader has its own main.tabindex values become 0. Media lose autoplay and get controls.wrapTables).It also looks for things to review and adds them to the report as warnings
(analyzeWarnings). MathML alttext is left the way
the book has it.
tabindex="-1" on the heading
so following the link moves focus.collectSearchPassages).applyStructureIds). This keeps the book’s markup
clean.renameCollidingIds).publisherStyles). Fonts and images it
uses are copied. --no-publisher-styles leaves it out completely.createAudioForPage makes one audio file and one cue file for each
narrated page.
decodeSourceRanges, cutToWav).
Pages are converted up to four at a time, half the computer’s cores.applyClipFades).joinPcmFiles).cleanupFilters). This cuts everything
below 90 Hz (rumble), softens “p” and “b” pops, applies the chosen enhancements (warm EQ,
compression, noise reduction, de-essing, click removal). Then it measures the loudness once
(measureLoudness) and raises narration quieter than −18
LUFS to that level. Louder narration keeps its level. A limiter holds the peaks under −1.5 dB
without delaying the audio (loudnessFilters).--small-audio (the default) that’s mono MP3, 24 kHz,
32 kb/s (SMALL_AUDIO_ENCODING). Otherwise it matches the source.analyzeAudio).Some decisions behind this:
ffmpeg-static), not the 4.4 that @ffmpeg-installer ships. FFmpeg 4.4’s MP3
decoder adds a 6.9 kHz whistle to some sources, like Kusamakura’s 24 kHz, 40 kb/s mono MP3s
(ffmpeg.ts).--audio-enhance lets you pick them, and --no-audio-cleanup turns all cleanup off.--audio-cache) is for development. It reuses a page’s audio if its clips
and settings haven’t changed (audio-cache.ts). Its version
number (MERGE_VERSION) goes up whenever the audio a conversion makes would be different.The cue file (createVtt) is WebVTT. Each cue’s text is a JSON selector
for its phrase, either an id or a CSS path from the nearest id (step 6.11).
TemplateRenderer.renderPage copies the template and fills it in.
That means the page’s content in main, its title and language, the table of contents (with the
current page marked and narrated pages flagged, renderToc),
previous and next links, the audio and its cue track, the page list, the About panel’s metadata and
accessibility summary, and the link to the publisher’s stylesheet. Writing direction and mode go
on the page as attributes (applyWritingStylesToPage). Identical
publisher stylesheets are only written once, named by their hash (styles/publisher-<hash>.css).
Then shapeBookPage turns the rendered page into the book’s plain
page (plan 11). Each page gets:
toc.html), Go to page (pages.html, only
if the book has a page list), About (about.html), Previous and Next;<audio controls> with its cue track, and the live regions the
reader uses for announcements;build-bootstrap.mjs builds it, readable, from bootstrap-source.js and the
unit-tested decisions in bootstrap-policy.js. It goes inline, and also into
src/js/bootstrap.js for sites whose security policy only allows same-origin scripts. It picks
which reader to load: the book’s own, the release a saved copy has, or the channel’s current
release. If anything fails, it falls back to the book’s own reader, and then to the plain page
(bootstrap.js). It’s frozen into every page, so the
only way to change it is to convert the book again.With the first page, the navigation pages are written too. These are toc.html, about.html (all
the EPUB’s metadata, credited to its source) and, if the book has a page list, pages.html.
They’re regular pages that work on their own, even from the file system. Their names are reserved,
so a source chapter called about.xhtml becomes about-content.html, and its links are updated.
generateAudioMap) has every narrated
page’s duration and cue starts, and where each chapter’s narration begins. The reader uses it for
the whole-book timeline, time left and resuming.generateSearchIndex) has
every passage, with its page, heading and selector. Search runs in the browser, and splits words
by language.copyImageAssets).reportBrokenLinks).--check, Ace (on the EPUB) and axe (on the output) run if they’re installed
(runOptionalChecks).writeConversionReport).The staging folder replaces the output folder (finalizeOutput).
The same code runs in a browser, bundled with stand-ins for Node’s file system, jsdom, logging and
FFmpeg (plan 19). It’s in converter/, and each stand-in is listed in
its Shims section. Narration works a little
differently there. The browser decodes each audio source once and cuts the clips itself, lined up
sample for sample with FFmpeg’s decoding, and ffmpeg.wasm does the fades, joins, cleanup and
encoding. The results and measurements are in .claude/19_BROWSER_CONVERTER_FINDINGS.md.
The converter page has every option above except the audio cache, the optional checks and
--ui-base-url (the page always uses the v0 update channel). Reader
updates is always shown, and starts on the channel. The page asks you to confirm the EPUB passes
EPUBCheck, since the browser can’t run it. Then it works in two steps. First you preview one of the
book’s content documents (often a chapter), converted from a copy of the EPUB whose reading order
is just that document, and shown right on the page. Then you convert the whole book and get a zip
of the files to put on any website. Nothing is kept in the browser for good. The preview’s files are in the browser’s Cache Storage
until the next preview replaces them, or the converter page is opened again. By default the preview stops the
narration after 5 minutes, keeping only the phrases that start before then, so it converts in
seconds instead of taking as long as the whole chapter.
cd convert && npm run build && node --test tests/*.test.mjs, then the
reader tests with node --test template/test/*.test.mjs. Testing lists the other
checks, such as the browser converter’s../refresh-demos.sh from the repository root.contracts/v0.md.MERGE_VERSION in
audio-cache.ts.