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

Error with image URL #101

Open
adamAfro opened this issue Sep 27, 2021 · 3 comments
Open

Error with image URL #101

adamAfro opened this issue Sep 27, 2021 · 3 comments

Comments

@adamAfro
Copy link

adamAfro commented Sep 27, 2021

So I've got an error like that

.../node_modules/epub-gen/lib/index.js:169
            mediaType = mime.getType(url.replace(/\?.*/, ""));
                                         ^
TypeError: Cannot read properties of undefined (reading 'replace')

which I fixed by adding if

if (url) { // 162
    if (image = self.options.images.find(function(element) {
      return element.url === url;
    })) {
      id = image.id;
      extension = image.extension;
    } else {
      id = uuid();
      mediaType = mime.getType(url.replace(/\?.*/, ""));
      extension = mime.getExtension(mediaType);
      dir = content.dir;
      self.options.images.push({id, url, dir, mediaType, extension});
    }
    return $(elem).attr("src", `images/${id}.${extension}`);
}

I don't know if there is more to that - maybe an image wasn't downloaded properly - it fixed error and gave valid epub so...

@valexandersaulys
Copy link

I have also encountered this issue fwiw.

@1Cr18Ni9
Copy link

Removing all malicious picture before generating epub file can resolve this issue. Like this:

// remove all img element that are not specified src attribute, src pointe to img source url:
document.querySelectorAll('img:not([src])').forEach(e => e.remove());

// then generate file with correct content
new Epub(option, "/path/to/book/file/path.epub");

@lapwat
Copy link

lapwat commented Jan 9, 2024

Sometimes, the url is stored in the srcset attribute. We can copy it in the src attribute.

import { JSDOM } from 'jsdom';

const doc = new JSDOM(htmlStrContent).window.document;

// remove all img element that are missing a src attribute
doc.querySelectorAll('img:not([src])').forEach(e => {
  if (e.hasAttribute('srcset')) {
    e.setAttribute('src', e.getAttribute('srcset'));
  } else {
    e.remove();
  }
});

const content = {
  title: 'Title',
  data: doc.body.innerHTML,
}

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

4 participants