This repository has been archived by the owner on Jul 4, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·81 lines (68 loc) · 1.93 KB
/
index.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
81
var cheerio = require('cheerio')
var path = require('path')
var fs = require('fs')
var url = require('url')
var sharp = require('sharp')
var through = require('through2')
var gutil = require('gulp-util')
var PluginError = gutil.PluginError
var contentTypes = {
".png": "image/png",
".gif": "image/gif",
".jpg": "image/jpeg",
".jpeg": "image/jpeg",
".bmp": "image/bmp",
".webp": "image/webp"
}
var toBase64 = function (extension, data) {
return 'data:' + contentTypes[extension] + ';base64,' + data.toString('base64');
};
function inliner(html, base) {
base = base || process.cwd()
var dom = cheerio.load(String(html))
inlineImages(dom)
return new Buffer(dom.html({decodeEntities: false}))
function inlineImages(dom) {
var styles = [];
dom('img').each(function(idx, el) {
el = dom(el)
var src = el.attr('src')
if (src && isLocal(src)) {
var dir = path.dirname(src)
var loc = path.join(base, src)
var extension = path.extname(loc)
sharp(loc)
.resize(14) // resize to 16px width and auto height
.toBuffer() // converts to buffer for Base64 conversion
.then(data => {
presource = toBase64(extension, data)
el.attr('src', presource)
el.attr('data-CogitipLoad-src', src)
})
.catch(err => {
console.error("FUCK FUCK ")
console.error(err)
})
}
})
}
function isLocal(href) {
return href && !url.parse(href).hostname;
}
}
function gulpExport(dir) {
var stream = through.obj(function(file, enc, cb) {
if (file.isStream()) {
this.emit('error', new PluginError(PLUGIN_NAME, 'Streams not supported!'));
return cb();
}
if (file.isBuffer()) {
file.contents = inliner(file.contents, dir);
this.push(file);
return cb();
}
return cb(null, file); //no-op
});
return stream;
}
module.exports = gulpExport;