Skip to content

Commit dfba361

Browse files
committed
feat: rewrite npmjs.org
1 parent 24a2c1a commit dfba361

2 files changed

Lines changed: 17 additions & 1 deletion

File tree

server/utils/readme.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,10 @@ const reservedPathsNpmJs = [
220220
'policies',
221221
]
222222

223+
const npmJsHosts = new Set(['www.npmjs.com', 'npmjs.com', 'www.npmjs.org', 'npmjs.org'])
224+
223225
const isNpmJsUrlThatCanBeRedirected = (url: URL) => {
224-
if (url.host !== 'www.npmjs.com' && url.host !== 'npmjs.com') {
226+
if (!npmJsHosts.has(url.host)) {
225227
return false
226228
}
227229

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)