diff --git a/src/components/copySvg.svelte b/src/components/copySvg.svelte index 4c14d19..f7db1fd 100644 --- a/src/components/copySvg.svelte +++ b/src/components/copySvg.svelte @@ -20,6 +20,7 @@ import { getSvelteCode } from '@/templates/getSvelteCode'; import { getAngularCode } from '@/templates/getAngularCode'; import { getWebComponent } from '@/templates/getWebComponent'; + import { getAstroCode } from '@/templates/getAstroCode'; //Icons: import ReactIcon from '@/components/icons/reactIcon.svelte'; @@ -27,6 +28,7 @@ import SvelteIcon from '@/components/icons/svelteIcon.svelte'; import AngularIcon from '@/components/icons/angularIcon.svelte'; import WebComponentIcon from '@/components/icons/webComponentIcon.svelte'; + import AstroIcon from '@/components/icons/astroIcon.svelte'; // Props: export let iconSize = 24; @@ -246,7 +248,7 @@ isLoading = false; }; - // Copy SVG as Standalone Angular component: + // Copy SVG as Web Component: const convertSvgWebComponent = async () => { isLoading = true; optionsOpen = false; @@ -278,6 +280,37 @@ isLoading = false; }; + + // Copy SVG as Astro component: + const convertSvgAstroComponent = async () => { + isLoading = true; + optionsOpen = false; + + const svgUrlToCopy = getSvgUrl(); + const content = await getSource({ + url: svgUrlToCopy + }); + + if (!content) { + toast.error('Failed to fetch the SVG content', { + duration: 5000 + }); + isLoading = false; + return; + } + + const astroComponentCode = getAstroCode({ + svgContent: content + }); + + await clipboard(astroComponentCode); + + toast.success(`Copied as Astro Component`, { + description: `${svgInfo.title} - ${svgInfo.category}` + }); + + isLoading = false; + }; (optionsOpen = isOpen)}> @@ -300,6 +333,9 @@
+ + + @@ -312,8 +348,8 @@ - - + +
@@ -422,6 +458,19 @@ + +
+ +
+
+ import type { IconProps } from '@/types/icon'; + + export let iconSize: IconProps['size']; + + + diff --git a/src/templates/getAstroCode.ts b/src/templates/getAstroCode.ts new file mode 100644 index 0000000..7b4c797 --- /dev/null +++ b/src/templates/getAstroCode.ts @@ -0,0 +1,16 @@ +interface AstroComponentParams { + svgContent: string; +} + +export function getAstroCode({ svgContent }: AstroComponentParams): string { + const cleanedSvg = svgContent + .replace(/\s*(width|height)="[^"]*"/gi, '') + .replace(/\s*(width|height)='[^']*'/gi, '') + .replace(/\s*(width|height)=\{[^}]*\}/gi, '') + .replace(/]*)>/i, (match, attrs) => { + const cleanedAttrs = attrs.replace(/\s*\{?\.\.\.Astro\.props\}?\s*/i, ''); + return ``; + }); + + return cleanedSvg.trim(); +}