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.
epubcfi.js is a self-contained EPUB Canonical Fragment Identifier (CFI) implementation. It parses CFI strings into plain JavaScript objects, generates CFIs from DOM Ranges, resolves CFIs back to Ranges, and compares or joins CFIs. The parser uses a hand-written state machine and handles escaped characters and text assertions correctly. The module has no dependencies and can be used outside a browser if you only need parsing or comparison.
Import
Parsed CFI structure
parse() returns plain objects. A collapsed (non-range) CFI is an array of path arrays, where each path array contains part objects:
| Property | Type | Description |
|---|---|---|
index | number | Step index (from / token). |
id | string | Element ID assertion (from [id]). |
offset | number | Character offset (from : token). |
temporal | number | Temporal offset (from ~ token). |
spatial | number[] | Spatial offset coordinates (from @ tokens). |
text | string[] | Text location assertions. |
side | string | Side bias (from ;s= parameter). |
Exports
isCFI
A RegExp that tests whether a string is a wrapped CFI (i.e. matches epubcfi(...)).
joinIndir(...cfis)
Joins two or more CFIs with the step indirection operator (!), wrapping the result in epubcfi(...).
Two or more CFI strings (wrapped or bare). The wrapper is stripped before joining.
parse(cfi)
Parses a CFI string into a plain object or array. Accepts both wrapped (epubcfi(...)) and bare forms.
A CFI string to parse.
{ parent, start, end } for range CFIs.
fromRange(range, filter?)
Converts a DOM Range to a CFI string. Produces a range CFI when the range is not collapsed.
A DOM
Range within a section document.An optional node filter with the same signature as a
TreeWalker filter: (node: Node) => NodeFilter.FILTER_ACCEPT | NodeFilter.FILTER_REJECT | NodeFilter.FILTER_SKIP. Use this to exclude injected nodes (annotations, highlights) from the CFI path.toRange(doc, cfi, filter?)
Resolves a CFI string to a DOM Range within a document.
The section
Document in which to resolve the CFI.A CFI string (wrapped or bare) or a parsed CFI object.
Same node filter as
fromRange. Must match the filter used when the CFI was created if injected nodes are present.compare(a, b)
Comparator for sorting CFIs. Returns a negative number, zero, or a positive number following the standard comparator convention, suitable for use with Array.prototype.sort.
A CFI string or parsed object.
A CFI string or parsed object.
fake.fromIndex(index)
Creates a fake base CFI for non-EPUB books (e.g. CBZ, FB2) that don’t have a real package document. The result can be used as the section.cfi property when constructing a book object.
Zero-based section index.
fake.toIndex(parts)
Extracts the zero-based section index from a parsed fake CFI. The inverse of fake.fromIndex.
A parsed CFI (the result of
parse()), or the parts array directly.Filter function
BothfromRange and toRange accept an optional filter that mirrors the TreeWalker filter API: