Skip to content

Commit c4ed837

Browse files
authored
feat: rewrite npmjs.org links (#1573)
1 parent 8d040ad commit c4ed837

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

server/utils/readme.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,8 +239,10 @@ const reservedPathsNpmJs = [
239239
'policies',
240240
]
241241

242+
const npmJsHosts = new Set(['www.npmjs.com', 'npmjs.com', 'www.npmjs.org', 'npmjs.org'])
243+
242244
const isNpmJsUrlThatCanBeRedirected = (url: URL) => {
243-
if (url.host !== 'www.npmjs.com' && url.host !== 'npmjs.com') {
245+
if (!npmJsHosts.has(url.host)) {
244246
return false
245247
}
246248

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,20 @@ describe('Markdown File URL Resolution', () => {
341341

342342
expect(result.html).toContain('href="https://www.npmjs.com/products"')
343343
})
344+
345+
it('redirects npmjs.org urls to local', async () => {
346+
const markdown = `[Some npmjs.org link](https://www.npmjs.org/package/test-pkg)`
347+
const result = await renderReadmeHtml(markdown, 'test-pkg')
348+
349+
expect(result.html).toContain('href="/package/test-pkg"')
350+
})
351+
352+
it('redirects npmjs.org urls to local (no www and http)', async () => {
353+
const markdown = `[Some npmjs.org link](http://npmjs.org/package/test-pkg)`
354+
const result = await renderReadmeHtml(markdown, 'test-pkg')
355+
356+
expect(result.html).toContain('href="/package/test-pkg"')
357+
})
344358
})
345359
})
346360

0 commit comments

Comments
 (0)