Skip to content

Commit 338e5c8

Browse files
committed
fix(api/readme): resolve srcset URLs in <source> elements
Fixes #161.
1 parent 4232ae2 commit 338e5c8

2 files changed

Lines changed: 20 additions & 1 deletion

File tree

server/api/registry/readme/[...pkg].get.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ export default defineCachedEventHandler(
126126
swr: true,
127127
getKey: event => {
128128
const pkg = getRouterParam(event, 'pkg') ?? ''
129-
return `readme:v4:${pkg.replace(/\/+$/, '').trim()}`
129+
return `readme:v5:${pkg.replace(/\/+$/, '').trim()}`
130130
},
131131
},
132132
)

server/utils/readme.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,25 @@ export async function renderReadmeHtml(
354354
}
355355
return { tagName, attribs }
356356
},
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+
},
357376
a: (tagName, attribs) => {
358377
// Add security attributes for external links
359378
if (attribs.href && hasProtocol(attribs.href, { acceptRelative: true })) {

0 commit comments

Comments
 (0)