SaraVieira / starter-book

A book starter to kickstart your writing journey 🎉
https://saravieira.github.io/starter-book/#/
MIT License
339 stars 29 forks source link

Image not downloaded for Epub? #8

Open nathansebhastian opened 3 years ago

nathansebhastian commented 3 years ago

Hello Sara,

I tried to generate an epub book with the script, but I have image download error from my console:

node generate-book/epub.js
Generating Template Files.....
Downloading Images...
[Download Error] Error while downloading ./cat.jpg { [Error: ENOENT: no such file or directory, open '/Users/nsebhastian/Desktop/starter-book/node_modules/epub-gen/tempDir/b99c020e-17bc-43e4-96f8-f958823440e9/OEBPS/cat.jpg']
  errno: -2,
  code: 'ENOENT',
  syscall: 'open',
  path:
   '/Users/nsebhastian/Desktop/starter-book/node_modules/epub-gen/tempDir/b99c020e-17bc-43e4-96f8-f958823440e9/OEBPS/cat.jpg' }
Making Cover...
[Success] cover image downloaded successfully!
Generating Epub Files...

It seems that the epub-gen library can't download images unless it's absolute path or HTTP? Is there a workaround for this?

Thank you!

SaraVieira commented 3 years ago

Hey

I actually encountered that too and that's why in the end and ended up uploading all images to a Cloudinary account :/

I do not have a workaround, sorry

nathansebhastian commented 3 years ago

Hi Sara,

No worries! I have managed to create a workaround by adding a new image renderer that replace the relative path with absolute path to the image src like this:

export const absoluteImageRenderer = ({ src, alt }) => {
  src = src.replace("./", "http://localhost:9000/")
  return (
    <figure>
      <img src={src} alt={alt} />
      <figcaption>{alt}</figcaption>
    </figure>
  );
}

Then call it only under GATSBY_SCRAPPER:

process.env.GATSBY_SCRAPPER === "1"
       ? {
           heading: headingRenderer,
           root: SmallRootRenderer,
           image: absoluteImageRenderer,
         }
       :

I'm using localhost:9000 because gatsby serve the local site there. May I create a pull request on this issue? Then you can review it afterward :)

SaraVieira commented 3 years ago

Heyy

That is smart!!!

Please do :)