Giter Site home page Giter Site logo

svgstore-cli's Introduction

svgstore

NPM version Downloads

Combines multiple SVG files into one using <symbol> elements which you may <use> in your markup. Heavily inspired by grunt-svgstore and gulp-svgstore, this is a standalone module that may be used in any asset pipeline.

Install

$ npm install --save svgstore

Usage

var svgstore = require('svgstore');
var fs = require('fs');

var sprites = svgstore()
    .add('unicorn', fs.readFileSync('./unicorn.svg', 'utf8'))
    .add('rainbow', fs.readFileSync('./rainbow.svg', 'utf8'));

fs.writeFileSync('./sprites.svg', sprites);

The resulting file may be consumed in markup as external content.

<body>
    <svg role="img"><use xlink:href="./sprites.svg#unicorn"/></svg>
    <svg role="img"><use xlink:href="./sprites.svg#rainbow"/></svg>
</body>

See the examples directory for more detail.

API

svgstore([options]): SvgStore

Creates a container SVG sprites document.

.element

The current cheerio instance.

.add(id, svg [, options]): SvgStore

  • id {String} Unique id for this SVG file.
  • svg {String} Raw source of the SVG file.
  • options {Object} Same as the options of svgstore(), but will only apply to this SVG file's <symbol>.

Appends a file to the sprite with the given id.

.toString([options]): String

  • options {Object}
    • inline {Boolean} (default: false) Don't output <?xml ?>, DOCTYPE, and the xmlns attribute.

Outputs sprite as a string of XML.

Options

  • cleanDefs {Boolean|Array} (default: false) Remove style attributes from SVG definitions, or a list of attributes to remove.
  • cleanSymbols {Boolean|Array} (default: false) Remove style attributes from SVG objects, or a list of attributes to remove.
  • svgAttrs {Boolean|Object} (default: false) A map of attributes to set on the root <svg> element. If you set an attribute's value to null, you remove that attribute. Values may be functions like jQuery.
  • symbolAttrs {Boolean|Object} (default: false) A map of attributes to set on each <symbol> element. If you set an attribute's value to null, you remove that attribute. Values may be functions like jQuery.
  • copyAttrs {Boolean|Array} (default: false) Attributes to have svgstore attempt to copy to the newly created <symbol> tag from it's source <svg> tag. The viewBox, aria-labelledby, and role attributes are always copied.
  • renameDefs {Boolean} (default: false) Rename defs content ids to make them inherit files' names so that it would help to avoid defs with same ids in the output file.

Contributing

Standards for this project, including tests, code coverage, and semantics are enforced with a build tool. Pull requests must include passing tests with 100% code coverage and no linting errors.

Test

$ npm test

Future Goals

The svgstore organization began after it was noticed that the common build task of converting an <svg> into a <symbol> tag was being implemented in a similar manner by many different projects across the JavaScript ecosystem.

The long-term goal for this project, in particular, is to provide a single standalone module that can be plugged in to any asset pipeline, thus allowing pipeline tools to focus on providing clean APIs and interfaces related to their build process integration, rather than implementing/duplicating SVG conversion logic directly.

Current build tools using svgstore:


MIT © Shannon Moeller

svgstore-cli's People

Contributors

dependabot[bot] avatar dpogue avatar jonespen avatar shannonmoeller avatar silverwind avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

svgstore-cli's Issues

identical elements with identical ID values in input defs should result in only one copy in output def

I have three separate svg files each referring to their own same and identical g='blah-common' in their respective defs, this is so that each works standalone and can be easily previewed.

When put through svgstore-cli the result is of course three copies of that g element and contents in the new defs, each with an id value of 'blah-common', which technically becomes invalid xml now (duplicate ID value!). Is there a way of doing a quick compare to see if an element with duplicated IDs are indeed identical (ie, their entire child tree) and if so, only use it once.

Or, alternatively, cripple the extra duplicated IDs (and any child duplicated ids) somehow so that although they're there, wasted, they don't cause the XML to be invalid.

Not sure what you should do if you get a duplicate ID but the content of the element is different. But that's a different problem.

Ability to remove header tags

Can we either remove them or see an --option to have them removed? They're unnecessary for inline SVGs:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">

[Feature Request] --pretty option

So that we have something like this

<svg version="1.1" xmlns="http://www.w3.org/2000/svg">
  <symbol id="icon-play-btn" viewBox="0 0 1000 1000">
    <path d="M500,10C229.4,10,10,229.4,10,500c0,270.6,219.4,490,490,490c270.6,0,490-219.4,490-490C990,229.4,770.6,10,500,10z M500,941C256.4,941,59,743.6,59,500C59,256.5,256.4,59,500,59c243.6,0,441,197.5,441,441C941,743.6,743.6,941,500,941z M377.5,745L745,504.5l-367.5-225V745z"/>
  </symbol>

  <symbol id="icon-refresh-btn" viewBox="0 0 100 100">
    <path d="M85.7 15.6l2.6 25.2c.1 1.3-.8 2.3-2.1 2.2L61 40.3c-1.3-.1-1.6-1-.7-1.9l7.5-7.5c-10.3-9.6-26.3-9.3-36.3.6-5.1 5.1-7.6 11.8-7.7 18.4L12 50c0-9.7 3.7-19.4 11.1-26.9 14.6-14.6 38.2-14.8 53.1-.7l7.5-7.5c1-.9 1.8-.6 2 .7zm-9.5 34.3c0 6.7-2.5 13.5-7.7 18.6-10 10-26.1 10.2-36.3.6l7.5-7.5c.9-.9.6-1.8-.7-1.9l-25.2-2.6c-1.3-.1-2.3.8-2.1 2.2l2.6 25.2c.1 1.3 1 1.6 1.9.7l7.5-7.5c14.9 14.2 38.5 14 53.1-.7 7.5-7.5 11.2-17.3 11.1-27l-11.7-.1z"/>
  </symbol>

  <symbol id="icon-angle-up" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100">
    <path d="M75,49.9L50.1,25L25,50L0,75h16.6l16.7-16.7l16.8-16.8l16.8,16.8L83.6,75l8.2-0.1l8.2-0.1C100,74.8,75,49.9,75,49.9z"/>
  </symbol>
