-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
29 lines (25 loc) · 797 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import svgson, { stringify } from 'svgson'
import toPath from 'element-to-path'
const elemToPath = node => {
let o = Object.assign({}, node)
if (/(rect|circle|ellipse|polygon|polyline|line|path)/.test(o.name)) {
o.attributes = Object.assign({}, o.attributes, {
d: toPath(o),
})
for (const attr in o.attributes) {
// Remove geometry properties not used
if (/^(x|y|x1|y1|x2|y2|points|width|height|cx|cy|rx|ry|r)$/.test(attr)) {
delete o.attributes[attr]
}
}
o.name = 'path'
} else if (o.children && Array.isArray(o.children)) {
o.children = o.children.map(elemToPath)
}
return o
}
export default async svg => {
const parsed = await svgson(svg)
const convertedToPath = elemToPath(parsed)
return stringify(convertedToPath)
}