Files
lucide/docs/api/gh-icon/diff/[...data].get.tsx
2026-11-06 13:05:36 +01:00

51 lines
1.4 KiB
TypeScript

import { defineHandler } from 'nitro/h3';
import { renderToString, renderToStaticMarkup } from 'react-dom/server';
import React from 'react';
import Diff from '~/.vitepress/lib/SvgPreview/Diff.tsx';
import iconNodes from '~/.vitepress/data/iconNodes';
import createLucideIcon from 'lucide-react/src/createLucideIcon';
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 = [];
const Icon = createLucideIcon(name, iconNodes[name]);
const oldSrc = iconNodes[name]
? renderToStaticMarkup(<Icon />)
.replaceAll('\n', '')
.replace(/<svg[^>]*>|<\/svg>/g, '')
: '';
event.res.headers.set('Content-Type', 'image/svg+xml');
event.res.headers.set('Cache-Control', 'public,max-age=31536000');
return renderToString(
<Diff
oldSrc={oldSrc}
newSrc={newSrc}
height={height}
width={width}
>
{children}
</Diff>,
);
});