Skip to content

Commit 865cd05

Browse files
authored
Merge pull request #1099 from FWeinb/feature/extract-viewbox-from-svg
extract viewbox from svg use in declartion
2 parents a7a0afc + 802d565 commit 865cd05

File tree

2 files changed

+44
-5
lines changed

2 files changed

+44
-5
lines changed

custom-icons-builder.js

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,35 @@ function extractPath(svgContent) {
1010
return m ? m[1] : "";
1111
}
1212

13+
function extractViewBox(svgContent) {
14+
const m = svgContent.match(/<svg.+?viewBox="([^"]+)"/);
15+
if (m) {
16+
const parts = m[1].split(" ").map(Number);
17+
if (parts.length === 4) {
18+
return parts;
19+
} else {
20+
throw new Error("Invalid viewBox format: " + m[1]);
21+
}
22+
}
23+
// Fallback to default viewBox if not found
24+
return [0, 0, 24, 24];
25+
}
26+
1327
const files = fs.readdirSync(ICONS_DIR).filter(f => f.endsWith(".svg"));
1428

1529
let output = "var icons = {\n";
1630

1731
files.forEach(file => {
18-
const name = path.basename(file, ".svg");
19-
const svgContent = fs.readFileSync(path.join(ICONS_DIR, file), "utf8");
20-
const pathData = extractPath(svgContent);
21-
22-
output += ` "${name}":[0,0,24,24,${JSON.stringify(pathData)}],\n`;
32+
try {
33+
const name = path.basename(file, ".svg");
34+
const svgContent = fs.readFileSync(path.join(ICONS_DIR, file), "utf8");
35+
const pathData = extractPath(svgContent);
36+
const viewBox = extractViewBox(svgContent);
37+
38+
output += ` "${name}":[${viewBox.join(",")},${JSON.stringify(pathData)}],\n`;
39+
} catch (err) {
40+
console.log(`❌ Failed to process ${file}: ${err.message}`);
41+
}
2342
});
2443

2544
output += "};\n\n";

0 commit comments

Comments
 (0)