-
Notifications
You must be signed in to change notification settings - Fork 167
/
Copy pathinline-images.js
80 lines (75 loc) · 2.19 KB
/
inline-images.js
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import { parseSrcset, stringifySrcset } from 'srcset';
import { getMimetypeFromURL, isImageURL } from './util/file-mimetype.js';
import fetchBase64 from './util/fetch-base64.js';
export default async function inlineImages(doc, fetchOptions = {}, out) {
if (out) {
out.write('Inlining images...\n');
}
let src_promises = Array.from(
doc.querySelectorAll('picture source[src], img[src]')
).map(async el => {
/*
For web pages using atypical URLs for images
let’s just use a generic MIME type and hope it works.
For an example, see:
https://github.com/danburzo/percollate/issues/174
*/
let mime = isImageURL(el.src, doc)
? getMimetypeFromURL(el.src, doc)
: 'image';
if (out) {
out.write(el.src + '\n');
}
let data = await fetchBase64(el.src, fetchOptions);
el.setAttribute('src', `data:${mime};base64,${data}`);
});
let srcset_promises = Array.from(
doc.querySelectorAll('picture source[srcset], img[srcset]')
).map(async el => {
if (el.getAttribute('src')) {
/*
If a `src` is present on the <img>/<source> element,
let's use that instead of fetching all the images
defined in the `srcset`, which may increase the size
of the EPUB file.
This is a stop-gap solution until we figure out
a more sophisticated way of telling which image
out of the set has the best quality.
*/
el.removeAttribute('srcset');
return;
}
try {
const items = parseSrcset(el.getAttribute('srcset'));
el.setAttribute(
'srcset',
stringifySrcset(
await Promise.all(
items.map(async item => {
/*
For web pages using atypical URLs for images
let’s just use a generic MIME type and hope it works.
For an example, see:
https://github.com/danburzo/percollate/issues/174
*/
let mime = isImageURL(item.url, doc)
? getMimetypeFromURL(item.url, doc)
: 'image';
let data = await fetchBase64(
item.url,
fetchOptions
);
return {
...item,
url: `data:${mime};base64,${data}`
};
})
)
)
);
} catch (err) {
console.error(err);
}
});
return Promise.all(src_promises.concat(srcset_promises));
}