Skip to content

Commit 7bef5a1

Browse files
committed
fix: allow to parse all link data on html links as well
1 parent 44e6b07 commit 7bef5a1

File tree

1 file changed

+29
-21
lines changed

1 file changed

+29
-21
lines changed

server/utils/readme.ts

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -411,31 +411,15 @@ ${html}
411411
return `<img src="${resolvedHref}"${altAttr}${titleAttr}>`
412412
}
413413

414-
// Resolve link URLs, add security attributes, and collect playground links
414+
// // Resolve link URLs, add security attributes, and collect playground links
415415
renderer.link = function ({ href, title, tokens }: Tokens.Link) {
416-
const resolvedHref = resolveUrl(href, packageName, repoInfo)
417416
const text = this.parser.parseInline(tokens)
418417
const titleAttr = title ? ` title="${title}"` : ''
418+
const plainText = text.replace(/<[^>]*>/g, '').trim()
419419

420-
// Check if this is a playground link
421-
const provider = matchPlaygroundProvider(resolvedHref)
422-
if (provider && !seenUrls.has(resolvedHref)) {
423-
seenUrls.add(resolvedHref)
424-
425-
// Extract label from link text (strip HTML tags for plain text)
426-
const plainText = text.replace(/<[^>]*>/g, '').trim()
427-
428-
collectedLinks.push({
429-
url: resolvedHref,
430-
provider: provider.id,
431-
providerName: provider.name,
432-
label: plainText || title || provider.name,
433-
})
434-
}
435-
436-
const hrefValue = resolvedHref.startsWith('#') ? resolvedHref.toLowerCase() : resolvedHref
420+
const intermediateTitleAttr = `${` data-title-intermediate="${plainText || title}"`}`
437421

438-
return `<a href="${hrefValue}"${titleAttr}>${text}</a>`
422+
return `<a href="${href}"${titleAttr}${intermediateTitleAttr}>${text}</a>`
439423
}
440424

441425
// GitHub-style callouts: > [!NOTE], > [!TIP], etc.
@@ -518,11 +502,35 @@ ${html}
518502
return { tagName, attribs }
519503
},
520504
a: (tagName, attribs) => {
505+
if (!attribs.href) {
506+
return { tagName, attribs }
507+
}
508+
509+
const resolvedHref = resolveUrl(attribs.href, packageName, repoInfo)
510+
511+
const provider = matchPlaygroundProvider(resolvedHref)
512+
if (provider && !seenUrls.has(resolvedHref)) {
513+
seenUrls.add(resolvedHref)
514+
515+
collectedLinks.push({
516+
url: resolvedHref,
517+
provider: provider.id,
518+
providerName: provider.name,
519+
/**
520+
* We need to set some data attribute before hand because `transformTags` doesn't
521+
* provide the text of the element. This will automatically be removed, because there
522+
* is an allow list for link attributes.
523+
* */
524+
label: attribs['data-title-intermediate'] || provider.name,
525+
})
526+
}
527+
521528
// Add security attributes for external links
522-
if (attribs.href && hasProtocol(attribs.href, { acceptRelative: true })) {
529+
if (resolvedHref && hasProtocol(resolvedHref, { acceptRelative: true })) {
523530
attribs.rel = 'nofollow noreferrer noopener'
524531
attribs.target = '_blank'
525532
}
533+
attribs.href = resolvedHref
526534
return { tagName, attribs }
527535
},
528536
div: prefixId,

0 commit comments

Comments
 (0)