forked from lakmeer/gulp-css-urlencode-inline-svgs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
38 lines (29 loc) · 912 Bytes
/
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
var through = require('through2');
var gutil = require('gulp-util');
const PLUGIN_NAME = 'gulp-css-urlencode-inline-svgs';
const delimiter = '}\n';
const detect = /data:image\/svg\+xml/g;
const target = /<svg[^;]+\<\/svg\>/g;
function urlencodeSvgInText (input) {
var lines = input.toString().split(delimiter);
for (var i in lines) {
if (lines[i].match(detect)) {
lines[i] = lines[i].replace(target, encodeURIComponent);
}
}
return lines.join(delimiter);
}
module.exports = function gulpCssUrlencodeInlineSvgs () {
return through.obj(function(file, enc, cb) {
if (file.isNull()) {
return cb(null, file);
}
if (file.isStream()) {
cb(new gutil.PluginError(PLUGIN_NAME, 'No support for streams.'));
}
if (file.isBuffer()) {
file.contents = new Buffer( urlencodeSvgInText(file.contents) );
}
cb(null, file);
});
}