• Stars
    star
    412
  • Rank 105,024 (Top 3 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created almost 9 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Transform svg files to json notation

svgson

Simple tool to transform svg files and Strings into Object or JSON.

Useful to manipulate SVG with js, to store in noSQL databses.


Travis Codecov Version Download MIT License

v2 docs

Install

yarn add svgson

Usage

const { parse, stringify } = require('svgson')

// ----------------------------
// Convert SVG to JSON AST
// ----------------------------
parse(`
  <svg>
    <line
      stroke= "#bada55"
      stroke-width= "2"
      stroke-linecap= "round"
      x1= "70"
      y1= "80"
      x2= "250"
      y2= "150">
    </line>
  </svg>`).then((json) => {
  console.log(JSON.stringify(json, null, 2))
  /*
    {
      name: 'svg',
      type: 'element',
      value: '',
      attributes: {},
      children: [
        {
          name: 'line',
          type: 'element',
          value: '',
          attributes: {
            stroke: '#bada55',
            'stroke-width': '2',
            'stroke-linecap': 'round',
            x1: '70',
            y1: '80',
            x2: '250',
            y2: '150'
          },
          children: []
        }
      ]
    }
  */

  // -------------------------------
  // Convert JSON AST back to SVG
  // -------------------------------
  const mysvg = stringify(json)
  /* returns the SVG as string */
})

umd usage

const svgson = require('svgson')
svgson
  .parse(
    `<svg>
      <line
        stroke= "#bada55"
        stroke-width= "2"
        stroke-linecap= "round"
        x1= "70"
        y1= "80"
        x2= "250"
        y2= "150">
      </line>
    </svg>`
  )
  .then(function(json) {
    console.log(JSON.stringify(json, null, 2)
  )
 
mysvg = svgson.stringify(json)
  

Test in browser here

API

svgson.parse

svgson.parse(input[, options])

Returns: Promise

  • input

    Type: String

  • options.transformNode

    Function to apply on each node when parsing, useful when need to reshape nodes or set default attributes.

    Type: Function that returns the node

    Default:

    function(node){
      return node
    }
  • options.camelcase

    Apply camelCase into attributes

    Type: Boolean

    Default: false

svgson.parseSync

Added in 3.1.0

svgson.parseSync(input[, options])

This function is a synchronous version of svgson.parse. The arguments are the same, but unlike svgson.parse, the return value is not wrapped in a Promise.

Returns: Object [Object]

svgson.stringify

svg = svgson.stringify(ast[, options])

Returns: String

  • ast

    svgson parsed result.

    Type: Object [Object]

  • options.transformAttr

    Function to apply on each attribute when stringifying.

    Type: Function that returns the key/attribute string with the ability to use the escape function on it.

    Default:

    function(key, value, escape) {
      return `${key}="${escape(value)}"`
    }
  • options.selfClose

    Type: Boolean

    Default: true

  • Pretty Printing

    In order to generate pretty formatted SVG output, use pretty npm module:

    pretty = require('pretty')
    formatted = pretty(svg)

Related

svgson-cli Transform SVG into Object from the Command Line

element-to-path Convert SVG element into path

path-that-svg Convert entire SVG with path

svg-path-tools Tools to manipulate SVG path (d)

License

MIT © Lionel T

More Repositories

1

csshake

CSS classes to move your DOM!
SCSS
4,814
star
2

reactour

Tourist Guide into your React Components
TypeScript
3,787
star
3

reshake

CSShake as a React Functional Component
TypeScript
267
star
4

svgsprit.es

Public endpoint to generate SVG Sprites using svg-spreact
JavaScript
153
star
5

draGGradients

A simple tool to generate and customize multiple css3 radial gradients.
JavaScript
138
star
6

outline-stroke

Convert stroked SVG into the outlined version
JavaScript
121
star
7

coloreact

A tiny Color Picker for React
JavaScript
88
star
8

Pixelator

A really simple tool to generate one element css box-shadow pixel style.
JavaScript
61
star
9

path-that-svg

Path that SVG!
JavaScript
56
star
10

element-to-path

Transform SVG elements into path
JavaScript
46
star
11

outline-stroke-cli

Convert stroked SVG into the outlined version
JavaScript
32
star
12

svg-path-tools

Tools to manipulate svg path (d)
JavaScript
26
star
13

Sassisfaction

Awesome Sass Resources
JavaScript
21
star
14

scale-that-svg

Scale that SVG!
JavaScript
19
star
15

svg-spreact-cli

CLI version of svg-spreact
JavaScript
19
star
16

svg-spreact

Create svg sprites using svgson and react
JavaScript
16
star
17

svgson-next

Deprecated: use [email protected] instead
JavaScript
15
star
18

svgson-cli

CLI version of svgson
JavaScript
14
star
19

tablexcel

Convert Excel tables in HTML code
11
star
20

micro-outline-stroke

UI and endpoint for svg-outline-stroke
JavaScript
10
star
21

newbancomail

Useful style package to use in projects.
CSS
10
star
22

elrumordelaluz

My website
JavaScript
6
star
23

my-css-guidelines

My personal CSS coding guidelines
5
star
24

iterm-tab

Open Item2 tab programatically from NodeJS
JavaScript
4
star
25

ministarter

Starter configuration for fast static web projects
JavaScript
3
star
26

telegram-translate-bot

A Telegram Bot to Translate into Italian
JavaScript
3
star
27

next-with-apollo-custom-app

JavaScript
2
star
28

responsimage

JavaScript
2
star
29

expresstart

Simple Express server starting files
JavaScript
1
star
30

react-braintree-custom-dropin

JavaScript
1
star
31

css-props

W3C List of CSS properties, both proposed and standard
JavaScript
1
star
32

tryExec

Exec an npm script trying several times in case a condition doesn't meets
JavaScript
1
star
33

fsvg

fsvg 💤
JavaScript
1
star
34

vercel-test-sharp-svg-fonts

JavaScript
1
star