2026-11-06 13:05:36 +01:00
|
|
|
import { defineHandler } from 'nitro/h3';
|
2024-09-01 21:04:36 +02:00
|
|
|
import { renderToString, renderToStaticMarkup } from 'react-dom/server';
|
2026-02-26 16:57:10 +01:00
|
|
|
import React from 'react';
|
2026-03-05 12:27:58 +01:00
|
|
|
import Diff from '~/.vitepress/lib/SvgPreview/Diff.tsx';
|
|
|
|
|
import iconNodes from '~/.vitepress/data/iconNodes';
|
2024-09-01 21:04:36 +02:00
|
|
|
import createLucideIcon from 'lucide-react/src/createLucideIcon';
|
|
|
|
|
|
2026-11-06 13:05:36 +01:00
|
|
|
export default defineHandler((event) => {
|
2024-09-01 21:04:36 +02:00
|
|
|
const { params } = event.context;
|
|
|
|
|
|
|
|
|
|
const pathData = params.data.split('/');
|
|
|
|
|
const data = pathData.at(-1).slice(0, -4);
|
|
|
|
|
const [name] = pathData;
|
|
|
|
|
|
|
|
|
|
const newSrc = Buffer.from(data, 'base64')
|
|
|
|
|
.toString('utf8')
|
|
|
|
|
.replaceAll('\n', '')
|
|
|
|
|
.replace(/<svg[^>]*>|<\/svg>/g, '');
|
|
|
|
|
|
2025-08-07 11:29:21 +02:00
|
|
|
const width = parseInt(
|
|
|
|
|
(newSrc.includes('<svg ') ? newSrc.match(/width="(\d+)"/)?.[1] : null) ?? '24',
|
|
|
|
|
);
|
|
|
|
|
const height = parseInt(
|
|
|
|
|
(newSrc.includes('<svg ') ? newSrc.match(/height="(\d+)"/)?.[1] : null) ?? '24',
|
|
|
|
|
);
|
|
|
|
|
|
2024-09-01 21:04:36 +02:00
|
|
|
const children = [];
|
|
|
|
|
|
2026-02-26 17:34:02 +01:00
|
|
|
const Icon = createLucideIcon(name, iconNodes[name]);
|
2026-02-26 16:57:10 +01:00
|
|
|
|
2024-09-01 21:04:36 +02:00
|
|
|
const oldSrc = iconNodes[name]
|
2026-02-26 17:34:02 +01:00
|
|
|
? renderToStaticMarkup(<Icon />)
|
2024-09-01 21:04:36 +02:00
|
|
|
.replaceAll('\n', '')
|
|
|
|
|
.replace(/<svg[^>]*>|<\/svg>/g, '')
|
|
|
|
|
: '';
|
|
|
|
|
|
2026-11-06 13:05:36 +01:00
|
|
|
event.res.headers.set('Content-Type', 'image/svg+xml');
|
|
|
|
|
event.res.headers.set('Cache-Control', 'public,max-age=31536000');
|
2024-09-01 21:04:36 +02:00
|
|
|
|
2026-02-26 16:57:10 +01:00
|
|
|
return renderToString(
|
|
|
|
|
<Diff
|
|
|
|
|
oldSrc={oldSrc}
|
|
|
|
|
newSrc={newSrc}
|
|
|
|
|
height={height}
|
|
|
|
|
width={width}
|
|
|
|
|
>
|
|
|
|
|
{children}
|
2026-02-26 17:34:02 +01:00
|
|
|
</Diff>,
|
|
|
|
|
);
|
2024-09-01 21:04:36 +02:00
|
|
|
});
|