</svg>

Upgrade cheerio dep to address high severity vuln in lodash.merge

Lodash.merge has a high severity vulnerability prior to 4.6.2. This project depends on cheerio which depends on Lodash. Upgrade the cheerio dependency to address the lodash vulnerability.

I have not audited this project to confirm if it uses cheerio in a way that would allow exploiting the old version of jquery, but upgrading the deps would be a best practice.

Use a command parser instead of argv directly

The current system works, but definitely doesn't scale and is messy for managing.

The main issue here is just wading through the possible modules and figuring out which one is best suited for the project.

style="fill:red" OR fill = "red"

Hi All

how about fill = "red"

i want to use use and class set SVG‘s color, but svgStore output is style="fill: red" and it can not effective.

<symbol id="baoyu" viewBox="0 0 48 48">
<rect id="XMLID_241_" x="0" style="display:none;opacity:0.16;"  fill="#E61F19" width="48" height="48"/>
<g id="XMLID_74_">
	<path id="XMLID_303_" style="fill:#26B2EF;" d="M8.749,40.262L8.749,40.262c-0.714-0.413-0.962-1.335-0.549-2.049l2-3.464
		c0.412-0.714,1.335-0.962,2.049-0.549h0c0.714,0.412,0.962,1.335,0.549,2.049l-2,3.464C10.385,40.427,9.463,40.674,8.749,40.262z"/>
	<path id="XMLID_266_" fill="#26B2EF" d="M13.747,43.726L13.747,43.726c-0.714-0.413-0.962-1.335-0.549-2.049l4-6.928
		c0.413-0.714,1.335-0.962,2.049-0.549h0c0.714,0.412,0.962,1.335,0.549,2.049l-4,6.928C15.383,43.892,14.461,44.139,13.747,43.726z
		"/>
	<path id="XMLID_261_" fill="#26B2EF" d="M22.749,40.262L22.749,40.262c-0.714-0.412-0.962-1.335-0.549-2.049l2-3.464
		c0.412-0.714,1.335-0.962,2.049-0.549l0,0c0.714,0.412,0.962,1.335,0.549,2.049l-2,3.464
		C24.385,40.427,23.463,40.674,22.749,40.262z"/>
	<path id="XMLID_310_" fill="#26B2EF" d="M28.251,42.86L28.251,42.86c-0.714-0.413-0.962-1.335-0.549-2.049l3.5-6.062
		c0.412-0.714,1.335-0.962,2.049-0.549h0c0.714,0.412,0.962,1.335,0.549,2.049l-3.5,6.062C29.888,43.025,28.966,43.273,28.251,42.86
		z"/>
	<path id="XMLID_45_" fill="#26B2EF" d="M23.5,12c3.417,0,6.489,2.033,7.826,5.178l0.767,1.805l1.961,0.021
		C36.782,19.034,39,21.275,39,24c0,2.757-2.243,5-5,5H13c-2.206,0-4-1.794-4-4c0-2.056,1.539-3.766,3.58-3.978l2.159-0.224
		l0.463-2.121C16.046,14.808,19.536,12,23.5,12 M23.5,9c-5.505,0-10.101,3.871-11.229,9.038C8.748,18.403,6,21.38,6,25
		c0,3.866,3.134,7,7,7h21c4.418,0,8-3.582,8-8c0-4.389-3.535-7.948-7.913-7.996C32.336,11.887,28.256,9,23.5,9L23.5,9z"/>
</g>
</symbol>

<svg width="50" height="50">
  <use xlink:href='#baoyu' class="test"></use>
</svg>

Feature request: preserve svg attributes in symbols

Hi,

I noticed today that attributes like aria-labelledby, role, focusable and preserveAspectRatio are all removed from the final output. This is currently supported in svgstore. Any possibility of porting those options to the cli version as well? Thanks!

First file in dir ignored

svgstore looks like a great alternative to svg-sprite (which has a Phantom dep)!

However I've noticed that on the two projects I've switched to this, the first SVG in a directory is ignored and so missing from the end result sprite.

I'm using it like this:

svgstore -o public/images/icon-sprite.svg -p "i-" --inline public/images/svg-icons/*.svg

Whatever file is first (alphabetically) in /svg-icons is excluded from the icon-sprite.svg file. My current workaround is an empty a.svg file in the directory.

svgstore creates an unusable output file

svgstore-cli version: 1.3.1
OS: Debian Linux 10.4
Browser: Firefox 68.8.0esr

After $npm install svgstore-cli --save-dev, I use the following command line:

$npx svgstore -o flags.svg images/an.svg

Please see the file bug.txt for my HTML. (It is an HTML file, but github would not accept it with a .html extension.) Nothing is rendered. On the other hand, if I replace the svg tag with <image src="images/an.svg">, then the image is rendered.

I tried this same test using images/us.svg instead of images/an.svg (and changing <use href="flags.svg#an"> to <use href="flags.svg#us">) and got the expected result.

(Because github will not allow me to attach a .svg file, I have changed the extensions on the image files to .txt.)

an.svg
us.svg
bug.html

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.