11import type { BadgePreset , ImageFormat , Sponsor , SponsorkitRenderOptions , Sponsorship } from '../types.ts'
2- import crypto from 'node:crypto'
32import { resizeImage } from './image.ts'
43
54export function genSvgImage (
@@ -9,8 +8,8 @@ export function genSvgImage(
98 radius : number ,
109 base64Image : string ,
1110 imageFormat : ImageFormat ,
11+ cropId : string ,
1212) {
13- const cropId = `c${ crypto . createHash ( 'md5' ) . update ( base64Image ) . digest ( 'hex' ) . slice ( 0 , 6 ) } `
1413 return `
1514 <clipPath id="${ cropId } ">
1615 <rect x="${ x } " y="${ y } " width="${ size } " height="${ size } " rx="${ size * radius } " ry="${ size * radius } " />
@@ -25,6 +24,7 @@ export async function generateBadge(
2524 preset : BadgePreset ,
2625 radius : number ,
2726 imageFormat : ImageFormat ,
27+ cropId : string ,
2828) {
2929 const { login } = sponsor
3030 let name = ( sponsor . name || sponsor . login ) . trim ( )
@@ -55,20 +55,25 @@ export async function generateBadge(
5555 ${ preset . name
5656 ? `<text x="${ x + size / 2 } " y="${ y + size + 18 } " text-anchor="middle" class="${ preset . name . classes || 'sponsorkit-name' } " fill="${ preset . name . color || 'currentColor' } ">${ encodeHtmlEntities ( name ) } </text>
5757 `
58- : '' } ${ genSvgImage ( x , y , size , radius , avatarBase64 , imageFormat ) }
58+ : '' } ${ genSvgImage ( x , y , size , radius , avatarBase64 , imageFormat , cropId ) }
5959</a>` . trim ( )
6060}
6161
6262export class SvgComposer {
6363 height = 0
6464 body = ''
65+ private cropId = 0
6566
6667 readonly config : Required < SponsorkitRenderOptions >
6768
6869 constructor ( config : Required < SponsorkitRenderOptions > ) {
6970 this . config = config
7071 }
7172
73+ getNextCropId ( ) {
74+ return `c${ this . cropId ++ } `
75+ }
76+
7277 addSpan ( height = 0 ) {
7378 this . height += height
7479 return this
@@ -96,7 +101,8 @@ export class SvgComposer {
96101 const x = offsetX + preset . boxWidth * i
97102 const y = this . height
98103 const radius = s . sponsor . type === 'Organization' ? 0.1 : 0.5
99- return await generateBadge ( x , y , s . sponsor , preset , radius , this . config . imageFormat )
104+ const cropId = this . getNextCropId ( )
105+ return await generateBadge ( x , y , s . sponsor , preset , radius , this . config . imageFormat , cropId )
100106 } ) )
101107
102108 this . body += sponsorLine . join ( '\n' )
0 commit comments