Skip to content

Commit ae62f9c

Browse files
authored
fix: extract label from image-only playground links (#1462)
1 parent d23916b commit ae62f9c

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

server/utils/readme.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -415,9 +415,15 @@ ${html}
415415
renderer.link = function ({ href, title, tokens }: Tokens.Link) {
416416
const text = this.parser.parseInline(tokens)
417417
const titleAttr = title ? ` title="${title}"` : ''
418-
const plainText = text.replace(/<[^>]*>/g, '').trim()
418+
let plainText = text.replace(/<[^>]*>/g, '').trim()
419+
420+
// If plain text is empty, check if we have an image with alt text
421+
if (!plainText && tokens.length === 1 && tokens[0]?.type === 'image') {
422+
plainText = tokens[0].text
423+
}
419424

420-
const intermediateTitleAttr = `${` data-title-intermediate="${plainText || title}"`}`
425+
const intermediateTitleAttr =
426+
plainText || title ? ` data-title-intermediate="${plainText || title}"` : ''
421427

422428
return `<a href="${href}"${titleAttr}${intermediateTitleAttr}>${text}</a>`
423429
}

test/unit/server/utils/readme.spec.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,19 @@ describe('Playground Link Extraction', () => {
6262
expect(result.playgroundLinks).toHaveLength(1)
6363
expect(result.playgroundLinks[0]!.provider).toBe('codesandbox')
6464
})
65+
66+
it('extracts label from image link', async () => {
67+
const markdown = `[![Edit CodeSandbox](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/s/example-abc123)`
68+
const result = await renderReadmeHtml(markdown, 'test-pkg')
69+
70+
expect(result.playgroundLinks).toHaveLength(1)
71+
expect(result.playgroundLinks[0]).toMatchObject({
72+
provider: 'codesandbox',
73+
providerName: 'CodeSandbox',
74+
label: 'Edit CodeSandbox',
75+
url: 'https://codesandbox.io/s/example-abc123',
76+
})
77+
})
6578
})
6679

6780
describe('Other Providers', () => {

0 commit comments

Comments
 (0)