Last active
April 18, 2020 19:12
-
-
Save MoOx/d5bc9ebef1a463cf4348 to your computer and use it in GitHub Desktop.
Cleanup svg
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cleanupSvg(svg) { | |
return [ | |
// some useless stuff for us | |
// that svgo doesn't remove | |
/<title>.*<\/title>/gi, | |
// remove hardcoded dimensions | |
/ +width="\d+(\.\d+)?(px)?"/gi, | |
/ +height="\d+(\.\d+)?(px)?"/gi, | |
// remove fill | |
/ +fill=\"(none|#[0-9a-f]+)\"/gi, | |
// Sketch.app shit | |
/ +sketch:type=\"MSShapeGroup\"/gi, | |
/ +sketch:type=\"MSPage\"/gi, | |
/ +sketch:type=\"MSLayerGroup\"/gi, | |
] | |
.reduce((acc, re) => { | |
return acc.replace(re, "") | |
}, svg) | |
.trim() | |
} |
@kud we've also got svgo loader which handles most of it
@bloodyowl answered on gitter.
SVGO is much better for this job
@kud You fill replacement is buggy and might remove some tags
@ai svgo doesn't remove those values since it's really opinonated from our usage (fill/width/height from css only)
@kud I've added some of your removal
Like I said to Kud, "xmlns:sketch" isn't mandatory, you can remove it too ;)
<title>
is important for accessibility though
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you need mine: