-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
41 lines (32 loc) · 1.17 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
var through2 = require('through2');
var _ = require('lodash');
var fs = require( 'fs' );
module.exports = function (opts) {
return through2.obj(function (chunk, enc, callback) {
var string = chunk.toString(),
regex = /<img(?:.*?)>/g,
foundImgs = string.match(regex);
if ( foundImgs ) {
var foundSvgs =_.filter(foundImgs, function( it ) {
return _.includes(it, 'data-inline-svg');
});
if ( foundSvgs && foundSvgs.length > 0 ) {
_.each(foundSvgs, function( it ) {
var srcMatch = /src=(?:"|\\'|')/;
var classMatch = /class=(?:"|\\'|')/;
var endMatch = /(?:"|\\'|')/;
var filePath = opts.basePath + it.split( srcMatch )[1].split( endMatch )[0];
var contents = fs.readFileSync(filePath, 'utf8');
if ( _.includes(it, 'class="') ) {
var classes = it.split( classMatch )[1].split( endMatch )[0];
var classString = 'class="' + classes + '" ';
contents = contents.replace('<svg', '<svg ' + classString);
}
string = string.replace( it, contents.trim() );
});
}
}
this.push(string);
callback();
});
};