Last active
July 1, 2022 02:56
-
-
Save mhmdnsr/18343cceb12858c8b148ef31d1e5a32b to your computer and use it in GitHub Desktop.
This code can parse a SVG Sprite image to it's original svgs.
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
// Install hast-util-to-html | |
// Install svg-parser | |
import {toHtml} from "hast-util-to-html"; | |
import {parse} from "svg-parser"; | |
import * as fs from 'fs'; | |
fs.readFile('input/path/sprite.svg', 'utf8', function (err, contents) { | |
const parsed = parse(contents); | |
const symbols = parsed.children[0].children; | |
symbols.forEach(symbol => { | |
const name = symbol.properties.id; | |
symbol.tagName = "svg"; | |
let newIcon = toHtml(symbol); | |
fs.writeFile(`output/path/${name}.svg`, newIcon, () => { | |
console.log(name); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment