Skip to content

Instantly share code, notes, and snippets.

@MoOx
Last active April 18, 2020 19:12
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MoOx/d5bc9ebef1a463cf4348 to your computer and use it in GitHub Desktop.
Save MoOx/d5bc9ebef1a463cf4348 to your computer and use it in GitHub Desktop.
Cleanup svg
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
Copy link

kud commented May 12, 2015

If you need mine:

var fileString = file.contents.toString()

      _.each([
        /<title>.*<\/title>/gi,
        /<desc>.*<\/desc>/gi,
        /<!--.*-->/gi,
        /<defs>.*<\/defs>/gi,
        / +sketch:type=\"MSShapeGroup\"/gi,
        / +sketch:type=\"MSPage\"/gi,
        / +sketch:type=\"MSLayerGroup\"/gi,
        / +fill=\".*\"/gi
      ], function( regex ) {
        fileString = fileString.replace(regex, '')
      })

@bloodyowl
Copy link

@kud we've also got svgo loader which handles most of it

@kud
Copy link

kud commented May 12, 2015

@bloodyowl answered on gitter.

@ai
Copy link

ai commented May 12, 2015

SVGO is much better for this job

@MoOx
Copy link
Author

MoOx commented May 12, 2015

@kud You fill replacement is buggy and might remove some tags

@MoOx
Copy link
Author

MoOx commented May 12, 2015

@ai svgo doesn't remove those values since it's really opinonated from our usage (fill/width/height from css only)

@MoOx
Copy link
Author

MoOx commented May 12, 2015

@kud I've added some of your removal

@AntoinePlu
Copy link

Like I said to Kud, "xmlns:sketch" isn't mandatory, you can remove it too ;)

@kewah
Copy link

kewah commented May 12, 2015

<title> is important for accessibility though

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment