Skip to content

Commit 1816ab4

Browse files
committed
fix: extract label from image-only playground links
1 parent 2d805c5 commit 1816ab4

2 files changed

Lines changed: 19 additions & 1 deletion

File tree

server/utils/readme.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,12 @@ ${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

420425
const intermediateTitleAttr = `${` data-title-intermediate="${plainText || title}"`}`
421426

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)