Commit 1d9b6ef4 authored by Philipp Nowinski's avatar Philipp Nowinski
Browse files

[FEATURE] add better url encoding

parent 9e8e49bf
......@@ -5,6 +5,7 @@ const parseXmlStringSync = require('xml2js-parser').parseStringSync;
const globby = require('globby');
const _ = require('underscore');
const util = require('util');
const svgToMiniDataURI = require('mini-svg-data-uri');
module.exports = class InlineSvg {
......@@ -67,7 +68,7 @@ module.exports = class InlineSvg {
const svgDimensions = this._getSvgDimensions(xmlString);
const svgData = {
name: path.basename(_data.fileName, '.svg'),
inline: this._urlEncode(_data.content),
inline: svgToMiniDataURI(_data.content),
width: parseInt(svgDimensions.width) + 'px',
height: parseInt(svgDimensions.height) + 'px',
dimensions: svgDimensions
......@@ -115,7 +116,7 @@ module.exports = class InlineSvg {
let files = await globby(path.join(_path, '*.svg'));
let svgs = [];
await files.forEach(async _file => {
let content = fs.readFileSync(_file);
let content = fs.readFileSync(_file, 'utf-8');
svgs.push({
fileName: _file,
content: content
......@@ -134,19 +135,4 @@ module.exports = class InlineSvg {
return fs.readFileSync(this._options.template, 'utf-8');
}
/**
* Returns the SVG content as an URL encoded string
*
* @param {Buffer} _content The SVG content
* @returns {String}
*/
_urlEncode(_content) {
_content = _content.toString('utf8');
_content = _content.replace(/"/g, "'");
_content = _content.replace(/\s+/g, " ");
_content = _content.replace(/[{}\|\\\^~\[\]`"<>#%]/g, match => '%'+match[0].charCodeAt(0).toString(16).toUpperCase());
return 'data:image/svg+xml;charset=utf8,' + _content.trim();
}
};
{
"name": "@sgalinski/inline-svg",
"version": "1.0.0",
"version": "1.0.2",
"lockfileVersion": 1,
"requires": true,
"dependencies": {
......@@ -1319,6 +1319,11 @@
"integrity": "sha512-jf84uxzwiuiIVKiOLpfYk7N46TSy8ubTonmneY9vrpHNAnp0QBt2BxWV9dO3/j+BoVAb+a5G6YDPW3M5HOdMWQ==",
"dev": true
},
"mini-svg-data-uri": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/mini-svg-data-uri/-/mini-svg-data-uri-1.0.1.tgz",
"integrity": "sha512-KJ3cjR4kJIP4RroDIXqVTOX0hDYaFMmeHPXqwakVuJmak31uB4+DEqK2L7cedtYHUOdQgh23YsXnAIOHLvjM0g=="
},
"minimatch": {
"version": "3.0.4",
"resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.0.4.tgz",
......
......@@ -19,6 +19,7 @@
"license": "MIT",
"dependencies": {
"globby": "^8.0.1",
"mini-svg-data-uri": "^1.0.1",
"mustache": "^2.3.2",
"underscore": "^1.9.1",
"xml2js-parser": "^1.1.1"
......
Markdown is supported
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