Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there a way to get contents of the epub without rendering it first? #1357

Open
alexjyong opened this issue Sep 12, 2023 · 1 comment
Open

Comments

@alexjyong
Copy link

alexjyong commented Sep 12, 2023

I noticed that when you access book.spineItems[n].contents, or something like that it's only available after the book has been rendered.

Is there a way to access the epub text without rendering the document first?

Currently I use this to get all text in a given book:

// Initialize the book with the path to the .epub file
var book = ePub("myEpub.epub");

// Array to store the text contents of each section
var sectionsContents = [];

// Render the book
var rendition = book.renderTo("area", {  flow: "scrolled-doc", width: 600, height: 400 });

// Handle rendering events
rendition.display().then(async function() {
  var spineItems = rendition.book.spine.spineItems;
  console.log(spineItems.length)
  for(var i =0; i< spineItems.length; i++){
    spineItem = spineItems[i];
    sectionsContents.push(spineItem.contents.innerText);
    await rendition.next()
  }
    
});

This works, but seems a bit clunky.

@johnfactotum
Copy link
Contributor

Instead of rendition.display(), wait for book.open(). There's also book.ready, and book.loaded.spine, etc.

var book = ePub();
book.open('myEpub.epub').then(() => { /* ... */ });

// or
var book = ePub('myEpub.epub');
book.ready.then(() => { /* ... */ });

And instead of rendition.next(), you should first get the section object and call section.load() or section.render(). See https://github.com/futurepress/epub.js/blob/master/examples/renderless.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants