Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ Copyrights licensed under the New BSD License. See the accompanying LICENSE file

var fs = require('fs'),
path = require('path'),
SVGO = require('svgo'),
svgo = new SVGO(),
svgo = require('svgo'),
dot = require('dot'),
template = dot.template(fs.readFileSync(path.join(__dirname, 'templates', 'basic.svg'), 'utf-8')),
v2 = require('./v2'),
Expand Down Expand Up @@ -37,10 +36,19 @@ module.exports = function badge (field1, field2, color, callback) {
};

// Run the SVG through SVGO.
return svgo.optimize(template(data)).then(function (object) {
if (callback) callback(null, object.data);
return object.data;
const raw = utils.fixupNumericEntities(template(data));
const optimized = svgo.optimize(raw, {
plugins: ['preset-default'],
});
if (optimized.modernError) {
if (callback) {
callback(optimized.modernError, undefined);
return;
}
return Promise.reject(optimized.modernError);
}
if (callback) callback(null, optimized.data);
return Promise.resolve(optimized.data);
};

/**
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,6 @@
"dependencies": {
"css-color-names": "~1.0.1",
"dot": "^1.1.3",
"svgo": "^1.3.2"
"svgo": "2.6.0"
}
}
13 changes: 12 additions & 1 deletion utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,18 @@ var LETTER_WIDTH = [
8,8,8,8,8,7,7,7,6,6,6,6,6,6,9,5,6,6,6,6,3,3,3,3,6,6,6,6,6,6,6,6,7,6,6,6,
6,5,6
];

/**
* Replace numeric entity codes with the related entity code name
* for the "<" and "&" characters. Prevent svgo "Unencoded ..." errors,
* see https://github.com/svg/svgo/issues/1498.
* @method replaceNumericEntities
* @param {String} string Input string
* @return {String} Fixed string
*/
module.exports.fixupNumericEntities = function replaceNumericEntitities(string) {
return string.replace(/&#(x3c|60);/gi, '&lt;')
.replace(/&#(x26|38);/gi, '&amp;');
}

/**
* Escape the string so that it doesn't break xml
Expand Down
36 changes: 25 additions & 11 deletions v2.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,7 @@ var colors = require('css-color-names'),
fs = require('fs'),
path = require('path'),
utils = require('./utils'),
SVGO = require('svgo'),
svgo = new SVGO({
plugins: [{
sortDefsChildren: false
}]
}),
svgo = require('svgo'),
TEMPLATE = dot.template(fs.readFileSync(path.join(__dirname, 'templates', 'v2.svg'), 'utf-8')),
COLOR_REGEX = /^[0-9a-f]{6}$/i,
STROKE_REGEX = /^s\{(.+?)\}$/i,
Expand Down Expand Up @@ -101,11 +96,30 @@ function sectionsToData(sections) {


module.exports = function badge_v2(sections, callback) {
var raw = TEMPLATE(sectionsToData(sections));
return svgo.optimize(raw).then(function(optimized) {
if (callback) callback(undefined, optimized.data);
return optimized.data;
});
var raw = utils.fixupNumericEntities(
TEMPLATE(sectionsToData(sections))
);
const optimized = svgo.optimize(raw, {
plugins: [
{
name: 'preset-default',
params: {
overrides: {
sortDefsChildren: false,
}
}
}
],
})
if (optimized.modernError) {
if (callback) {
callback(optimized.modernError, undefined);
return;
}
return Promise.reject(optimized.modernError);
}
if (callback) callback(undefined, optimized.data);
return Promise.resolve(optimized.data);
};


Expand Down