Visit the original Scour - an SVG scrubber, http://codedread.com/scour/
Scour was created by Jeff Schiller.
Please note that this is a partial port, which means it is not finsihed at all. For thoose who want to clean their SVG files and have them as clean as possible as I highly recommend to use the original Scour.py. (Please see the list of implemented and missing processing steps below.)
Recommended! Be sure to check out SVGO, a Nodejs-based tool for optimizing SVG vector graphics files.
As Command Line Tool
npm install svg-cleaner -gsvg-cleaner INPUT_FILE OUTPUT_FILE
As module - simple interface:
var cleanedSvgString = require'svg-cleaner'cleansvgString;
As module - chainable interface
var SVGCleaner = require'svg-cleaner';var mySVGCleaner = SVGCleanercreateCleaner;mySVGCleanerloadsvgStringshortenIDsremoveCommentssvgString;var svgStringWithShortIDAndWithoutComments = mySVGCleanersvgString;
I needed a library to work with SVG-Stacker, that could rename IDs and keep the references inside the SVG document structure intact. SVG-Stacker merges different svg files and needs to make sure that the ids from different files are unique in the merged version. I found that Scour implemented that feature.
I tried to keep the ideas and way how Scour cleans SVG files. I translated the processing steps and copied most of the original comments from scour into the new source code, as they describe the original ideas best. I marked all of these orginial comments by putting 'Scour:' in the first line and used the markdown syntax for quotes (>) to indent the original comment.
Tests are based on mocha, run
Visual tests are based on phantomjs. The idea is to render and rasterize
both, the original SVG file and cleaned version of that SVG file to compare the rasterized results
to ensure they are visualy identical.
Make sure you have installed phantomjs, then run
SVGCleanercreateCleaner;// creates SVG Cleaner instanceSVGCleanerloadsvgString;// loads an SVG StringSVGCleanerreadFileSyncsrcFilename;// loads an SVG fileSVGCleanerclean;// performs all processing stepsSVGCleanersvgString;// returns SVG as StringSVGCleanerwriteFileSynctargetFilename;// writes SVG to file
It makes sense to use clean(), as processing steps need to be performend in a specific order. To make use of single processing steps, you can call these steps directly. See description below:
Removal of namespaced elements
namespacesToRemove: array of namespace prefixes, e.g.: ['dc', 'rdf', 'sodipodi', 'cc', 'inkscape']
Removal of namespaced attributes
namespacesToRemove: array of namespace prefixes
Removal of comments
Repairation of styles
Removal of unreferenced elements
Removal of empty elements
Shortening of id attribute values
startNumber: default: 1, optional
Shortens the IDs, spreadsheet-style, i.e. from a to z, then from aa to az, ba to bz, etc., until zz.
Removal of unreferenced id attributes
Processing steps in scour, that are not implemented yet:
SVG-Cleaner is released under the same license as Scour: