5.3.1 • Published 2 years ago

svgson v5.3.1

Weekly downloads
20,511
License
MIT
Repository
github
Last release
2 years ago

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: {},
      parent: null,
      children: [
        {
          name: 'line',
          type: 'element',
          value: '',
          attributes: {
            stroke: '#bada55',
            'stroke-width': '2',
            'stroke-linecap': 'round',
            x1: '70',
            y1: '80',
            x2: '250',
            y2: '150'
          },
          parent: null,
          children: []
        }
      ]
    }
  */

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

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.transformNode

    Function to apply on each node when stringifying, useful when need to reshape nodes or change/update values.

    Type: Function that returns the node

    Default:

    function(node){
      return node
    }
  • 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

svg-lineart-animatorvue-svg-converter@ads-medienmanufaktur/easy-sprite@infinitebrahmanuniverse/nolb-svgs@everything-registry/sub-chunk-2859varnasvgctrsvgtoavg-clisvgson-clisvgson-loadersvgpdfsvgviewboxsvg-mapssvg-path-animatorsvpugsvg-icon-centersvg2rnsvgicon-subset-webfontsvgicosvg-chameleonsvg-change-color-exportsvg-a11y-checkersvg-spreactsvg-component-convertersvg-convert-to-pathsvg-uniquevite-awesome-svg-loader@deepvision/svg-sprite-generator@cpin/cpin-icon@prisma-cms/world-modulek4us-share@ianczm/hilti-iconsrol-fla-parsersssvgto-faicon@akashic/akashic-cli-scan@aws/pdk@aws-prototyping-sdk/cdk-graph-plugin-diagram@aibee/bmap@bestminr/svg-icon-webpack-loader@c-hive/team-contribution-calendar@bryansh/svelte-weather-icons@cicara/sico@bolt/build-tools@bolt/elements-icon@deepvision/admin-kit@dicebear/avatars-webpack-loaderurbit-sigil-jsurbitme-sigil-jsxtra-team-lit-board-to-data@dicebear/avatars@grvsh02/homezy-design-system@fooddyshop/backend@hitomihiumi/lazy-canvas@iconfont-componentized/parser@glyphs/cli@granello-dev/spares-shared@infosecinnovations/project-fantastic@lucide/build-icons@lona/svg-model@muya-ui/baozheng-scripts@muraldevkit/ds-utilities@nervina-labs/dob-render@nfq/svg-to-react@neodx/svg@neodx/svg-sprite@mbcrm/icons@monicon/core@monicon/icon-loader@monicon/svelte@kodozorro/svg-to-reactnative@oktaytest/icon-loader@oktaytest/svelte@phosphor-icons/unplugin@rocketsoftware/icons@rev-ai/screen-maporange-ui-iconopenrazerpdf-exportingpath-that-svgscale-that-svgsdsvgutilspwa-icons-generatorreact-native-svg-converterreact-native-svg-editorreact-native-united-states-iconsnode-red-contrib-ui-svgnode-svg-fillplatforma-icons-librarypixelmapspix31postcss-embed-svgpptx-parserrenew-libreorder-svgprocedural-weaponsray-buildsketch-to-svg-json-loaderreact-svgsreact-scripts-svg
5.3.1

2 years ago

5.3.0

2 years ago

5.2.1

4 years ago

5.2.0

4 years ago

5.1.0

4 years ago

5.0.0

4 years ago

4.1.0

5 years ago

4.0.0

6 years ago

3.1.0

6 years ago

3.0.0

7 years ago

3.0.0-rc.0

7 years ago

2.1.1

7 years ago

2.1.0

8 years ago

2.0.4

8 years ago

2.0.3

9 years ago

2.0.2

9 years ago

2.0.1

9 years ago

2.0.0

9 years ago

1.2.2

9 years ago

1.2.1

9 years ago

1.2.0

9 years ago

1.1.0

9 years ago

1.0.5

9 years ago

1.0.4

9 years ago

1.0.3

9 years ago

1.0.2

9 years ago

1.0.1

9 years ago

1.0.0

9 years ago