futurepress / epub.js

Enhanced eBooks in the browser.
http://futurepress.org
Other
6.45k stars 1.11k forks source link

Render all spine sections #887

Open benjaminpick opened 5 years ago

benjaminpick commented 5 years ago

Hi,

similar to #659 I need to generate a Table of Contents based on the HTML that is inside the sections. However, book.spine.each() only returns the sections without the contents property (except for the current section). Is there a method to load all sections?

The example code

const epub = ePub(blob);

epub.loaded.spine.then((spine) => {
  spine.each((item) => {
    item.load().then((contents) => {
      console.log(contents);
    });
  });
});

results in 404 errors (localhost/OEBPS/section.xhtml) as I am passing the epub as data blob.

EDIT: I could use

book.archive.zip

to access to those files. Does epub.js currently only unzip the current section?

respinos commented 5 years ago

The book (epub here) has a request object that you can pass to item.load() which is aware of your context, either loading from the HTTP path where the book was found, or the data blob. I believe that should help you out here.

rafaelsaback commented 4 years ago

Providing this.book.load.bind(this.book) as the argument to item.load did the job for me:

this.book.loaded.spine.then((spine) => {
  spine.each((item) => {
    item.load(this.book.load.bind(this.book)).then((contents) => {
      console.log(contents);
    });
  });
});