Skip to content

iconToHTML()

This function is part of Iconify Utils package.

Function iconToHTML() generates SVG from a list of attributes for <svg> element and icon content.

It can be used with results of iconToSVG(), parseSVGContent() or buildParsedSVG().

Usage

Function has the following parameters:

  • body, string. Icon content, without <svg> tag.
  • attributes, Record<string,string>. Attributes to add, such as viewBox.

Function returns string.

Notes

Function adds the following attributes automatically, do not add them in attributes parameter:

  • "xmlns": always added.
  • "xmlns:xlink": added if body contains "xlink:". Modern icons should not contain "xlink:" attributes.

Attribute values are not modified. Make sure they are escaped before calling function.

Example

example.ts
tsimport { icons } from '@iconify-json/codicon';
import { getIconData, iconToSVG, iconToHTML, replaceIDs } from '@iconify/utils';

const iconName = 'debug-console';

// Get content for icon
const iconData = getIconData(icons, iconName);
if (!iconData) {
   throw new Error(`Icon "${iconName}" is missing`);
}

// Use it to render icon
const renderData = iconToSVG(iconData, {
   height: 'auto',
});

// Generate SVG string
const svg = iconToHTML(replaceIDs(renderData.body), renderData.attributes);

// Log SVG
console.log(svg);

Released under the Apache 2.0 License.