epub.js icon indicating copy to clipboard operation
epub.js copied to clipboard

how to get CFI range from epubCfi.

Open nidhigadhavi opened this issue 4 years ago • 7 comments

If I want CFI range from CFI string than how can I get this parsed CFI range in epub, I am stuck here please help me for the same.

nidhigadhavi avatar Mar 11 '20 10:03 nidhigadhavi

EPUB CFIs allow the expression of simple ranges extending from a start location to an end location. A range must be expressed as a triple of parent path (P), start subpath (S) and end subpath (E), or of the form:

epubcfi(P,S,E)

The composition of the cfi string includes the parent path and subpath, separated by an exclamation mark; You can use regular expressions to separate them first, then use commas to stitch them together.

Pseudocode:

const startCfi = 'epubcfi(/6/4[chap01ref]!/4/2/10/2/1:1)'
const endCfi = 'epubcfi(/6/4[chap01ref]!/4/2/16/4/1:0)'
const cfiBase = startCfi.replace(/!.*/, '') 
const cfiStart = startCfi.replace(/.*!/, '').replace(/\)$/, '')
const cfiEnd = endCfi.replace(/.*!/, '').replace(/\)$/, '')
const cfiRange = `${cfiBase }!,${cfiStart },${cfiEnd })`

GerogeLiu avatar Jun 07 '20 01:06 GerogeLiu

@GerogeLiu can you please help me to convert cfirange to start and end cfi

connectus-technology avatar Jun 24 '20 12:06 connectus-technology

@GerogeLiu can you please help me to convert cfirange to start and end cfi

please show me your code, I will try to help you!

GerogeLiu avatar Jun 24 '20 14:06 GerogeLiu

rendition.on("selected", (cfiRange, contents) => { book.getRange(cfiRange).then(range => { if (range.toString()) { // } }) })

need to convert cfiRange to start and end cfi so that i can use compare function provided by the library to know whether some part of currently selected text was previously already selected

connectus-technology avatar Jun 24 '20 14:06 connectus-technology

rendition.on("selected", (cfiRange, contents) => { book.getRange(cfiRange).then(range => { if (range.toString()) { // } }) })

need to convert cfiRange to start and end cfi so that i can use compare function provided by the library to know whether some part of currently selected text was previously already selected

The easy way that you can get start and end cfi split cfiRange by ',' ,the first eleme;nt in the obtained array is "cfibase", and the last two elements are connected to "cfibase" to get start and endcfi

GerogeLiu avatar Jun 25 '20 02:06 GerogeLiu

rendition.on("selected", (cfiRange, contents) => { book.getRange(cfiRange).then(range => { if (range.toString()) { // } }) }) need to convert cfiRange to start and end cfi so that i can use compare function provided by the library to know whether some part of currently selected text was previously already selected

The easy way that you can get start and end cfi split cfiRange by ',' ,the first eleme;nt in the obtained array is "cfibase", and the last two elements are connected to "cfibase" to get start and endcfi

// example:
const cfiRange='epubcfi(/6/4[chap01ref]!/4[body01]/10[para05],/2/1:1,/3:4)';
let cfiParts = cfiRange.split(',');  // cfiBase:  cfiParts[0] 
let startCfi = cfiParts[0] + cfiParts[1] + ')';   // start: 'epubcfi(/6/4[chap01ref]!/4[body01]/10[para05]/2/1:1)'
let endCfi = cfiParts[0] + cfiParts[2];       // end: 'epubcfi(/6/4[chap01ref]!/4[body01]/10[para05]/3:4)'

GerogeLiu avatar Jun 25 '20 02:06 GerogeLiu

here's my solution for getting range of the displayed page:

const url = "/assets/alice.epub";
const book = ePub(url);
const {
    start,
    end
} = renditionRef.current.location;
if (start && end) {

    const splitCfi = start.cfi.split('/');
    const baseCfi = splitCfi[0] + '/' + splitCfi[1] + '/' + splitCfi[2] + '/' + splitCfi[3];
    const startCfi = start.cfi.replace(baseCfi, '');
    const endCfi = end.cfi.replace(baseCfi, '');
    const rangeCfi = [baseCfi, startCfi, endCfi].join(',');

    book.ready.then(function() {
        book.getRange(rangeCfi).then(function(range) {
            let text = range.toString()
            console.log(text);
        });
    });
}

lasuax avatar Jun 13 '22 00:06 lasuax