Created
March 17, 2016 19:06
-
-
Save dennishall1/3e96d52e73db20b27cd0 to your computer and use it in GitHub Desktop.
Split SVG Spritesheet into individual SVG files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) ); |
Thanks man <3
How to use this?
How to use this?
- Save to a file
- have your sprite.svg in the same directory
- might need to create
src/svg/
path in the directory you have this script and sprite - execute using nodeJS
-> your files will be insrc/svg/
How to use this?
- Save to a file
- have your sprite.svg in the same directory
- might need to create
src/svg/
path in the directory you have this script and sprite- execute using nodeJS
-> your files will be insrc/svg/
I've tried alot of things can you write this? So its a question of copy pasting and running a command in nodejs for that location.
I'm a noob here.
Thanks, man. <3
Worked very well thanks a lot!
I can't use it. It gives me an error.
Line: 3
Character: 1
Error: It was waiting for an object
Code: 800A138F
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Served me very well. Thank you for sharing!