@@ -4,3 +4,39 @@ import { twMerge } from "tailwind-merge";
44export function cn ( ...inputs : ClassValue [ ] ) {
55 return twMerge ( clsx ( inputs ) ) ;
66}
7+
8+ const createCanvasImageUrl = ( width : number , height : number ) : string => {
9+ const canvas = document . createElement ( "canvas" ) ;
10+ canvas . width = width ;
11+ canvas . height = height ;
12+
13+ const ctx = canvas . getContext ( "2d" ) ;
14+ if ( ! ctx ) {
15+ return `https://placehold.co/${ width } x${ height } ` ;
16+ }
17+
18+ ctx . fillStyle = "#f5f5f5" ;
19+ ctx . fillRect ( 0 , 0 , width , height ) ;
20+
21+ const fontSize = Math . max ( 12 , Math . floor ( width * 0.15 ) ) ;
22+ ctx . font = `bold ${ fontSize } px Inter, Arial, Helvetica, sans-serif` ;
23+ ctx . fillStyle = "#888888" ;
24+
25+ const text = `${ width } x ${ height } ` ;
26+ const textWidth = ctx . measureText ( text ) . width ;
27+ const x = ( width - textWidth ) / 2 ;
28+ const y = ( height + fontSize ) / 2 ;
29+
30+ ctx . fillText ( text , x , y ) ;
31+
32+ return canvas . toDataURL ( ) ;
33+ } ;
34+
35+ export function replaceExternalImagesWithCanvas ( html : string ) : string {
36+ return html . replace (
37+ / h t t p s : \/ \/ p l a c e h o l d \. c o \/ ( \d + ) x ( \d + ) / g,
38+ ( _match , width , height ) => {
39+ return createCanvasImageUrl ( parseInt ( width ) , parseInt ( height ) ) ;
40+ }
41+ ) ;
42+ }
0 commit comments