Created
September 6, 2016 16:15
-
-
Save iest/6f18108257c36e0303a62fb78a3ec8b9 to your computer and use it in GitHub Desktop.
Couple utility functions to cleanup SVGs (especially from Sketch output)
This file contains 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
// https://gist.github.com/MoOx/1eb30eac43b2114de73a | |
const generic = { | |
title: /<title>.*<\/title>/gi, | |
desc: /<desc>.*<\/desc>/gi, | |
comment: /<!--.*-->/gi, | |
defs: /<defs>.*<\/defs>/gi, | |
width: / +width="\d+(\.\d+)?(px)?"/gi, | |
height: / +height="\d+(\.\d+)?(px)?"/gi, | |
sketchMSShapeGroup: / +sketch:type=\"MSShapeGroup\"/gi, | |
sketchMSPage: / +sketch:type=\"MSPage\"/gi, | |
sketchMSLayerGroup: / +sketch:type=\"MSLayerGroup\"/gi, | |
}; | |
const fillStroke = { | |
fill: / +fill=\"(none|#[0-9a-f]+)\"/gi, | |
stroke: / +stroke=\"(none|#[0-9a-f]+)\"/gi, | |
}; | |
function clean(cleanups, svg) { | |
return Object.keys(cleanups) | |
.reduce((acc, key) => { | |
return acc.replace(cleanups[key], ''); | |
}, svg) | |
.trim(); | |
} | |
export function cleanAll(svg) { | |
return clean({ | |
...generic, | |
...fillStroke, | |
}, svg); | |
} | |
export function keepFillStroke(svg) { | |
return clean(generic, svg); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example usage:
clean(stringOfSVG)