2024-02-01 22:38:21 +09:00
|
|
|
import { eventHandler, setResponseHeader, defaultContentType } from 'h3';
|
|
|
|
|
import { renderToString } from 'react-dom/server';
|
|
|
|
|
import { createElement } from 'react';
|
2023-06-04 17:52:03 +02:00
|
|
|
import SvgPreview from '../../../lib/SvgPreview/index.tsx';
|
2024-02-01 22:38:21 +09:00
|
|
|
import createLucideIcon, { IconNode } from 'lucide-react/src/createLucideIcon';
|
2023-06-04 17:52:03 +02:00
|
|
|
import { parseSync } from 'svgson';
|
|
|
|
|
|
|
|
|
|
export default eventHandler((event) => {
|
2024-02-01 22:38:21 +09:00
|
|
|
const { params } = event.context;
|
2023-06-04 17:52:03 +02:00
|
|
|
|
|
|
|
|
const [strokeWidth, svgData] = params.data.split('/');
|
|
|
|
|
const data = svgData.slice(0, -4);
|
|
|
|
|
|
|
|
|
|
const src = Buffer.from(data, 'base64').toString('utf8');
|
|
|
|
|
|
|
|
|
|
const Icon = createLucideIcon(
|
|
|
|
|
'icon',
|
|
|
|
|
parseSync(src.includes('<svg') ? src : `<svg>${src}</svg>`).children.map(
|
2024-02-01 22:38:21 +09:00
|
|
|
({ name, attributes }) => [name, attributes],
|
|
|
|
|
) as IconNode,
|
2023-06-04 17:52:03 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const svg = Buffer.from(
|
|
|
|
|
// We can't use jsx here, is not supported here by nitro.
|
2023-08-15 19:32:25 +02:00
|
|
|
renderToString(createElement(Icon, { strokeWidth }))
|
|
|
|
|
.replace(/fill\="none"/, 'fill="#fff"')
|
|
|
|
|
.replace(
|
|
|
|
|
/>/,
|
|
|
|
|
`><style>
|
|
|
|
|
@media screen and (prefers-color-scheme: light) {
|
|
|
|
|
svg { fill: transparent !important; }
|
|
|
|
|
}
|
|
|
|
|
@media screen and (prefers-color-scheme: dark) {
|
|
|
|
|
svg { stroke: #fff; fill: transparent !important; }
|
|
|
|
|
}
|
2024-02-01 22:38:21 +09:00
|
|
|
</style>`,
|
|
|
|
|
),
|
2023-06-04 17:52:03 +02:00
|
|
|
).toString('utf8');
|
|
|
|
|
|
2024-02-01 22:38:21 +09:00
|
|
|
defaultContentType(event, 'image/svg+xml');
|
|
|
|
|
setResponseHeader(event, 'Cache-Control', 'public,max-age=31536000');
|
2023-06-04 17:52:03 +02:00
|
|
|
|
2024-02-01 22:38:21 +09:00
|
|
|
return svg;
|
|
|
|
|
});
|