Files

51 lines
1.4 KiB
TypeScript
Raw Permalink Normal View History

2026-11-06 13:05:36 +01:00
import { defineHandler } from 'nitro/h3';
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';
import createLucideIcon from 'lucide-react/src/createLucideIcon';
2026-11-06 13:05:36 +01:00
export default defineHandler((event) => {
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, '');
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',
);
const children = [];
2026-02-26 17:34:02 +01:00
const Icon = createLucideIcon(name, iconNodes[name]);
2026-02-26 16:57:10 +01:00
const oldSrc = iconNodes[name]
2026-02-26 17:34:02 +01:00
? renderToStaticMarkup(<Icon />)
.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');
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>,
);
});