Skip to content

Commit

Permalink
Don't add symbols to <defs> (version bump), update readme, and code c…
Browse files Browse the repository at this point in the history
…omments
  • Loading branch information
kevinkace committed Mar 1, 2017
1 parent 41afffa commit fa40922
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 42 deletions.
21 changes: 11 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# svg-spritzer

*Note:* I made this before finding out about [svgstore](https://github.com/svgstore/svgstore), (this is why you put a delimiter between identifyers!).
It has more features, support, and likely a better future.

![](https://media.giphy.com/media/3orif3jbFCSDlr9G3m/giphy.gif)

Sprite SVGs into a single SVG like this:
Expand All @@ -8,16 +11,14 @@ Sprite SVGs into a single SVG like this:
<svg style="position: absolute; width: 0; height: 0;"
width="0" height="0" version="1.1" xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink">
<defs>
<symbol id="icon1" viewBox="0 0 32 32">
<title>icon1</title>
<path d="M18 15l7 7h-5v8h-4v-8h-5l7-7z"></path>
</symbol>
<symbol id="icon2" viewBox="0 0 32 32">
<title>icon2</title>
<path d="M18 10l4 4-8 8-4-4zM31.298 12z"></path>
</symbol>
</defs>
<symbol id="icon1" viewBox="0 0 32 32">
<title>icon1</title>
<path d="M18 15l7 7h-5v8h-4v-8h-5l7-7z"></path>
</symbol>
<symbol id="icon2" viewBox="0 0 32 32">
<title>icon2</title>
<path d="M18 10l4 4-8 8-4-4zM31.298 12z"></path>
</symbol>
</svg>
```

Expand Down
2 changes: 0 additions & 2 deletions debug.log

This file was deleted.

55 changes: 30 additions & 25 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,32 @@
"use strict";

const path = require("path"),
const path = require("path"),

fsp = require("fs-promise"),
globule = require("globule"),
parser = require("svg-parser"),

eol = require("os").EOL,
eol = "\n",
doubleLine = new RegExp(eol + eol, "g");


// input: array of SVG objects (`[{ name : "svg", attributes : {}, children : [] }, {}])
// output: the sprited SVG object (same format as the input), all input SVGs within a `<symbol>`
function sprite(svgObjs) {
const svg = {
name : "svg",
children : [],
attributes : {
style : "position: absolute; width: 0; height: 0;",
width : 0,
height : 0,
version : "1.1",
xmlns : "http://www.w3.org/2000/svg",
"xmlns:xlink" : "http://www.w3.org/1999/xlink"
}
},
defs = {
name : "defs",
children : []
};

// create a symbol for each icon, and push to defs.children
name : "svg",
children : [],
attributes : {
style : "position: absolute; width: 0; height: 0;",
width : 0,
height : 0,
version : "1.1",
xmlns : "http://www.w3.org/2000/svg",
"xmlns:xlink" : "http://www.w3.org/1999/xlink"
}
};

// create a symbol for each icon, and push to svg.children
svgObjs.forEach((svgObj) => {
let children = [{
name : "title",
Expand All @@ -44,15 +43,16 @@ function sprite(svgObjs) {
}
};

defs.children.push(symbol);
// add symbol to svg
svg.children.push(symbol);
});

// add defs to svg
svg.children.push(defs);

return svg;
}


// input: tag object
// output: SVG string of tag object, is called recursively on children
function render(el) {
let rendered = "",
renderedChildren = "";
Expand All @@ -65,8 +65,7 @@ function render(el) {
rendered += `<${el.name}`;

// attrs
// eslint-disable-next-line keyword-spacing
for(let attribute in (el.attributes || {})) {
for(let attribute in (el.attributes || {})) { // eslint-disable-line keyword-spacing
rendered += ` ${attribute}="${el.attributes[attribute]}"`;
}

Expand All @@ -90,6 +89,9 @@ function render(el) {
return rendered.replace(doubleLine, eol);
}


// input: opts specifying file path, svg the SVG string
// output: promise of writing the file
function saveFile(opts, svg) {
if(!opts || !opts.output) {
return svg;
Expand All @@ -98,6 +100,9 @@ function saveFile(opts, svg) {
return fsp.writeFile(opts.output, svg);
}


// input: glob where to find SVGs to sprite, opts for where to write file
// output: promise of processing SVGs
module.exports = function(glob, opts) {
return Promise.all(
// read and parse all files found with glob
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "svg-spritzer",
"version": "0.1.0",
"version": "0.2.0",
"description": "SVG spriting the way I want it.",
"main": "index.js",
"scripts": {
Expand Down
2 changes: 0 additions & 2 deletions test/fixtures/output.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 0 additions & 2 deletions test/fixtures/output/expected.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit fa40922

Please sign in to comment.