Skip to content

Instantly share code, notes, and snippets.

@dfa1234
Created December 12, 2021 15:44
Show Gist options
  • Save dfa1234/cc582ee644524a6bafa798c6d0ada250 to your computer and use it in GitHub Desktop.
Save dfa1234/cc582ee644524a6bafa798c6d0ada250 to your computer and use it in GitHub Desktop.
// Simple node file to remove fill attribute from svg (for using in angular, mat icon, or other...)
// External libraries needed: "xmldoc": "^1.1.2"
const fs = require('fs');
const xmldoc = require('xmldoc');
const removeFillAttrRecursively = (node)=> {
if(node?.attr?.fill){
delete node.attr.fill;
}
if(node?.children?.length){
node.children.map(child => {
return removeFillAttrRecursively(child);
})
}
return node;
}
const dirIn = `images/icons-in`;
const dirOut = `images/icons-out`;
console.log('#### start ####');
const dirList = fs.readdirSync(dirIn);
if (!fs.existsSync(dirOut)){
fs.mkdirSync(dirOut);
}
dirList.forEach((svgFilename) => {
const fileContent = fs.readFileSync(`${dirIn}/${svgFilename}`,{encoding: 'utf-8'});
const document = new xmldoc.XmlDocument(fileContent);
const resultDoc = removeFillAttrRecursively(document);
console.log('Writing', svgFilename);
fs.writeFileSync(`${dirOut}/${svgFilename}`, resultDoc.toString(),{encoding: 'utf-8'});
})
console.log('#### done ####');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment