-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
37 lines (32 loc) · 1.11 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
var path = require('path');
var fs = require('fs');
var mimetypes = {
'.gif': 'image/gif',
'.jpeg': 'image/jpeg',
'.jpg': 'image/jpeg',
'.jpe': 'image/jpeg',
'.png': 'image/png'
};
var encode = function( filePath ) {
return new Buffer(fs.readFileSync(filePath)).toString('base64');
};
module.exports = function( inputText, inputName, outputName ) {
if (!inputText || !inputName) {
return inputText;
}
var transformedText = inputText,
regex = /url\((?:\\?['"])?([\w\/\\._-]*?)([\w-_]+\.\w{3,4})\\?['"]?\)/g,
// capture group $1 (path)
// capture group $2 (filename)
found = inputText.match(regex);
if (found) {
transformedText = inputText.replace(regex, function( match, filePath, fileName ) {
var fullPath = path.dirname(inputName) + '/' + (filePath ? filePath : '') + fileName,
prefix = 'data:' + mimetypes[path.extname(fileName.toLowerCase())] + ';base64,',
falseFind = match.charAt === '#';
var encodedFile = encode(fullPath);
return falseFind ? match : 'url(' + prefix + encodedFile + ')';
});
}
return transformedText;
};