futurepress / epub.js

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

book.loaded.metadata returns incomplete metadata when metadata contains multiple of the same element #950

Open BrianSladek opened 5 years ago

BrianSladek commented 5 years ago

For example, epub metadata can contain multiple creator or language elements, but this library only returns at most one of the creators/languages/subjects/etc. in book.loaded.metadata. parseMetadata in packaging.js currently returns a map of metadata type to string value. It may be better to restructure this to return an array of strings for metadata types that may contain multiple elements.

This epub contains multiple authors and languages, but epubjs only returns one of each -- can be used for testing.

SebastianKohler commented 2 years ago

I wasn't able to get the parse method in packaging to work, but here is how I (after a lot of trials and errors) got all of the creators from the package document of the epub. It can of course also be used to get other metadata that book.loaded.metadata doesn't provide.

this.book.archive.getText('/' + this.book.container.packagePath).then((res) => {
        const parser = new DOMParser();
        const opf = parser.parseFromString(res, 'text/xml');
        const creatorElements = opf.getElementsByTagName('dc:creator');
        let creators = [];
        for (let i = 0; i < creatorElements.length; i++) {
          creators.push(creatorElements.item(i).textContent);
        }
        console.log(creators);
 });