Skip to content

Commit 7f3f451

Browse files
committed
refactor: use shared utility for get package manager for markdown
1 parent 72a495a commit 7f3f451

4 files changed

Lines changed: 22 additions & 14 deletions

File tree

src/components/CopyPageDropdown.tsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ import {
1010
DropdownContent,
1111
DropdownItem,
1212
} from './Dropdown'
13-
import { PACKAGE_MANAGERS } from '~/utils/markdown/installCommand'
13+
import {
14+
getPackageManager,
15+
PACKAGE_MANAGERS,
16+
} from '~/utils/markdown/installCommand'
1417

1518
// Markdown icon component matching the screenshot
1619
function MarkdownIcon({ className }: { className?: string }) {
@@ -121,9 +124,8 @@ export function CopyPageDropdown({
121124
// Read package manager from localStorage (same key as PackageManagerTabs)
122125
if (typeof localStorage !== 'undefined') {
123126
const pm = localStorage.getItem('packageManager')
124-
if (pm && PACKAGE_MANAGERS.includes(pm as any)) {
125-
params.set('pm', pm)
126-
}
127+
const validPm = getPackageManager(pm)
128+
params.set('pm', validPm)
127129
}
128130
const queryString = params.toString()
129131
return queryString ? `${base}?${queryString}` : base

src/routes/$libraryId/$version.docs.framework.$framework.{$}[.]md.tsx

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { findLibrary, getBranch } from '~/libraries'
22
import { loadDocs } from '~/utils/docs'
33
import { notFound, createFileRoute } from '@tanstack/react-router'
44
import { filterFrameworkContent } from '~/utils/markdown/filterFrameworkContent'
5+
import { getPackageManager } from '~/utils/markdown/installCommand'
56

67
export const Route = createFileRoute(
78
'/$libraryId/$version/docs/framework/$framework/{$}.md',
@@ -21,11 +22,7 @@ export const Route = createFileRoute(
2122
}
2223
}) => {
2324
const url = new URL(request.url)
24-
const pm = (url.searchParams.get('pm') || 'npm') as
25-
| 'npm'
26-
| 'pnpm'
27-
| 'yarn'
28-
| 'bun'
25+
const pm = getPackageManager(url.searchParams.get('pm'))
2926
const keepMarkers = url.searchParams.get('keep_markers') === 'true'
3027

3128
const { libraryId, version, framework, _splat: docsPath } = params

src/routes/$libraryId/$version.docs.{$}[.]md.tsx

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { createFileRoute } from '@tanstack/react-router'
22
import { getBranch, getLibrary, type LibraryId } from '~/libraries'
33
import { loadDocs } from '~/utils/docs'
44
import { filterFrameworkContent } from '~/utils/markdown/filterFrameworkContent'
5+
import { getPackageManager } from '~/utils/markdown/installCommand'
56

67
export const Route = createFileRoute('/$libraryId/$version/docs/{$}.md')({
78
server: {
@@ -15,11 +16,7 @@ export const Route = createFileRoute('/$libraryId/$version/docs/{$}.md')({
1516
}) => {
1617
const url = new URL(request.url)
1718
const framework = url.searchParams.get('framework')
18-
const pm = (url.searchParams.get('pm') || 'npm') as
19-
| 'npm'
20-
| 'pnpm'
21-
| 'yarn'
22-
| 'bun'
19+
const pm = getPackageManager(url.searchParams.get('pm'))
2320
const keepMarkers = url.searchParams.get('keep_markers') === 'true'
2421

2522
const { libraryId, version, _splat: docsPath } = params

src/utils/markdown/installCommand.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,18 @@ export type InstallMode =
1414
| 'create'
1515
| 'custom'
1616

17+
/**
18+
* Get package manager from query parameter, defaulting to 'npm' if not specified or invalid.
19+
*/
20+
export function getPackageManager(
21+
pm: string | null | undefined,
22+
): PackageManager {
23+
if (pm && PACKAGE_MANAGERS.includes(pm as PackageManager)) {
24+
return pm as PackageManager
25+
}
26+
return 'npm' // default
27+
}
28+
1729
/**
1830
* Generate install command(s) for a package manager.
1931
* Each group of packages becomes one command line.

0 commit comments

Comments
 (0)