Skip to content

Commit 7e2890e

Browse files
committed
fix: address PR review comments for markdown output
- Fix Cache-Control mismatch: change from 3600s to 60s to match ISR TTL - Add URL validation after normalizeGitUrl() to skip non-HTTP URLs - Fix installSize check: use !== undefined instead of truthiness - Enhance sparkline with numeric trend values for LLM readability - Add comprehensive unit tests for markdown utils (97.97% coverage)
1 parent d853437 commit 7e2890e

File tree

3 files changed

+500
-13
lines changed

3 files changed

+500
-13
lines changed

server/routes/raw/[...slug].md.get.ts

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import { generatePackageMarkdown } from '../../utils/markdown'
22
import * as v from 'valibot'
33
import { PackageRouteParamsSchema } from '#shared/schemas/package'
4-
import {
5-
CACHE_MAX_AGE_ONE_HOUR,
6-
NPM_MISSING_README_SENTINEL,
7-
ERROR_NPM_FETCH_FAILED,
8-
} from '#shared/utils/constants'
4+
import { NPM_MISSING_README_SENTINEL, ERROR_NPM_FETCH_FAILED } from '#shared/utils/constants'
5+
6+
// Cache TTL matches the ISR config for /raw/** routes (60 seconds)
7+
const CACHE_MAX_AGE = 60
98
import { parseRepositoryInfo } from '#shared/utils/git-providers'
109

1110
const NPM_API = 'https://api.npmjs.org'
@@ -225,11 +224,7 @@ export default defineEventHandler(async event => {
225224
})
226225

227226
setHeader(event, 'Content-Type', 'text/markdown; charset=utf-8')
228-
setHeader(
229-
event,
230-
'Cache-Control',
231-
`public, max-age=${CACHE_MAX_AGE_ONE_HOUR}, stale-while-revalidate`,
232-
)
227+
setHeader(event, 'Cache-Control', `public, max-age=${CACHE_MAX_AGE}, stale-while-revalidate`)
233228

234229
return markdown
235230
})

server/utils/markdown.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ function formatNumber(num: number): string {
3030
return new Intl.NumberFormat('en-US').format(num)
3131
}
3232

33+
function formatCompactNumber(num: number): string {
34+
return new Intl.NumberFormat('en-US', { notation: 'compact', maximumFractionDigits: 1 }).format(
35+
num,
36+
)
37+
}
38+
3339
function formatBytes(bytes: number): string {
3440
if (bytes < 1024) return `${bytes} B`
3541
if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} kB`
@@ -65,6 +71,8 @@ function getRepositoryUrl(repository?: {
6571
}): string | null {
6672
if (!repository?.url) return null
6773
let url = normalizeGitUrl(repository.url)
74+
// Skip non-HTTP URLs after normalization
75+
if (!isHttpUrl(url)) return null
6876
// Append directory for monorepo packages
6977
if (repository.directory) {
7078
url = joinURL(`${url}/tree/HEAD`, repository.directory)
@@ -160,7 +168,7 @@ export function generatePackageMarkdown(options: PackageMarkdownOptions): string
160168
const statsSeparators: string[] = []
161169
const statsValues: string[] = []
162170

163-
// Weekly downloads with sparkline
171+
// Weekly downloads with sparkline and trend numbers
164172
if (weeklyDownloads !== undefined) {
165173
statsHeaders.push('Downloads (weekly)')
166174
statsSeparators.push('---')
@@ -169,7 +177,9 @@ export function generatePackageMarkdown(options: PackageMarkdownOptions): string
169177
if (dailyDownloads && dailyDownloads.length > 0) {
170178
const weeklyTotals = buildWeeklyTotals(dailyDownloads)
171179
if (weeklyTotals.length > 1) {
172-
downloadCell += ` ${generateSparkline(weeklyTotals)}`
180+
// Add both numeric trend and visual sparkline for LLM + human readability
181+
const compactTotals = weeklyTotals.map(formatCompactNumber).join(' ')
182+
downloadCell += ` [${compactTotals}] ${generateSparkline(weeklyTotals)}`
173183
}
174184
}
175185
statsValues.push(downloadCell)
@@ -182,7 +192,7 @@ export function generatePackageMarkdown(options: PackageMarkdownOptions): string
182192
statsValues.push(String(depCount))
183193

184194
// Install size
185-
if (installSize) {
195+
if (installSize !== undefined) {
186196
statsHeaders.push('Install Size')
187197
statsSeparators.push('---')
188198
statsValues.push(formatBytes(installSize))

0 commit comments

Comments
 (0)