-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
icons.js
35 lines (29 loc) · 943 Bytes
/
icons.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import { resolve } from 'path'
import { readFileSync } from 'fs'
import { getIconData, iconToSVG, replaceIDs } from '@iconify/utils';
const iconsPath = resolve(__dirname, 'node_modules/@iconify-json/mdi/icons.json')
const iconsData = JSON.parse(readFileSync(iconsPath))
// console.log(Object.keys(iconsData))
const svgAttributesBase = {
'xmlns': 'http://www.w3.org/2000/svg',
'xmlns:xlink': 'http://www.w3.org/1999/xlink',
}
export const getIconSVG = function(name) {
const icon = getIconData(iconsData, name)
if (!icon) return
const renderData = iconToSVG(icon, {
height: 'auto',
});
const svgAttributes = {
...svgAttributesBase,
...renderData.attributes,
};
const svgAttributesStr = Object.keys(svgAttributes)
.map(
(attr) => `${attr}="${svgAttributes[attr]}"`
)
.join(' ');
// Generate SVG
const svg = `<svg ${svgAttributesStr}>${replaceIDs(renderData.body)}</svg>`;
return svg
}