mirror of
https://github.com/pheralb/svgl.git
synced 2025-12-29 08:01:36 +08:00
feat: convert `styles to object in parseReactSvgContent()` utility
This commit is contained in:
@@ -6,12 +6,29 @@ interface ParseReactSvgOptions {
|
|||||||
typescript: boolean;
|
typescript: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const convertStyleStringToObject = (styleString: string): string => {
|
||||||
|
const styleObj: Record<string, string> = {};
|
||||||
|
styleString.split(";").forEach((style) => {
|
||||||
|
const [property, value] = style.split(":").map((s) => s.trim());
|
||||||
|
if (property && value) {
|
||||||
|
const camelCaseProperty = property.replace(/-([a-z])/g, (g) =>
|
||||||
|
g[1].toUpperCase(),
|
||||||
|
);
|
||||||
|
styleObj[camelCaseProperty] = value;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return JSON.stringify(styleObj);
|
||||||
|
};
|
||||||
|
|
||||||
export const parseReactSvgContent = async ({
|
export const parseReactSvgContent = async ({
|
||||||
componentName,
|
componentName,
|
||||||
svgCode,
|
svgCode,
|
||||||
typescript,
|
typescript,
|
||||||
}: ParseReactSvgOptions) => {
|
}: ParseReactSvgOptions) => {
|
||||||
const reactifiedSvg = svgCode
|
const processedSvg = svgCode.replace(/style="([^"]*)"/g, (_, styleString) => {
|
||||||
|
return `style={${convertStyleStringToObject(styleString)}}`;
|
||||||
|
});
|
||||||
|
const reactifiedSvg = processedSvg
|
||||||
.replace("<svg", "<svg {...props}")
|
.replace("<svg", "<svg {...props}")
|
||||||
.replace(/class="/g, 'className="')
|
.replace(/class="/g, 'className="')
|
||||||
.replace(/clip-rule="/g, 'clipRule="')
|
.replace(/clip-rule="/g, 'clipRule="')
|
||||||
|
|||||||
Reference in New Issue
Block a user