Skip to content

Instantly share code, notes, and snippets.

@mhmdnsr
Last active July 1, 2022 02:56
Show Gist options
  • Save mhmdnsr/18343cceb12858c8b148ef31d1e5a32b to your computer and use it in GitHub Desktop.
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.
// 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