Skip to content

Instantly share code, notes, and snippets.

@dennishall1
Created March 17, 2016 19:06
Show Gist options
  • Save dennishall1/3e96d52e73db20b27cd0 to your computer and use it in GitHub Desktop.
Save dennishall1/3e96d52e73db20b27cd0 to your computer and use it in GitHub Desktop.
Split SVG Spritesheet into individual SVG files
var fs = require('fs');
var path = require('path');
var markup = fs.readFileSync('sprite.svg').toString();
var lines = markup.split(/\n/g);
var symbols = {};
var currentSymbol = null;
lines.forEach(function(line){
var open = line.match(/symbol.*?id="(.*?)"/);
var close = line.match(/<\/symbol>/);
if(currentSymbol){
symbols[currentSymbol].push(line);
}
if(open){
currentSymbol = open[1];
symbols[currentSymbol] = [line];
}
if(close){
symbols[currentSymbol] = symbols[currentSymbol].join('\n').replace('<symbol', '<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"').replace('</symbol', '</svg');
fs.writeFileSync(path.join(__dirname, 'src/svg/' + currentSymbol + '.svg'), symbols[currentSymbol]);
currentSymbol = null;
}
});
console.log( Object.keys(symbols) );
@GrossDesignCo
Copy link

I had to do this recently so I made a little web app for it:

https://spritemap-to-svg.vercel.app

Hope this helps someone!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment