Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion server/utils/readme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,10 @@ const reservedPathsNpmJs = [
'policies',
]

const npmJsHosts = new Set(['www.npmjs.com', 'npmjs.com', 'www.npmjs.org', 'npmjs.org'])

const isNpmJsUrlThatCanBeRedirected = (url: URL) => {
if (url.host !== 'www.npmjs.com' && url.host !== 'npmjs.com') {
if (!npmJsHosts.has(url.host)) {
return false
}

Expand Down
14 changes: 14 additions & 0 deletions test/unit/server/utils/readme.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,20 @@ describe('Markdown File URL Resolution', () => {

expect(result.html).toContain('href="https://www.npmjs.com/products"')
})

it('redirects npmjs.org urls to local', async () => {
const markdown = `[Some npmjs.org link](https://www.npmjs.org/package/test-pkg)`
const result = await renderReadmeHtml(markdown, 'test-pkg')

expect(result.html).toContain('href="/package/test-pkg"')
})

it('redirects npmjs.org urls to local (no www and http)', async () => {
const markdown = `[Some npmjs.org link](http://npmjs.org/package/test-pkg)`
const result = await renderReadmeHtml(markdown, 'test-pkg')

expect(result.html).toContain('href="/package/test-pkg"')
})
})
})

Expand Down
Loading