-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptimise.js
More file actions
executable file
·43 lines (34 loc) · 991 Bytes
/
optimise.js
File metadata and controls
executable file
·43 lines (34 loc) · 991 Bytes
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
const Svgo = require('svgo');
const deasync = require('synchronized-promise');
const makeRegex = str => new RegExp(` +id=\"(${str})\"`, 'gi');
const idRegex = makeRegex('.+');
const getIds = str => (str.match(idRegex) || []).map(s => s.replace(idRegex, '$1'));
module.exports = function optimise(name, content, opts) {
const ids = getIds(content);
const DEFAULTS = {
plugins: [
{
cleanupIDs: false,
},
],
};
const svgo = new Svgo(opts || DEFAULTS);
const optimize = content =>
new Promise((res, rej) => {
svgo.optimize(content).then(res, rej);
});
//const optimizeSync = deasync(optimize);
try {
//let { data: output } = optimizeSync(content);
let output = content;
ids.forEach(id => {
const r = makeRegex(id);
const replacement = ` id="${name}-${id}"`;
output = output.replace(r, replacement);
});
const withIds = output.replace();
return output;
} catch (e) {
throw e;
}
};