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
21 changes: 12 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ Copyright (c) 2016, Yahoo Inc.
Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
*/

var fs = require('fs'),
const fs = require('fs'),
path = require('path'),
SVGO = require('svgo'),
svgo = new SVGO(),
{optimize} = require('svgo'),
dot = require('dot'),
template = dot.template(fs.readFileSync(path.join(__dirname, 'templates', 'basic.svg'), 'utf-8')),
v2 = require('./v2'),
Expand All @@ -21,8 +20,8 @@ var fs = require('fs'),
* @param {String} color Color text to pick
* @param {Function} callback Function to call when done (error, SVG)
*/
module.exports = function badge (field1, field2, color, callback) {
var data = {
module.exports = async function badge (field1, field2, color, callback) {
const data = {
text: [
utils.escapeXml(field1),
utils.escapeXml(field2)
Expand All @@ -37,10 +36,14 @@ 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 object = optimize(
template(data)
// Due to https://github.com/svg/svgo/issues/1498
.replace(/&#(x3c|60);/gi, '<')
.replace(/&#(x26|38);/gi, '&')
);
if (callback) callback(null, object.data);
return object.data;
};

/**
Expand Down
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,16 @@
}
},
"devDependencies": {
"chai": "^4.2.0",
"coveralls": "^3.0.11",
"eslint": "^6.8.0",
"mocha": "^7.1.1",
"nyc": "^15.0.0",
"sinon": "^9.0.1"
"chai": "^4.3.4",
"coveralls": "^3.1.1",
"eslint": "^8.5.0",
"mocha": "^9.1.3",
"nyc": "^15.1.0",
"sinon": "^12.0.1"
},
"dependencies": {
"css-color-names": "~1.0.1",
"dot": "^1.1.3",
"svgo": "^1.3.2"
"svgo": "^2.8.0"
}
}
63 changes: 31 additions & 32 deletions v2.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,12 @@ Copyright (c) 2016, Yahoo Inc.
Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
*/

var colors = require('css-color-names'),
const colors = require('css-color-names'),
dot = require('dot'),
fs = require('fs'),
path = require('path'),
utils = require('./utils'),
SVGO = require('svgo'),
svgo = new SVGO({
plugins: [{
sortDefsChildren: false
}]
}),
{optimize, extendDefaultPlugins} = 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 @@ -43,29 +38,26 @@ function getColorCode(input) {
}

function sectionsToData(sections) {
var badgeData = {
const badgeData = {
width: 0,
height: 0,
sections: [],
};
sections.forEach(function(section, s) {
var sectionData = {
x: 0,
width: 0,
lines: [],
},
sectionHeight,
text,
lines;
const sectionData = {
x: 0,
width: 0,
lines: [],
};
if (! Array.isArray(section)) {
section = [ section ];
}
text = section.shift();
const text = section.shift();
sectionData.x = badgeData.width;
sectionData.color = (s === 0 ? DEFAULT_COLOR_FIRST : DEFAULT_COLOR_REST);
section.forEach(function(attribute) {
// stroke attribute `s{color}` as CSS color or color code in hex
var strokeAttribute = STROKE_REGEX.exec(attribute);
const strokeAttribute = STROKE_REGEX.exec(attribute);
if (strokeAttribute) {
sectionData.stroke = getColorCode(strokeAttribute[1]) || null;
}
Expand All @@ -77,35 +69,42 @@ function sectionsToData(sections) {
// FUTURE -- text alignment `a{align}` lmr (only matters when multiline)
// FUTURE -- font `f{font}` mainly for monospace (`fm`)
});
lines = text.split('\n');
const lines = text.split('\n');
lines.forEach(function(line, l) {
var lineData = {
x: 0,
y: 0,
text: line,
},
lineWidth;
lineWidth = (2 * PAD_X) + utils.textWidth(lineData.text, DEFAULT_LETTER_WIDTH);
const lineData = {
x: 0,
y: 0,
text: line,
};
const lineWidth = (2 * PAD_X) + utils.textWidth(lineData.text, DEFAULT_LETTER_WIDTH);
lineData.x = badgeData.width + PAD_X;
lineData.y = (LINE_HEIGHT * l) + PAD_Y + LINE_HEIGHT - DECENDER_HEIGHT;
sectionData.lines.push(lineData);
sectionData.width = Math.max(sectionData.width, lineWidth);
});
badgeData.sections.push(sectionData);
sectionHeight = (2 * PAD_Y) + (lines.length * LINE_HEIGHT);
const sectionHeight = (2 * PAD_Y) + (lines.length * LINE_HEIGHT);
badgeData.height = Math.max(badgeData.height, sectionHeight);
badgeData.width += sectionData.width;
});
return badgeData;
}


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;
module.exports = async function badge_v2(sections, callback) {
const raw = TEMPLATE(sectionsToData(sections))
// Due to https://github.com/svg/svgo/issues/1498
.replace(/&#(x3c|60);/gi, '<')
.replace(/&#(x26|38);/gi, '&');

const optimized = optimize(raw, {
plugins: extendDefaultPlugins([{
name: 'sortDefsChildren',
active: false
}])
});
if (callback) callback(undefined, optimized.data);
return optimized.data;
};


Expand Down