UNPKG

@ikona/cli

Version:
34 lines (33 loc) 1.02 kB
// src/icons/generate-svg-sprite.ts import { parse } from "node-html-parser"; function generateSvgSprite(iconsData) { const symbols = iconsData.map((iconData) => { const input = iconData.content; const root = parse(input); const svg = root.querySelector("svg"); if (!svg) throw new Error("No SVG element found"); svg.tagName = "symbol"; svg.setAttribute("id", iconData.name); svg.removeAttribute("xmlns"); svg.removeAttribute("xmlns:xlink"); svg.removeAttribute("version"); svg.removeAttribute("width"); svg.removeAttribute("height"); return svg.toString().trim(); }); return [ `<?xml version="1.0" encoding="UTF-8"?>`, `<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="0" height="0">`, `<defs>`, // for semantics: https://developer.mozilla.org/en-US/docs/Web/SVG/Element/defs ...symbols, `</defs>`, `</svg>`, "" // trailing newline ].join("\n"); } export { generateSvgSprite };