fread-ink / epub-cfi-resolver

A simple parser, resolver and generator for the EPUB-CFI format
GNU Affero General Public License v3.0
19 stars 3 forks source link

Get cfi position from string? #3

Open Yaco opened 4 years ago

Yaco commented 4 years ago

Hi! Just wondering if it would be possibl to use this to get cfi position (range?) of a string of text inside an ePub file?

Thanks!

Juul commented 4 years ago

Hm. You can use the function CFI.generate(node, offset, extra) to generate a CFI given a reference to a node (e.g. a text node) and an optional offset into the text node, and you could do that twice for the beginning and end of the range but there is currently no function for turning those two CFIs into a range CFI.

This is definitely an oversight and needs to be added. Pull requests very welcome.

I'd look at the CFI.generatePart function and instead of directly building the CFI in reverse order, then build the same data structure as returned by the .get function. Once you have such a data structure for both the start and end CFIs I'd traverse these two data structures in the forward direction, comparing them and building the range CFI on piece at a time.