[READ-ONLY] a fast, modern browser for the npm registry

fix: resolve srcset URLs in <source> elements (#188)

+20 -1
+1 -1
server/api/registry/readme/[...pkg].get.ts
··· 126 126 swr: true, 127 127 getKey: event => { 128 128 const pkg = getRouterParam(event, 'pkg') ?? '' 129 - return `readme:v4:${pkg.replace(/\/+$/, '').trim()}` 129 + return `readme:v5:${pkg.replace(/\/+$/, '').trim()}` 130 130 }, 131 131 }, 132 132 )
+19
server/utils/readme.ts
··· 354 354 } 355 355 return { tagName, attribs } 356 356 }, 357 + source: (tagName, attribs) => { 358 + if (attribs.src) { 359 + attribs.src = resolveImageUrl(attribs.src, packageName, repoInfo) 360 + } 361 + if (attribs.srcset) { 362 + attribs.srcset = attribs.srcset 363 + .split(',') 364 + .map(entry => { 365 + const parts = entry.trim().split(/\s+/) 366 + const url = parts[0] 367 + if (!url) return entry.trim() 368 + const descriptor = parts[1] 369 + const resolvedUrl = resolveImageUrl(url, packageName, repoInfo) 370 + return descriptor ? `${resolvedUrl} ${descriptor}` : resolvedUrl 371 + }) 372 + .join(', ') 373 + } 374 + return { tagName, attribs } 375 + }, 357 376 a: (tagName, attribs) => { 358 377 // Add security attributes for external links 359 378 if (attribs.href && hasProtocol(attribs.href, { acceptRelative: true })) {