About
Prose provide a TypeScript library for working with EPUB Canonical Fragment Identifiers (CFI). This library provides utilities for parsing, resolving, generating, comparing, and serializing CFIs according to the EPUB CFI 1.1 specification.
Installation
npm install @prose-reader/cfiUsage
Parse
Parse a CFI string into a structured object.
import { parse } from '@prose-reader/cfi'
// Parse a simple CFI
const parsed = parse('epubcfi(/6/4[chap01ref]!/4[body01]/10[para05]/2/1:3)')
// Parse a range CFI
const rangeParsed = parse('epubcfi(/6/4[chap01ref]!/4[body01],/6/4[chap01ref]!/4[body01]/10[para05])')Resolve
Resolve a CFI to a DOM node or range.
Generate
Generate a CFI from a DOM node or position.
Compare
Compare two CFIs according to the EPUB CFI specification sorting rules.
Serialize
Convert a parsed CFI back to a string.
API Reference
Parse
Parses a CFI string into a structured object. The parsed CFI can be either a simple path or a range.
Resolve
Resolves a CFI to a DOM node or range. Options include:
throwOnError: Whether to throw an error if the CFI cannot be resolvedasRange: Whether to return a range instead of a single node
Generate
Generates a CFI from a DOM node or position. Options include:
includeTextAssertions: Whether to include text assertionstextAssertionLength: Maximum length of text to use for assertionsincludeSideBias: Whether to include a side bias assertionspatialOffset: Spatial coordinates for image/video contentextensions: Extension parameters to include
Compare
Compares two CFIs according to the EPUB CFI specification sorting rules. Returns:
-1 if first CFI is before second
0 if CFIs are equal
1 if first CFI is after second
Serialize
Converts a parsed CFI back to a string representation.
Last updated
Was this helpful?