aboutsummaryrefslogtreecommitdiff
path: root/apps/website/src/components/logos
diff options
context:
space:
mode:
Diffstat (limited to 'apps/website/src/components/logos')
-rw-r--r--apps/website/src/components/logos/Logo.astro52
1 files changed, 52 insertions, 0 deletions
diff --git a/apps/website/src/components/logos/Logo.astro b/apps/website/src/components/logos/Logo.astro
new file mode 100644
index 0000000..0532396
--- /dev/null
+++ b/apps/website/src/components/logos/Logo.astro
@@ -0,0 +1,52 @@
+---
+import type { LogoType } from "@webtypes/Config";
+import { dirname } from "path";
+import { fileURLToPath } from "url";
+import type { HTMLAttributes } from "astro/types";
+
+export interface Props extends HTMLAttributes<"svg"> {
+ logo: LogoType,
+ size?: number | [number, number],
+ silent?: boolean,
+}
+
+const {
+ logo,
+ silent = false,
+ size = undefined,
+ ...attr
+} = Astro.props;
+let svg: string | undefined;
+
+try {
+ if (logo == undefined) return;
+ const dir = dirname(fileURLToPath(import.meta.url)) + "/../../../public/media";
+ svg = (await import(`${dir}/${logo.replaceAll(".", "/")}.svg?raw` /* @vite-ignore */)).default;
+
+ if (svg == undefined) return;
+
+ if (typeof size == "number" || Array.isArray(size)) {
+ // SVG main element regex
+ const svgElementRegex = /<svg[^>]*>/;
+
+ svg = svg.replace(svgElementRegex, (match) => {
+ let newMatch = match;
+ if (typeof size == "number") {
+ newMatch = newMatch.replace(/width="[^"]*"/, `width="${size}"`);
+ newMatch = newMatch.replace(/height="[^"]*"/, `height="${size}"`);
+ } else if (Array.isArray(size)) {
+ newMatch = newMatch.replace(/width="[^"]*"/, `width="${size[0]}"`);
+ newMatch = newMatch.replace(/height="[^"]*"/, `height="${size[1]}"`);
+ }
+ return newMatch;
+ });
+ }
+
+ svg = svg.replace(/<svg/, `<svg ${Object.entries(attr).map(([key, value]) => `${key}="${value}"`).join(" ")}`);
+} catch (err) {
+ if (typeof silent != "boolean" || silent == false) console.error(err);
+}
+
+---
+
+<Fragment set:html={svg} />