-
-
Notifications
You must be signed in to change notification settings - Fork 390
feat: add markdown output support for package pages #151
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
BYK
wants to merge
1
commit into
npmx-dev:main
Choose a base branch
from
BYK:byk/feat/md
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,172 @@ | ||
| import { generatePackageMarkdown } from '../../utils/markdown' | ||
BYK marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| import { | ||
| isStandardReadme, | ||
| fetchReadmeFromJsdelivr, | ||
| } from '../../utils/readme-loaders' | ||
| import * as v from 'valibot' | ||
| import { PackageRouteParamsSchema } from '#shared/schemas/package' | ||
| import { NPM_MISSING_README_SENTINEL, ERROR_NPM_FETCH_FAILED } from '#shared/utils/constants' | ||
|
|
||
| // Cache TTL matches the ISR config for /raw/** routes (60 seconds) | ||
| const CACHE_MAX_AGE = 60 | ||
|
|
||
| const NPM_API = 'https://api.npmjs.org' | ||
|
|
||
| const standardReadmeFilenames = [ | ||
| 'README.md', | ||
| 'readme.md', | ||
| 'Readme.md', | ||
| 'README', | ||
| 'readme', | ||
| 'README.markdown', | ||
| 'readme.markdown', | ||
| ] | ||
|
|
||
| function encodePackageName(name: string): string { | ||
| if (name.startsWith('@')) { | ||
| return `@${encodeURIComponent(name.slice(1))}` | ||
| } | ||
| return encodeURIComponent(name) | ||
| } | ||
|
|
||
| async function fetchWeeklyDownloads(packageName: string): Promise<{ downloads: number } | null> { | ||
BYK marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| try { | ||
| const encodedName = encodePackageName(packageName) | ||
| return await $fetch<{ downloads: number }>( | ||
| `${NPM_API}/downloads/point/last-week/${encodedName}`, | ||
| ) | ||
| } catch { | ||
| return null | ||
| } | ||
| } | ||
|
|
||
| function parsePackageParamsFromSlug(slug: string): { | ||
| rawPackageName: string | ||
| rawVersion: string | undefined | ||
| } { | ||
| const segments = slug.split('/').filter(Boolean) | ||
|
|
||
| if (segments.length === 0) { | ||
| return { rawPackageName: '', rawVersion: undefined } | ||
| } | ||
|
|
||
| const vIndex = segments.indexOf('v') | ||
|
|
||
| if (vIndex !== -1 && vIndex < segments.length - 1) { | ||
| return { | ||
| rawPackageName: segments.slice(0, vIndex).join('/'), | ||
| rawVersion: segments.slice(vIndex + 1).join('/'), | ||
| } | ||
| } | ||
|
|
||
| const fullPath = segments.join('/') | ||
| const versionMatch = fullPath.match(/^(@[^/]+\/[^@]+|[^@]+)@(.+)$/) | ||
| if (versionMatch) { | ||
| const [, packageName, version] = versionMatch as [string, string, string] | ||
| return { | ||
| rawPackageName: packageName, | ||
| rawVersion: version, | ||
| } | ||
| } | ||
|
|
||
| return { | ||
| rawPackageName: fullPath, | ||
| rawVersion: undefined, | ||
| } | ||
| } | ||
|
|
||
| export default defineEventHandler(async event => { | ||
| // Get the slug parameter - Nitro captures it as "slug.md" due to the route pattern | ||
| const params = getRouterParams(event) | ||
| const slugParam = params['slug.md'] || params.slug | ||
|
|
||
| if (!slugParam) { | ||
| throw createError({ | ||
| statusCode: 404, | ||
| statusMessage: 'Package not found', | ||
| }) | ||
| } | ||
|
|
||
| // Remove .md suffix if present (it will be there from the route) | ||
| const slug = slugParam.endsWith('.md') ? slugParam.slice(0, -3) : slugParam | ||
|
|
||
| const { rawPackageName, rawVersion } = parsePackageParamsFromSlug(slug) | ||
|
|
||
| if (!rawPackageName) { | ||
| throw createError({ | ||
| statusCode: 404, | ||
| statusMessage: 'Package not found', | ||
| }) | ||
| } | ||
|
|
||
| const { packageName, version } = v.parse(PackageRouteParamsSchema, { | ||
| packageName: rawPackageName, | ||
| version: rawVersion, | ||
| }) | ||
|
|
||
| let packageData | ||
| try { | ||
| packageData = await fetchNpmPackage(packageName) | ||
| } catch { | ||
| throw createError({ | ||
| statusCode: 502, | ||
| statusMessage: ERROR_NPM_FETCH_FAILED, | ||
| }) | ||
| } | ||
|
|
||
| let targetVersion = version | ||
| if (!targetVersion) { | ||
| targetVersion = packageData['dist-tags']?.latest | ||
| } | ||
|
|
||
| if (!targetVersion) { | ||
| throw createError({ | ||
| statusCode: 404, | ||
| statusMessage: 'Package version not found', | ||
| }) | ||
| } | ||
|
|
||
| const versionData = packageData.versions[targetVersion] | ||
| if (!versionData) { | ||
| throw createError({ | ||
| statusCode: 404, | ||
| statusMessage: 'Package version not found', | ||
| }) | ||
| } | ||
|
|
||
| let readmeContent: string | undefined | ||
|
|
||
| if (version) { | ||
| readmeContent = versionData.readme | ||
| } else { | ||
| readmeContent = packageData.readme | ||
| } | ||
BYK marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| const readmeFilename = version ? versionData.readmeFilename : packageData.readmeFilename | ||
| const hasValidNpmReadme = readmeContent && readmeContent !== NPM_MISSING_README_SENTINEL | ||
|
|
||
| if (!hasValidNpmReadme || !isStandardReadme(readmeFilename)) { | ||
| const jsdelivrReadme = await fetchReadmeFromJsdelivr( | ||
| packageName, | ||
| standardReadmeFilenames, | ||
| targetVersion, | ||
| ) | ||
| if (jsdelivrReadme) { | ||
| readmeContent = jsdelivrReadme | ||
| } | ||
| } | ||
|
|
||
| const weeklyDownloadsData = await fetchWeeklyDownloads(packageName) | ||
|
|
||
| const markdown = generatePackageMarkdown({ | ||
| pkg: packageData, | ||
| version: versionData, | ||
| readme: readmeContent && readmeContent !== NPM_MISSING_README_SENTINEL ? readmeContent : null, | ||
| weeklyDownloads: weeklyDownloadsData?.downloads, | ||
| }) | ||
|
|
||
| setHeader(event, 'Content-Type', 'text/markdown; charset=utf-8') | ||
| setHeader(event, 'Cache-Control', `public, max-age=${CACHE_MAX_AGE}, stale-while-revalidate`) | ||
|
|
||
| return markdown | ||
| }) | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.