-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.mjs
More file actions
50 lines (41 loc) · 904 Bytes
/
index.mjs
File metadata and controls
50 lines (41 loc) · 904 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
44
45
46
47
48
49
50
/**
* @typedef {import("svgo").CustomPlugin} SVGOPlugin
* @typedef {SVGOPlugin['fn']} SVGOPluginFunction
* @typedef {SVGOPlugin['name']} SVGOPluginName
* @typedef {import("svgo/lib/types").XastElement} SVGOElement
*/
/**
* @type {SVGOPluginName}
*/
const name = 'SVGOAddViewBox';
/**
* @type {SVGOPluginFunction}
*/
const fn = (_, params) => {
const { overwrite = true } = params;
return {
root: {
enter: node => {
const element = /** @type {SVGOElement} */ (node?.children?.[0]);
if (!element) {
return;
}
const { width, height, viewBox } = element.attributes;
if (viewBox && !overwrite) {
return;
}
if (typeof width === 'undefined' || typeof height === 'undefined') {
return;
}
element.attributes.viewBox = `0 0 ${Number(width)} ${Number(height)}`;
}
}
};
};
/**
* @type {SVGOPlugin}
*/
export default {
fn,
name
};