badgen.net/libs/icons.js

38 wiersze
980 B
JavaScript
Czysty Zwykły widok Historia

2018-07-25 06:58:32 +00:00
const fs = require('fs')
const { join, parse } = require('path')
2018-07-27 13:16:31 +00:00
function genIcons (iconFolder, whiten) {
2018-07-27 03:31:21 +00:00
const icons = {}
2018-07-25 06:58:32 +00:00
2018-07-27 03:31:21 +00:00
fs.readdirSync(join(__dirname, iconFolder)).forEach(filename => {
const imageType = {
'.svg': 'svg+xml',
'.png': 'png'
}[parse(filename).ext]
2018-07-25 06:58:32 +00:00
2018-07-27 03:31:21 +00:00
if (!imageType) return
2018-07-25 06:58:32 +00:00
2018-07-27 03:31:21 +00:00
const key = parse(filename).name
const iconFile = join(__dirname, iconFolder, filename)
2018-07-27 13:16:31 +00:00
const svgSource = fs.readFileSync(iconFile, 'utf8')
const svg = whiten ? whitenSVG(svgSource) : svgSource
2018-07-27 03:31:21 +00:00
const b64 = Buffer.from(svg).toString('base64')
2018-07-25 06:58:32 +00:00
2018-07-27 03:31:21 +00:00
icons[key] = `data:image/${imageType};base64,${b64}`
})
2018-07-25 06:58:32 +00:00
2018-07-27 03:31:21 +00:00
return icons
}
2018-07-27 13:16:31 +00:00
function whitenSVG (svg, whiten) {
return svg
.replace(/fill="#\w{3,6}"/g, 'fill="white"')
.replace(/stroke="#\w{3,6}"/g, 'stroke="white"')
.replace(/<path /g, '<path fill="white" ')
}
2018-07-27 03:31:21 +00:00
module.exports = {
builtin: genIcons('icons')
// simple: genIcons('../node_modules/simple-icons/icons', true)
2018-07-27 03:31:21 +00:00
}