Skip to content

Commit 5871b6a

Browse files
committed
fix: replace npmjs links in html
1 parent c28f514 commit 5871b6a

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

server/utils/readme.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,16 @@ const isNpmJsUrlThatCanBeRedirected = (url: URL) => {
210210
return true
211211
}
212212

213+
const replaceHtmlLink = (html: string) => {
214+
return html.replace(/href="([^"]+)"/g, (match, href) => {
215+
if (isNpmJsUrlThatCanBeRedirected(new URL(href, 'https://www.npmjs.com'))) {
216+
const newHref = href.replace(/^https?:\/\/(www\.)?npmjs\.com/, '')
217+
return `href="${newHref}"`
218+
}
219+
return match
220+
})
221+
}
222+
213223
/**
214224
* Resolve a relative URL to an absolute URL.
215225
* If repository info is available, resolve to provider's raw file URLs.
@@ -436,7 +446,14 @@ ${html}
436446
return `<blockquote>${body}</blockquote>\n`
437447
}
438448

439-
marked.setOptions({ renderer })
449+
marked.setOptions({
450+
renderer,
451+
walkTokens: token => {
452+
if (token.type === 'html') {
453+
token.text = replaceHtmlLink(token.text)
454+
}
455+
},
456+
})
440457

441458
const rawHtml = marked.parse(content) as string
442459

0 commit comments

Comments
 (0)