forked from hasantayyar/html-inline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
109 lines (100 loc) · 3.43 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
var trumpet = require('trumpet');
var through = require('through2');
var fs = require('fs');
var path = require('path');
module.exports = function (opts) {
if (!opts) opts = {};
var basedir = opts.basedir || process.cwd();
var tr = trumpet();
if (!(opts.ignoreScripts || opts['ignore-scripts'])) {
tr.selectAll('script[src]', function (node) {
var file = fix(node.getAttribute('src'));
node.removeAttribute('src');
fs.createReadStream(file)
.pipe(node.createWriteStream())
;
});
}
if (!(opts.ignoreImages || opts['ignore-images'])) {
tr.selectAll('img[src]', function (node) {
inline64(node, 'src');
})
}
if (!(opts.ignoreLinks || opts['ignore-links'])) {
tr.selectAll('link[href]', function (node) {
var rel = (node.getAttribute('rel') || '').toLowerCase();
if (rel === 'stylesheet') return;
inline64(node, 'href');
})
}
if (!(opts.ignoreStyles || opts['ignore-styles'])) {
tr.selectAll('link[href]', function (node) {
var rel = node.getAttribute('rel').toLowerCase();
if (rel !== 'stylesheet') return;
var file = fix(node.getAttribute('href'));
var w = node.createWriteStream({ outer: true });
w.write('<style>');
var r = fs.createReadStream(file);
r.pipe(w, { end: false });
r.on('end', function () { w.end('</style>') });
});
}
return tr;
function fix (p) {
if(path.isAbsolute(p)) {
return path.resolve(basedir, path.relative('/', p));
} else {
return path.resolve(basedir, p);
}
}
function enc (s) {
return s.replace(/"/g, '"')
.replace(/>/g, '>')
.replace(/</g, '<')
;
}
function inline64 (node, name) {
var href = node.getAttribute(name);
if (/^data:/.test(href)) return;
var file = fix(href);
var w = node.createWriteStream({ outer: true });
var attrs = node.getAttributes();
w.write('<' + node.name);
Object.keys(attrs).forEach(function (key) {
if (key === name) return;
w.write(' ' + key + '="' + enc(attrs[key]) + '"');
});
var ext = path.extname(file).replace(/^\./, '').toLowerCase();
var type = node.getAttribute('type')
if (!type) type = {
svg: 'image/svg+xml',
png: 'image/png',
jpg: 'image/jpeg',
jpeg: 'image/jpeg',
gif: 'image/jpeg'
}[ext] || 'image/png'
w.write(' ' + name + '="data:' + type + ';base64,');
fs.createReadStream(file).pipe(through(write, end));
var bytes = 0, last = null;
function write (buf, enc, next) {
if (last) {
buf = Buffer.concat([ last, buf ]);
last = null;
}
var b;
if (buf.length % 3 === 0) {
b = buf;
}
else {
b = buf.slice(0, buf.length - buf.length % 3);
last = buf.slice(buf.length - buf.length % 3);
}
w.write(b.toString('base64'));
next();
}
function end () {
if (last) w.write(last.toString('base64'));
w.end('">');
}
}
};