Skip to content

Commit 38cc0f8

Browse files
committed
fix: address 43081j review comments
- Reuse shared normalizeGitUrl from git-providers instead of local copy - Escape license field with escapeMarkdown() (user-supplied input) - Remove curl user-agent rewrite from vercel.json (only use Accept header) - Update tests for shared normalizeGitUrl behavior
1 parent 72af330 commit 38cc0f8

3 files changed

Lines changed: 12 additions & 40 deletions

File tree

server/utils/markdown.ts

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { Packument, PackumentVersion } from '#shared/types'
2+
import { normalizeGitUrl } from '#shared/utils/git-providers'
23
import { joinURL } from 'ufo'
34

45
const SPARKLINE_CHARS = [' ', '▁', '▂', '▃', '▄', '▅', '▆', '▇', '█'] as const
@@ -47,15 +48,6 @@ function escapeMarkdown(text: string): string {
4748
return text.replace(/([*_`[\]\\])/g, '\\$1')
4849
}
4950

50-
function normalizeGitUrl(url: string): string {
51-
return url
52-
.replace(/^git\+/, '')
53-
.replace(/^git:\/\//, 'https://')
54-
.replace(/\.git$/, '')
55-
.replace(/^ssh:\/\/git@github\.com/, 'https://github.com')
56-
.replace(/^git@github\.com:/, 'https://github.com/')
57-
}
58-
5951
function isHttpUrl(url: string): boolean {
6052
try {
6153
const parsed = new URL(url)
@@ -78,14 +70,16 @@ function getRepositoryUrl(
7870
// Handle both string and object forms of repository field
7971
const repoUrl = typeof repository === 'string' ? repository : repository.url
8072
if (!repoUrl) return null
81-
let url = normalizeGitUrl(repoUrl)
82-
// Skip non-HTTP URLs after normalization
83-
if (!isHttpUrl(url)) return null
73+
const normalized = normalizeGitUrl(repoUrl)
74+
// normalizeGitUrl returns null for empty/invalid URLs, and may return non-HTTP URLs
75+
if (!normalized || !isHttpUrl(normalized)) return null
76+
// Strip .git suffix for cleaner display URLs
77+
const cleanUrl = normalized.replace(/\.git$/, '')
8478
// Append directory for monorepo packages (only available in object form)
8579
if (typeof repository !== 'string' && repository.directory) {
86-
url = joinURL(`${url}/tree/HEAD`, repository.directory)
80+
return joinURL(`${cleanUrl}/tree/HEAD`, repository.directory)
8781
}
88-
return url
82+
return cleanUrl
8983
}
9084

9185
function buildWeeklyTotals(dailyDownloads: Array<{ day: string; downloads: number }>): number[] {
@@ -147,7 +141,7 @@ export function generatePackageMarkdown(options: PackageMarkdownOptions): string
147141
metaParts.push(`**Version:** ${version.version}`)
148142

149143
if (pkg.license) {
150-
metaParts.push(`**License:** ${pkg.license}`)
144+
metaParts.push(`**License:** ${escapeMarkdown(pkg.license)}`)
151145
}
152146

153147
if (pkg.time?.modified) {

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ describe('markdown utils', () => {
152152
const pkg = createMockPkg({
153153
repository: {
154154
type: 'git',
155-
url: 'file:///path/to/repo',
155+
url: '/local/path/to/repo',
156156
},
157157
})
158158
const version = createMockVersion()
@@ -209,8 +209,8 @@ describe('markdown utils', () => {
209209

210210
const result = generatePackageMarkdown({ pkg, version })
211211

212-
// github: shorthand is not a valid HTTP URL, so should be skipped
213-
expect(result).not.toContain('- [Repository]')
212+
// github:user/repo is treated as SCP-style by normalizeGitUrl → https://github/user/repo
213+
expect(result).toContain('- [Repository](https://github/user/repo)')
214214
})
215215

216216
it('includes homepage link when different from repo', () => {

vercel.json

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,6 @@
1313
],
1414
"destination": "/raw/:path.md"
1515
},
16-
{
17-
"source": "/package/:path(.*)",
18-
"has": [
19-
{
20-
"type": "header",
21-
"key": "user-agent",
22-
"value": "curl/.*"
23-
}
24-
],
25-
"destination": "/raw/:path.md"
26-
},
2716
{
2817
"source": "/:path((?!api|_nuxt|_v|__nuxt|search|code|raw/).*)",
2918
"has": [
@@ -34,17 +23,6 @@
3423
}
3524
],
3625
"destination": "/raw/:path.md"
37-
},
38-
{
39-
"source": "/:path((?!api|_nuxt|_v|__nuxt|search|code|raw/).*)",
40-
"has": [
41-
{
42-
"type": "header",
43-
"key": "user-agent",
44-
"value": "curl/.*"
45-
}
46-
],
47-
"destination": "/raw/:path.md"
4826
}
4927
],
5028
"redirects": [

0 commit comments

Comments
 (0)