UNPKG

badgen-font-color

Version:
49 lines (38 loc) 1.36 kB
const { badgen } = require('badgen'); // https://github.com/badgen/badgen/blob/master/src/color-presets.ts const colorPresets = { green: '3C1', blue: '08C', red: 'E43', yellow: 'DB1', orange: 'F73', purple: '94E', pink: 'E5B', grey: '999', gray: '999', cyan: '1BC', black: '2A2A2A' }; const fontColor = (str, color) => { const regex = /<text x="([0-9]*)" y="([0-9]*)" textLength="([0-9]*)">(.*)<\/text>\n <\/g>/gm; const result = regex.exec(str) if (result) { return str.replace(result[0], `<text x="${result[1]}" y="${result[2]}" textLength="${result[3]}" fill="#${colorPresets[color] || color}">${result[4]}</text>\n </g>`) } return str }; const labelFontColor = (str, color) => { const regex = /<text x="([0-9]*)" y="([0-9]*)" textLength="([0-9]*)">(.*)<\/text>/gm; const result = regex.exec(str) if (result) { return str.replace(result[0], `<text x="${result[1]}" y="${result[2]}" textLength="${result[3]}" fill="#${colorPresets[color] || color}">${result[4]}</text>`) } return str }; module.exports = function (options) { const svg = badgen(options) if (!options.labelFontColor && !options.fontColor) { return svg } return fontColor(labelFontColor(svg, options.labelFontColor || "fff"), options.fontColor || "fff"); };