epub.js
epub.js copied to clipboard
how to get CFI range from epubCfi.
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.
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 can you please help me to convert cfirange to start and end cfi
@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!
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
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
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)'
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);
});
});
}