Skip to content

Instantly share code, notes, and snippets.

@Ledzz
Created August 22, 2018 08:16
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 Ledzz/84813a198aa23c95f2dcf06c502e8834 to your computer and use it in GitHub Desktop.
Save Ledzz/84813a198aa23c95f2dcf06c502e8834 to your computer and use it in GitHub Desktop.
const fs = require('fs');
fs.readFile('./sprite.svg', 'utf8', (err, data) => {
const regex = /<symbol id='([^']+)' viewBox='([^']+)'>(.*?)<\/symbol>/gs;
const outputDir = './icons';
let match;
const matches = [];
while ((match = regex.exec(data)) !== null) {
const [symbol, name, viewBox, content] = match;
matches.push({name, viewBox, content: content.trim()});
}
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir);
}
matches.forEach(match => {
const content = `<svg viewBox='${match.viewBox}'>${match.content}</svg>`;
fs.writeFileSync(`${outputDir}/${match.name}.svg`, content);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment