forked from marijnh/Eloquent-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathadd_images_to_epub.js
More file actions
25 lines (23 loc) · 838 Bytes
/
add_images_to_epub.js
File metadata and controls
25 lines (23 loc) · 838 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
const {readdirSync, lstatSync, readFileSync, writeFileSync} = require("fs")
const path = require("path")
let images = []
function scanDir(dir) {
for (let file of readdirSync(dir)) {
let full = path.resolve(dir, file), type
if (lstatSync(full).isDirectory())
return scanDir(full)
if (/\.svg$/.test(file))
type = "image/svg+xml"
else if (/\.png$/.test(file))
type = "image/png"
else if (/\.jpg$/.test(file))
type = "image/jpeg"
else
throw new Error("Unknown image type: " + full)
let local = full.slice(full.indexOf("/img/") + 1)
images.push(` <item id="image.${file}" href="${local}" media-type="${type}"/>`)
}
}
scanDir("epub/img")
let out = readFileSync("epub/content.opf.src", "utf8").replace("{{images}}", images.join("\n"))
writeFileSync("epub/content.opf", out)