-
Notifications
You must be signed in to change notification settings - Fork 1
/
convertIcons.js
61 lines (49 loc) · 2.09 KB
/
convertIcons.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import { JSDOM } from 'jsdom';
import glob from 'glob';
import fs from 'fs';
function convertSvgIcon(svgString, name = 'icon') {
// Load the input SVG string into a DOM object
const inputSvg = new JSDOM(svgString).window.document.querySelector('svg');
// Create a new SVG element
const outputSvg = inputSvg.ownerDocument.createElementNS('http://www.w3.org/2000/svg', 'svg');
// Add the XML namespace attribute to the new SVG element
outputSvg.setAttribute('xmlns', 'http://www.w3.org/2000/svg');
// Create a new symbol element
const defs = outputSvg.ownerDocument.createElementNS('http://www.w3.org/2000/svg', 'defs');
const symbol = outputSvg.ownerDocument.createElementNS('http://www.w3.org/2000/svg', 'symbol');
defs.appendChild(symbol);
// Extract necessary attributes
const attributes = {
id: name,
viewBox: inputSvg.getAttribute('viewBox') || '0 0 24 24',
'stroke-width': inputSvg.getAttribute('stroke-width') || '2',
stroke: inputSvg.getAttribute('stroke') || 'currentColor',
fill: inputSvg.getAttribute('fill') || 'none',
'stroke-linecap': inputSvg.getAttribute('stroke-linecap') || 'round',
'stroke-linejoin': inputSvg.getAttribute('stroke-linejoin') || 'round'
};
// Set attributes on the symbol element
Object.keys(attributes).forEach((key) => {
symbol.setAttribute(key, attributes[key]);
});
const childArr = [];
for (let i = 0; i < inputSvg.children.length; i++) {
const child = inputSvg.children.item(i);
childArr.push(child);
}
// Move all child nodes from the input SVG to the symbol element
childArr.forEach((child) => {
symbol.appendChild(child);
});
// Add the symbol element to the new SVG element
outputSvg.appendChild(defs);
// Return the string representation of the new SVG
return outputSvg.outerHTML;
}
const svgPaths = glob.sync('static/icons/original/*.svg', { absolute: true });
svgPaths.forEach((path) => {
const newPath = path.replace('static/icons/original', 'static/icons');
const file = fs.readFileSync(path, 'utf8');
const fileName = path.split('/').pop().split('.').shift();
fs.writeFileSync(newPath, convertSvgIcon(file, fileName));
});