2024-09-01 21:04:36 +02:00
|
|
|
import { eventHandler, setResponseHeader, defaultContentType } from 'h3';
|
|
|
|
|
import { renderToString, renderToStaticMarkup } from 'react-dom/server';
|
|
|
|
|
import { createElement } from 'react';
|
|
|
|
|
import Diff from '../../../lib/SvgPreview/Diff.tsx';
|
|
|
|
|
import iconNodes from '../../../data/iconNodes';
|
|
|
|
|
import createLucideIcon from 'lucide-react/src/createLucideIcon';
|
|
|
|
|
|
|
|
|
|
export default eventHandler((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, '');
|
|
|
|
|
|
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 = [];
|
|
|
|
|
|
|
|
|
|
const oldSrc = iconNodes[name]
|
|
|
|
|
? renderToStaticMarkup(createElement(createLucideIcon(name, iconNodes[name])))
|
|
|
|
|
.replaceAll('\n', '')
|
|
|
|
|
.replace(/<svg[^>]*>|<\/svg>/g, '')
|
|
|
|
|
: '';
|
|
|
|
|
|
|
|
|
|
const svg = Buffer.from(
|
|
|
|
|
// We can't use jsx here, is not supported here by nitro.
|
2025-08-07 11:29:21 +02:00
|
|
|
renderToString(
|
|
|
|
|
createElement(Diff, { oldSrc, newSrc, showGrid: true, height, width }, children),
|
|
|
|
|
),
|
2024-09-01 21:04:36 +02:00
|
|
|
).toString('utf8');
|
|
|
|
|
|
|
|
|
|
defaultContentType(event, 'image/svg+xml');
|
|
|
|
|
setResponseHeader(event, 'Cache-Control', 'public,max-age=31536000');
|
|
|
|
|
|
|
|
|
|
return svg;
|
|
|
|
|
});
|