@@ -62,8 +62,8 @@ const svgo = new SVGO({
62
62
}, { moveElemsAttrsToGroup: false,
63
63
}, { moveGroupAttrsToElems: true,
64
64
}, { collapseGroups: true,
65
- }, { removeRasterImages: false ,
66
- }, { mergePaths: false ,
65
+ }, { removeRasterImages: true ,
66
+ }, { mergePaths: true ,
67
67
}, { convertShapeToPath: true,
68
68
}, { sortAttrs: true,
69
69
}, { transformsWithOnePath: true,
@@ -90,6 +90,10 @@ function collectAttrs(node, attrs) {
90
90
return collectAttrs(node.parentNode, attrs)
91
91
}
92
92
93
+ var uniqueId = function() {
94
+ return Math.random().toString(36).substr(2, 16);
95
+ };
96
+
93
97
async function convertSvgToSketch(svg, seed, id, hideFilledPathes) {
94
98
if (svg[0] != "<") {
95
99
return ""
@@ -105,6 +109,8 @@ async function convertSvgToSketch(svg, seed, id, hideFilledPathes) {
105
109
// roughjs assumes pt, rest of the world px :-O
106
110
const width = parseInt(.75 * select("/x:svg/@width", doc)[0].value)
107
111
const height = parseInt(.75 * select("/x:svg/@height", doc)[0].value)
112
+ //const width = parseInt(1 * select("/x:svg/@width", doc)[0].value)
113
+ //const height = parseInt(1 * select("/x:svg/@height", doc)[0].value)
108
114
109
115
// init canvas
110
116
const canvas = createCanvas(width, height, "svg")
@@ -158,10 +164,30 @@ async function convertSvgToSketch(svg, seed, id, hideFilledPathes) {
158
164
const markers = s.serializeToString(select("//x:g[@id='lines']", doc)[0])
159
165
let result = canvas.toBuffer().toString()
160
166
result = result.replace("</svg>", defs + fontStyle + markers + "</svg>")
167
+
161
168
const result_doc = new dom().parseFromString((await svgo.optimize(result)).data, "svg+xml");
162
169
_.each(select("//x:svg/x:g", result_doc), (elem) => {
163
170
elem.setAttribute("transform", "scale(0.75)")
164
171
})
172
+
173
+ const suffix = "-" + uniqueId()
174
+ _.each(select("//x:svg/x:defs/x:marker", result_doc), (elem) => {
175
+ console.log(elem.getAttribute("id"))
176
+ elem.setAttribute("id", elem.getAttribute("id") + suffix)
177
+ })
178
+ _.each(select("//x:svg/x:g[@id='lines']", result_doc), (lines_elem) => {
179
+ lines_elem.setAttribute("id", "lines" + suffix)
180
+ //console.log(lines_elem)
181
+ _.each(select("//x:g/x:path", lines_elem), (line_elem) => {
182
+ if (line_elem.hasAttribute("marker-start")) {
183
+ line_elem.setAttribute("marker-start", line_elem.getAttribute("marker-start").slice(0, -1) + suffix + ")")
184
+ }
185
+ if (line_elem.hasAttribute("marker-end")) {
186
+ line_elem.setAttribute("marker-end", line_elem.getAttribute("marker-end").slice(0, -1) + suffix + ")")
187
+ }
188
+ })
189
+ })
190
+
165
191
if (hideFilledPathes) {
166
192
_.each(select("//x:path/@fill", result_doc), (attr) => {
167
193
if (attr.value && attr.value != 'none') {
0 commit comments