svger-cli 1.0.3 → 1.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1,7 +1,3 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Generates a React functional component string from an SVG file's content,
|
|
3
|
-
* safely converting any inline `style="..."` to React-compatible object.
|
|
4
|
-
*/
|
|
5
1
|
export declare function reactTemplate({ componentName, svgContent, defaultWidth, defaultHeight, defaultFill, defaultStroke, }: {
|
|
6
2
|
componentName: string;
|
|
7
3
|
svgContent: string;
|
|
@@ -1,9 +1,5 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Generates a React functional component string from an SVG file's content,
|
|
3
|
-
* safely converting any inline `style="..."` to React-compatible object.
|
|
4
|
-
*/
|
|
5
1
|
export function reactTemplate({ componentName, svgContent, defaultWidth = 24, defaultHeight = 24, defaultFill = "currentColor", defaultStroke = "none", }) {
|
|
6
|
-
//
|
|
2
|
+
// convert style string to React object
|
|
7
3
|
function styleStringToObject(style) {
|
|
8
4
|
const obj = {};
|
|
9
5
|
style.split(";").forEach((pair) => {
|
|
@@ -12,7 +8,6 @@ export function reactTemplate({ componentName, svgContent, defaultWidth = 24, de
|
|
|
12
8
|
const [key, value] = pair.split(":");
|
|
13
9
|
if (!key || value === undefined)
|
|
14
10
|
return;
|
|
15
|
-
// convert kebab-case to camelCase
|
|
16
11
|
const camelKey = key.trim().replace(/-([a-z])/g, (_, c) => c.toUpperCase());
|
|
17
12
|
let v = value.trim();
|
|
18
13
|
if (!isNaN(Number(v)))
|
|
@@ -21,18 +16,18 @@ export function reactTemplate({ componentName, svgContent, defaultWidth = 24, de
|
|
|
21
16
|
});
|
|
22
17
|
return obj;
|
|
23
18
|
}
|
|
24
|
-
// clean SVG
|
|
19
|
+
// clean SVG
|
|
25
20
|
let cleaned = svgContent
|
|
26
|
-
.replace(/<\?xml.*?\?>/g, "")
|
|
27
|
-
.replace(/<!DOCTYPE.*?>/g, "")
|
|
28
|
-
.replace(/\r?\n|\r/g, "")
|
|
21
|
+
.replace(/<\?xml.*?\?>/g, "")
|
|
22
|
+
.replace(/<!DOCTYPE.*?>/g, "")
|
|
23
|
+
.replace(/\r?\n|\r/g, "")
|
|
29
24
|
.trim();
|
|
30
|
-
//
|
|
25
|
+
// fix style attributes
|
|
31
26
|
cleaned = cleaned.replace(/style="([^"]*)"/g, (_, styleContent) => {
|
|
32
|
-
return `{
|
|
27
|
+
return `style={${JSON.stringify(styleStringToObject(styleContent))}}`;
|
|
33
28
|
});
|
|
34
|
-
//
|
|
35
|
-
cleaned = cleaned.replace(/<svg([^>]*)>/, `<svg$1 width={props.width || ${defaultWidth}} height={props.height || ${defaultHeight}} fill={props.fill || "${defaultFill}"} stroke={props.stroke || "${defaultStroke}"} {...props}>`);
|
|
29
|
+
// fix svg tag
|
|
30
|
+
cleaned = cleaned.replace(/<svg([^>]*)>/, `<svg$1 width={props.width || ${defaultWidth}} height={props.height || ${defaultHeight}} fill={props.fill || "${defaultFill}"} stroke={props.stroke || "${defaultStroke}"} xmlns="http://www.w3.org/2000/svg" xmlnsXlink="http://www.w3.org/1999/xlink" {...props}>`);
|
|
36
31
|
return `import * as React from "react";
|
|
37
32
|
import type { SVGProps } from "react";
|
|
38
33
|
|
package/package.json
CHANGED
|
@@ -1,7 +1,3 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Generates a React functional component string from an SVG file's content,
|
|
3
|
-
* safely converting any inline `style="..."` to React-compatible object.
|
|
4
|
-
*/
|
|
5
1
|
export function reactTemplate({
|
|
6
2
|
componentName,
|
|
7
3
|
svgContent,
|
|
@@ -17,14 +13,13 @@ export function reactTemplate({
|
|
|
17
13
|
defaultFill?: string;
|
|
18
14
|
defaultStroke?: string;
|
|
19
15
|
}) {
|
|
20
|
-
//
|
|
16
|
+
// convert style string to React object
|
|
21
17
|
function styleStringToObject(style: string) {
|
|
22
18
|
const obj: Record<string, string | number> = {};
|
|
23
19
|
style.split(";").forEach((pair) => {
|
|
24
20
|
if (!pair.trim()) return;
|
|
25
21
|
const [key, value] = pair.split(":");
|
|
26
22
|
if (!key || value === undefined) return;
|
|
27
|
-
// convert kebab-case to camelCase
|
|
28
23
|
const camelKey = key.trim().replace(/-([a-z])/g, (_, c) => c.toUpperCase());
|
|
29
24
|
let v: string | number = value.trim();
|
|
30
25
|
if (!isNaN(Number(v))) v = Number(v);
|
|
@@ -33,22 +28,22 @@ export function reactTemplate({
|
|
|
33
28
|
return obj;
|
|
34
29
|
}
|
|
35
30
|
|
|
36
|
-
// clean SVG
|
|
31
|
+
// clean SVG
|
|
37
32
|
let cleaned = svgContent
|
|
38
|
-
.replace(/<\?xml.*?\?>/g, "")
|
|
39
|
-
.replace(/<!DOCTYPE.*?>/g, "")
|
|
40
|
-
.replace(/\r?\n|\r/g, "")
|
|
33
|
+
.replace(/<\?xml.*?\?>/g, "")
|
|
34
|
+
.replace(/<!DOCTYPE.*?>/g, "")
|
|
35
|
+
.replace(/\r?\n|\r/g, "")
|
|
41
36
|
.trim();
|
|
42
37
|
|
|
43
|
-
//
|
|
38
|
+
// fix style attributes
|
|
44
39
|
cleaned = cleaned.replace(/style="([^"]*)"/g, (_, styleContent) => {
|
|
45
|
-
return `{
|
|
40
|
+
return `style={${JSON.stringify(styleStringToObject(styleContent))}}`;
|
|
46
41
|
});
|
|
47
42
|
|
|
48
|
-
//
|
|
43
|
+
// fix svg tag
|
|
49
44
|
cleaned = cleaned.replace(
|
|
50
45
|
/<svg([^>]*)>/,
|
|
51
|
-
`<svg$1 width={props.width || ${defaultWidth}} height={props.height || ${defaultHeight}} fill={props.fill || "${defaultFill}"} stroke={props.stroke || "${defaultStroke}"} {...props}>`
|
|
46
|
+
`<svg$1 width={props.width || ${defaultWidth}} height={props.height || ${defaultHeight}} fill={props.fill || "${defaultFill}"} stroke={props.stroke || "${defaultStroke}"} xmlns="http://www.w3.org/2000/svg" xmlnsXlink="http://www.w3.org/1999/xlink" {...props}>`
|
|
52
47
|
);
|
|
53
48
|
|
|
54
49
|
return `import * as React from "react";
|