Package Exports
- svgson
- svgson/dist/svgson.cjs.js
- svgson/dist/svgson.esm.js
- svgson/dist/svgson.umd.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (svgson) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
Simple tool to transform svg files and Strings into Object or JSON.
Useful to manipulate SVG with js, to store in noSQL databses.
Install
yarn add svgsonUsage
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 */
})
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
inputType:
Stringoptions.transformNodeFunction to apply on each node when parsing, useful when need to reshape nodes or set default attributes.
Type:
Functionthat returns the nodeDefault:
function(node){ return node }
options.camelcaseApply
camelCaseinto attributesType:
BooleanDefault:
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
astsvgsonparsed result.Type:
Object[Object]options.transformAttrFunction to apply on each attribute when stringifying.
Type:
Functionthat 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:
Functionthat returns the nodeDefault:
function(node){ return node }
options.selfCloseType:
BooleanDefault:
truePretty Printing
In order to generate pretty formatted SVG output, use
prettynpm 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