About
Prose provides 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
Section titled “Installation”npm install @prose-reader/cfiParse a CFI string into a structured object.
import { parse } from '@prose-reader/cfi'
// Parse a simple CFIconst parsed = parse('epubcfi(/6/4[chap01ref]!/4[body01]/10[para05]/2/1:3)')
// Parse a range CFIconst rangeParsed = parse('epubcfi(/6/4[chap01ref]!/4[body01],/6/4[chap01ref]!/4[body01]/10[para05])')Resolve
Section titled “Resolve”Resolve a CFI to a DOM node or range.
import { resolve } from '@prose-reader/cfi'
// Resolve to a nodeconst result = resolve('epubcfi(/6/4[chap01ref]!/4[body01]/10[para05])', document)
// Resolve to a rangeconst rangeResult = resolve('epubcfi(/6/4[chap01ref]!/4[body01],/6/4[chap01ref]!/4[body01]/10[para05])', document, { asRange: true })Generate
Section titled “Generate”Generate a CFI from a DOM node or position.
import { generate } from '@prose-reader/cfi'
// Generate from a nodeconst cfi = generate(node)
// Generate with optionsconst cfiWithOptions = generate(node, { includeTextAssertions: true, textAssertionLength: 20, includeSideBias: 'before', spatialOffset: [50, 50], extensions: { 'vnd.example.param': 'value' }})
// Generate from a positionconst positionCfi = generate({ node: textNode, offset: 5, temporal: 30, spatial: [25, 75]})
// Generate a rangeconst rangeCfi = generate({ start: { node: startNode, offset: 0 }, end: { node: endNode, offset: 10 }})Compare
Section titled “Compare”Compare two CFIs according to the EPUB CFI specification sorting rules.
import { compare } from '@prose-reader/cfi'
// Compare two CFIsconst result = compare( 'epubcfi(/6/4[chap01ref]!/4[body01]/10[para05])', 'epubcfi(/6/4[chap01ref]!/4[body01]/10[para06])')// Returns -1 if first CFI is before second, 0 if equal, 1 if afterSerialize
Section titled “Serialize”Convert a parsed CFI back to a string.
import { parse, serialize } from '@prose-reader/cfi'
// Parse and then serializeconst parsed = parse('epubcfi(/6/4[chap01ref]!/4[body01]/10[para05])')const cfiString = serialize(parsed)API Reference
Section titled “API Reference”function parse(cfi: string): ParsedCfiParses a CFI string into a structured object. The parsed CFI can be either a simple path or a range.
A string that is only partly a CFI is not one, so every character has to be read as part of it. parse throws for a character that can't be, such as text after the closing parenthesis, for an epubcfi( or an assertion never closed, and for a step or an offset without its number. Whitespace around the CFI is ignored. It doesn't check that the parts come in an order the specification allows.
Resolve
Section titled “Resolve”function resolve( cfi: string | ParsedCfi, document: Document, options?: ResolveOptions): ResolveResultResolves 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
Section titled “Generate”function generate( position: Node | CfiPosition | { start: CfiPosition; end: CfiPosition }, options?: GenerateOptions): stringGenerates 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
Section titled “Compare”function compare(a: ParsedCfi | string, b: ParsedCfi | string): numberCompares 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
Section titled “Serialize”function serialize(parsed: ParsedCfi): stringConverts a parsed CFI back to a string representation.