svger-cli 1.0.0 → 1.0.2

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,31 +1,3 @@
1
- /**
2
- * Generates a React functional component string from an SVG file's content.
3
- *
4
- * This template replaces XML/DOCTYPE declarations, cleans up formatting,
5
- * and injects React props (`width`, `height`, `fill`, and any others via `...props`)
6
- * directly into the root `<svg>` tag.
7
- *
8
- * @param {Object} params - Template generation parameters.
9
- * @param {string} params.componentName - The name of the generated React component.
10
- * @param {string} params.svgContent - The raw SVG markup to transform into a React component.
11
- * @param {number} [params.defaultWidth=24] - Default width of the SVG (used if none is provided via props).
12
- * @param {number} [params.defaultHeight=24] - Default height of the SVG (used if none is provided via props).
13
- * @param {string} [params.defaultFill="currentColor"] - Default fill color of the SVG.
14
- *
15
- * @returns {string} The complete TypeScript React component code as a string.
16
- *
17
- * @example
18
- * const svg = '<svg viewBox="0 0 24 24"><path d="M0 0h24v24H0z"/></svg>';
19
- * const componentCode = reactTemplate({
20
- * componentName: "MyIcon",
21
- * svgContent: svg,
22
- * defaultWidth: 32,
23
- * defaultHeight: 32,
24
- * });
25
- *
26
- * // Result: a ready-to-write .tsx file containing a typed React component
27
- * console.log(componentCode);
28
- */
29
1
  export declare function reactTemplate({ componentName, svgContent, defaultWidth, defaultHeight, defaultFill, }: {
30
2
  componentName: string;
31
3
  svgContent: string;
@@ -1,46 +1,30 @@
1
- /**
2
- * Generates a React functional component string from an SVG file's content.
3
- *
4
- * This template replaces XML/DOCTYPE declarations, cleans up formatting,
5
- * and injects React props (`width`, `height`, `fill`, and any others via `...props`)
6
- * directly into the root `<svg>` tag.
7
- *
8
- * @param {Object} params - Template generation parameters.
9
- * @param {string} params.componentName - The name of the generated React component.
10
- * @param {string} params.svgContent - The raw SVG markup to transform into a React component.
11
- * @param {number} [params.defaultWidth=24] - Default width of the SVG (used if none is provided via props).
12
- * @param {number} [params.defaultHeight=24] - Default height of the SVG (used if none is provided via props).
13
- * @param {string} [params.defaultFill="currentColor"] - Default fill color of the SVG.
14
- *
15
- * @returns {string} The complete TypeScript React component code as a string.
16
- *
17
- * @example
18
- * const svg = '<svg viewBox="0 0 24 24"><path d="M0 0h24v24H0z"/></svg>';
19
- * const componentCode = reactTemplate({
20
- * componentName: "MyIcon",
21
- * svgContent: svg,
22
- * defaultWidth: 32,
23
- * defaultHeight: 32,
24
- * });
25
- *
26
- * // Result: a ready-to-write .tsx file containing a typed React component
27
- * console.log(componentCode);
28
- */
29
1
  export function reactTemplate({ componentName, svgContent, defaultWidth = 24, defaultHeight = 24, defaultFill = "currentColor", }) {
30
- const cleaned = svgContent
31
- .replace(/<\?xml.*?\?>/g, "") // Remove XML declarations
32
- .replace(/<!DOCTYPE.*?>/g, "") // Remove DOCTYPE lines
33
- .replace(/\r?\n|\r/g, "") // Remove newlines
2
+ // پاکسازی XML/DOCTYPE و style inline و class/id
3
+ let cleaned = svgContent
4
+ .replace(/<\?xml.*?\?>/g, "")
5
+ .replace(/<!DOCTYPE.*?>/g, "")
6
+ .replace(/\r?\n|\r/g, "")
7
+ .replace(/\s*style="[^"]*"/g, "") // حذف style inline
8
+ .replace(/\s*(class|id)=["'][^"']*["']/g, "") // حذف class/id
34
9
  .trim();
10
+ // تبدیل attribute svg به props-react compatible
11
+ cleaned = cleaned.replace(/<svg([^>]*)>/, `<svg$1
12
+ width={width}
13
+ height={height}
14
+ fill={fill}
15
+ stroke={props.stroke || "none"}
16
+ className={props.className}
17
+ {...props}>`);
35
18
  return `import * as React from "react";
19
+ import type { SVGProps } from "react";
36
20
 
37
- export const ${componentName}: React.FC<React.SVGProps<SVGSVGElement>> = ({
21
+ export const ${componentName}: React.FC<SVGProps<SVGSVGElement>> = ({
38
22
  width = ${defaultWidth},
39
23
  height = ${defaultHeight},
40
24
  fill = "${defaultFill}",
41
25
  ...props
42
26
  }) => (
43
- ${cleaned.replace(/<svg/, `<svg width={width} height={height} fill={fill} {...props}`)}
27
+ ${cleaned}
44
28
  );
45
29
 
46
30
  export default ${componentName};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "svger-cli",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "description": "CLI and runtime for converting SVGs to React components with watch support",
5
5
  "main": "dist/cli.js",
6
6
  "type": "module",
@@ -1,31 +1,3 @@
1
- /**
2
- * Generates a React functional component string from an SVG file's content.
3
- *
4
- * This template replaces XML/DOCTYPE declarations, cleans up formatting,
5
- * and injects React props (`width`, `height`, `fill`, and any others via `...props`)
6
- * directly into the root `<svg>` tag.
7
- *
8
- * @param {Object} params - Template generation parameters.
9
- * @param {string} params.componentName - The name of the generated React component.
10
- * @param {string} params.svgContent - The raw SVG markup to transform into a React component.
11
- * @param {number} [params.defaultWidth=24] - Default width of the SVG (used if none is provided via props).
12
- * @param {number} [params.defaultHeight=24] - Default height of the SVG (used if none is provided via props).
13
- * @param {string} [params.defaultFill="currentColor"] - Default fill color of the SVG.
14
- *
15
- * @returns {string} The complete TypeScript React component code as a string.
16
- *
17
- * @example
18
- * const svg = '<svg viewBox="0 0 24 24"><path d="M0 0h24v24H0z"/></svg>';
19
- * const componentCode = reactTemplate({
20
- * componentName: "MyIcon",
21
- * svgContent: svg,
22
- * defaultWidth: 32,
23
- * defaultHeight: 32,
24
- * });
25
- *
26
- * // Result: a ready-to-write .tsx file containing a typed React component
27
- * console.log(componentCode);
28
- */
29
1
  export function reactTemplate({
30
2
  componentName,
31
3
  svgContent,
@@ -39,24 +11,37 @@ export function reactTemplate({
39
11
  defaultHeight?: number;
40
12
  defaultFill?: string;
41
13
  }) {
42
- const cleaned = svgContent
43
- .replace(/<\?xml.*?\?>/g, "") // Remove XML declarations
44
- .replace(/<!DOCTYPE.*?>/g, "") // Remove DOCTYPE lines
45
- .replace(/\r?\n|\r/g, "") // Remove newlines
14
+ // پاکسازی XML/DOCTYPE و style inline و class/id
15
+ let cleaned = svgContent
16
+ .replace(/<\?xml.*?\?>/g, "")
17
+ .replace(/<!DOCTYPE.*?>/g, "")
18
+ .replace(/\r?\n|\r/g, "")
19
+ .replace(/\s*style="[^"]*"/g, "") // حذف style inline
20
+ .replace(/\s*(class|id)=["'][^"']*["']/g, "") // حذف class/id
46
21
  .trim();
47
22
 
23
+ // تبدیل attribute svg به props-react compatible
24
+ cleaned = cleaned.replace(
25
+ /<svg([^>]*)>/,
26
+ `<svg$1
27
+ width={width}
28
+ height={height}
29
+ fill={fill}
30
+ stroke={props.stroke || "none"}
31
+ className={props.className}
32
+ {...props}>`
33
+ );
34
+
48
35
  return `import * as React from "react";
36
+ import type { SVGProps } from "react";
49
37
 
50
- export const ${componentName}: React.FC<React.SVGProps<SVGSVGElement>> = ({
38
+ export const ${componentName}: React.FC<SVGProps<SVGSVGElement>> = ({
51
39
  width = ${defaultWidth},
52
40
  height = ${defaultHeight},
53
41
  fill = "${defaultFill}",
54
42
  ...props
55
43
  }) => (
56
- ${cleaned.replace(
57
- /<svg/,
58
- `<svg width={width} height={height} fill={fill} {...props}`
59
- )}
44
+ ${cleaned}
60
45
  );
61
46
 
62
47
  export default ${componentName};
package/.svg-lock DELETED
@@ -1 +0,0 @@
1
- []