Skip to content

Instantly share code, notes, and snippets.

@iest
Created September 6, 2016 16:15
Show Gist options
  • Save iest/6f18108257c36e0303a62fb78a3ec8b9 to your computer and use it in GitHub Desktop.
Save iest/6f18108257c36e0303a62fb78a3ec8b9 to your computer and use it in GitHub Desktop.
Couple utility functions to cleanup SVGs (especially from Sketch output)
// 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);
}
@iest
Copy link
Author

iest commented Sep 6, 2016

Example usage: clean(stringOfSVG)

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