MCPcopy Index your code
hub / github.com/elrumordelaluz/svgson

github.com/elrumordelaluz/svgson @v5.3.0

Chat with this repo
repository ↗ · DeepWiki ↗ · release v5.3.0 ↗ · + Follow
19 symbols 37 edges 8 files 0 documented · 0% 7 cross-repo links updated 2y agov4.0.0 · 2019-07-09★ 4731 open issues
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

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

  <pre lang=js>

const svgson = require('svgson') svgson .parse( &lt;svg&gt; &lt;line stroke= "#bada55" stroke-width= "2" stroke-linecap= "round" x1= "70" y1= "80" x2= "250" y2= "150"&gt; &lt;/line&gt; &lt;/svg&gt; ) .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:

js 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:

js 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:

js 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

Extension points exported contracts — how you extend this code

INode (Interface)
(no doc)
types.d.ts
IParseOptions (Interface)
(no doc)
types.d.ts
IStringifyOptions (Interface)
(no doc)
types.d.ts

Core symbols most depended-on inside this repo

svgson
called by 16
src/svgson.js
stringify
called by 5
src/stringify.js
deepTransform
called by 4
test.js
optimizeSVG
called by 2
test.js
unescapeAttr
called by 1
test.js
svgsonSync
called by 1
src/svgson.js
applyFilters
called by 1
src/svgson.js
parseInput
called by 1
src/tools.js

Shape

Function 16
Interface 3

Languages

TypeScript100%

Modules by API surface

src/tools.js9 symbols
types.d.ts3 symbols
test.js3 symbols
src/svgson.js3 symbols
src/stringify.js1 symbols

For agents

$ claude mcp add svgson \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact

Ask about this repo answers extend the page