Skip to content

Instantly share code, notes, and snippets.

@voskresla
Created June 18, 2020 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save voskresla/658abd243d1718abe56e71a00f68b8e0 to your computer and use it in GitHub Desktop.
Save voskresla/658abd243d1718abe56e71a00f68b8e0 to your computer and use it in GitHub Desktop.
prepare-svg
const fs = require('fs')
const SVGO = require('svgo')
const svgo = new SVGO({
plugins: [
{ removeXMLNS: true },
{ sortAttrs: true },
{ removeDimensions: true },
],
})
fs.readdirSync('./icons').forEach(file => {
fs.renameSync('./icons/' + file, './icons/' + file.replace(/[ ']/g, '-'))
})
const stopList = ['star-half', 'zoom-out', 'zoom']
const stopListFilter = (name) => {
const [filename] = name.split('.')
if (stopList.includes(filename)) {
console.warn(`Иконка ${name} требует ручной обработки. Возможно это dualtone.`)
return false
}
return true
}
const svgFilter = (file) => {
const [name, ext] = file.split('.')
return ext === 'svg'
}
fs.readdirSync('./icons')
.filter(stopListFilter)
.filter(svgFilter)
.forEach(file => {
const filepath = './icons/' + file
const icon = fs.readFileSync(filepath, 'utf-8')
svgo.optimize(icon).then(({ data }) => {
fs.writeFileSync(
filepath,
data
.replace(/ fill="[^white|none]\S+"/g, ' fill="currentColor"')
.replace(/ stroke="[^white]\S+"/g, ' stroke="currentColor"')
)
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment