UNPKG

edge-iconify

Version:

Iconify integration for the Edge template engine

78 lines (75 loc) 2.37 kB
// index.ts import { edgeGlobals as edgeGlobals2 } from "edge.js"; // src/svg_generator.ts import { edgeGlobals } from "edge.js"; import { getIcon, buildIcon } from "iconify-icon"; var SvgGenerator = class { #options; constructor(options = {}) { this.#options = { scale: 1, ...options }; } /** * Generate svg for the given icon name */ generate(name, props) { const icon = getIcon(name); if (!icon) { throw new Error(`Cannot locate icon "${name}". Make sure you have registered the icon bundle`); } const { rotate, hFlip, vFlip, width, height, size, ...attributes } = props || {}; const svg = buildIcon(icon, { rotate: rotate === void 0 && hFlip ? 0 : rotate, hFlip, vFlip, width: width || this.#options.scale + "em", height: height || this.#options.scale + "em" }); if (size && size === "none") { svg.attributes.width = false; svg.attributes.height = false; } const propsClass = (attributes.class || "").split(" "); const defaultClass = (this.#options.defaultClass || "").split(" "); const className = [...propsClass, ...defaultClass].filter(Boolean).join(" "); if (className) { attributes.class = className; } return `<svg ${edgeGlobals.html.attrs({ ...svg.attributes, ...attributes }).value}>${svg.body}</svg>`; } }; // index.ts import { addCollection, addIcon } from "iconify-icon"; var edgeIconify = (edge, _, options) => { const svgGenerator = new SvgGenerator(options); edge.global( "svg", function(name, props) { return edgeGlobals2.html.safe(svgGenerator.generate(name, props)); } ); edge.registerTag({ block: false, seekable: true, tagName: "svg", compile(parser, buffer, token) { const parsed = parser.utils.transformAst( parser.utils.generateAST(token.properties.jsArg, token.loc, token.filename), token.filename, parser ); const openingBrace = parsed.type !== "SequenceExpression" ? "(" : ""; const closingBrace = parsed.type !== "SequenceExpression" ? ")" : ""; buffer.outputExpression( `state.svg${openingBrace}${parser.utils.stringify(parsed)}${closingBrace}.value`, token.filename, token.loc.start.line, false ); } }); }; export { addCollection, addIcon, edgeIconify };