computer-scienceangular-roadmapbackend-roadmapblockchain-roadmapdba-roadmapdeveloper-roadmapdevops-roadmapfrontend-roadmapgo-roadmaphactoberfestjava-roadmapjavascript-roadmapnodejs-roadmappython-roadmapqa-roadmapreact-roadmaproadmapstudy-planvue-roadmapweb3-roadmap
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
39 lines
854 B
39 lines
854 B
2 years ago
|
---
|
||
|
import { parse } from 'node-html-parser';
|
||
2 years ago
|
import type { Attributes } from 'node-html-parser/dist/nodes/html';
|
||
2 years ago
|
|
||
|
export interface Props {
|
||
|
icon: string;
|
||
2 years ago
|
class?: string;
|
||
2 years ago
|
}
|
||
|
|
||
|
async function getSVG(name: string) {
|
||
|
const filepath = `/src/icons/${name}.svg`;
|
||
|
|
||
|
const files = import.meta.glob<string>('/src/icons/**/*.svg', {
|
||
|
eager: true,
|
||
|
as: 'raw',
|
||
|
});
|
||
|
|
||
|
if (!(filepath in files)) {
|
||
|
throw new Error(`${filepath} not found`);
|
||
|
}
|
||
|
|
||
|
const root = parse(files[filepath]);
|
||
|
|
||
|
const svg = root.querySelector('svg');
|
||
|
|
||
|
return {
|
||
|
attributes: svg?.attributes,
|
||
|
innerHTML: svg?.innerHTML,
|
||
|
};
|
||
|
}
|
||
|
|
||
|
const { icon, ...attributes } = Astro.props as Props;
|
||
|
const { attributes: baseAttributes, innerHTML } = await getSVG(icon);
|
||
|
|
||
|
const svgAttributes = { ...baseAttributes, ...attributes };
|
||
|
---
|
||
|
|
||
2 years ago
|
<svg {...svgAttributes} set:html={innerHTML}></svg>
|