Commit be28cb6b authored by Stefan Galinski's avatar Stefan Galinski 🎮
Browse files

[FEATURE] Allow to work with recent fontawesome version that has subdirectories

parent 1411e9b3
......@@ -4,7 +4,6 @@ const fs = require('fs');
const parseXmlStringSync = require('xml2js-parser').parseStringSync;
const globby = require('globby');
const _ = require('underscore');
const util = require('util');
const svgToMiniDataURI = require('mini-svg-data-uri');
const imagemin = require('imagemin');
const imageminSvgo = require('imagemin-svgo');
......@@ -37,13 +36,14 @@ module.exports = class InlineSvg {
*/
async _process(_svgFilePath) {
return new Promise(async (_resolve, _reject) => {
try {
let svgs = await this._readFiles(_svgFilePath);
let templateContent = await imagemin.buffer(this._getTemplateContent(), {
plugins: [
imageminSvgo()
]
});
svgs.forEach(svg => this._processSvg(svg))
svgs.forEach(svg => this._processSvg(svg, _svgFilePath));
let template = mustache.render(
templateContent.toString(),
_.extend(
......@@ -55,6 +55,9 @@ module.exports = class InlineSvg {
)
);
_resolve(template);
} catch (e) {
console.log(e);
}
});
}
......@@ -66,15 +69,18 @@ module.exports = class InlineSvg {
* data.fileName: A string containing the file name
*
* @param {Object} _data Object containing info about the SVG
* @param {String} _svgFilePath Original File Path from the fetched SVG
*/
_processSvg(_data) {
_processSvg(_data, _svgFilePath) {
const xmlString = parseXmlStringSync(_data.content.toString(), {
attrNameProcessors: [name => name.toLowerCase()]
});
const svgDimensions = this._getSvgDimensions(xmlString);
const _svgFilePathAdjusted = _svgFilePath.replace('**', '');
const svgData = {
name: path.basename(_data.fileName, '.svg'),
inline: svgToMiniDataURI(_data.content),
name: _data.fileName.replace(_svgFilePathAdjusted, '').replace(/\//g, '-').replace('.svg', '').replace('fontawesome-', ''),
// add fill="white" to allow later color changes based on this value
inline: svgToMiniDataURI(_data.content.indexOf('fill=') === -1 ? _data.content.replace('<svg', '<svg fill="white"') : _data.content),
width: parseInt(svgDimensions.width) + 'px',
height: parseInt(svgDimensions.height) + 'px',
dimensions: svgDimensions
......@@ -140,5 +146,4 @@ module.exports = class InlineSvg {
_getTemplateContent() {
return fs.readFileSync(this._options.template);
}
};
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment