@@ -7,8 +7,12 @@ import type {
77
88definePageMeta ({
99 name: ' code' ,
10- path: ' /package-code/:path+' ,
11- alias: [' /package/code/:path+' , ' /code/:path+' ],
10+ path: ' /package-code/:org?/:packageName/v/:version/:filePath(.*)?' ,
11+ alias: [
12+ ' /package/code/:org?/:packageName/v/:version/:filePath(.*)?' ,
13+ ' /package/code/:packageName/v/:version/:filePath(.*)?' ,
14+ // '/code/@:org?/:packageName/v/:version/:filePath(.*)?',
15+ ],
1216})
1317
1418const route = useRoute (' code' )
@@ -19,23 +23,11 @@ const route = useRoute('code')
1923// /code/nuxt/v/4.2.0/src/index.ts → packageName: "nuxt", version: "4.2.0", filePath: "src/index.ts"
2024// /code/@nuxt/kit/v/1.0.0 → packageName: "@nuxt/kit", version: "1.0.0", filePath: null
2125const parsedRoute = computed (() => {
22- const segments = route .params .path
23-
24- // Find the /v/ separator for version
25- const vIndex = segments .indexOf (' v' )
26- if (vIndex === - 1 || vIndex >= segments .length - 1 ) {
27- // No version specified - redirect or error
28- return {
29- packageName: segments .join (' /' ),
30- version: null as string | null ,
31- filePath: null as string | null ,
32- }
33- }
34-
35- const packageName = segments .slice (0 , vIndex ).join (' /' )
36- const afterVersion = segments .slice (vIndex + 1 )
37- const version = afterVersion [0 ] ?? null
38- const filePath = afterVersion .length > 1 ? afterVersion .slice (1 ).join (' /' ) : null
26+ const packageName = route .params .org
27+ ? ` ${route .params .org }/${route .params .packageName } `
28+ : route .params .packageName
29+ const version = route .params .version
30+ const filePath = route .params .filePath || null
3931
4032 return { packageName , version , filePath }
4133})
@@ -45,14 +37,31 @@ const version = computed(() => parsedRoute.value.version)
4537const filePathOrig = computed (() => parsedRoute .value .filePath )
4638const filePath = computed (() => parsedRoute .value .filePath ?.replace (/ \/ $ / , ' ' ))
4739
40+ // Navigation helper - build URL for a path
41+ function getCodeUrl(args : {
42+ org? : string
43+ packageName: string
44+ version: string
45+ filePath? : string
46+ }): string {
47+ const base = args .org
48+ ? ` /package-code/${args .org }/${args .packageName }/v/${args .version } `
49+ : ` /package-code/${args .packageName }/v/${args .version } `
50+ return args .filePath ? ` ${base }/${args .filePath } ` : base
51+ }
52+
4853// Fetch package data for version list
4954const { data : pkg } = usePackage (packageName )
5055
5156// URL pattern for version selector - includes file path if present
52- const versionUrlPattern = computed (() => {
53- const base = ` /package-code/${packageName .value }/v/{version} `
54- return filePath .value ? ` ${base }/${filePath .value } ` : base
55- })
57+ const versionUrlPattern = computed (() =>
58+ getCodeUrl ({
59+ org: route .params .org ,
60+ packageName: route .params .packageName ,
61+ version: ' {version}' ,
62+ filePath: filePath .value ,
63+ }),
64+ )
5665
5766// Fetch file tree
5867const { data : fileTree, status : treeStatus } = useFetch <PackageFileTreeResponse >(
@@ -192,17 +201,13 @@ const breadcrumbs = computed(() => {
192201})
193202
194203// Navigation helper - build URL for a path
195- function getCodeUrl(path ? : string ): string {
196- const base = ` /package-code/${packageName .value }/v/${version .value } `
197- return path ? ` ${base }/${path } ` : base
204+ function getCurrentCodeUrlWithPath(path ? : string ): string {
205+ return getCodeUrl ({
206+ ... route .params ,
207+ filePath: path ,
208+ })
198209}
199210
200- // Base path segments for route objects (e.g., ['nuxt', 'v', '4.2.0'] or ['@nuxt', 'kit', 'v', '1.0.0'])
201- const basePath = computed (() => {
202- const segments = packageName .value .split (' /' )
203- return [... segments , ' v' , version .value ?? ' ' ]
204- })
205-
206211// Extract org name from scoped package
207212const orgName = computed (() => {
208213 const name = packageName .value
@@ -244,13 +249,7 @@ function copyPermalinkUrl() {
244249}
245250
246251// Canonical URL for this code page
247- const canonicalUrl = computed (() => {
248- let url = ` https://npmx.dev/package-code/${packageName .value }/v/${version .value } `
249- if (filePath .value ) {
250- url += ` /${filePath .value } `
251- }
252- return url
253- })
252+ const canonicalUrl = computed (() => ` https://npmx.dev${getCodeUrl (route .params )} ` )
254253
255254// Toggle markdown view mode
256255const markdownViewModes = [
@@ -350,7 +349,7 @@ defineOgImageComponent('Default', {
350349 >
351350 <NuxtLink
352351 v-if =" filePath"
353- :to =" getCodeUrl ()"
352+ :to =" getCurrentCodeUrlWithPath ()"
354353 class =" text-fg-muted hover:text-fg transition-colors shrink-0"
355354 >
356355 {{ $t('code.root') }}
@@ -360,7 +359,7 @@ defineOgImageComponent('Default', {
360359 <span class =" text-fg-subtle" >/</span >
361360 <NuxtLink
362361 v-if =" i < breadcrumbs.length - 1"
363- :to =" getCodeUrl (crumb.path)"
362+ :to =" getCurrentCodeUrlWithPath (crumb.path)"
364363 class =" text-fg-muted hover:text-fg transition-colors"
365364 >
366365 {{ crumb.name }}
@@ -402,8 +401,8 @@ defineOgImageComponent('Default', {
402401 <CodeFileTree
403402 :tree =" fileTree.tree"
404403 :current-path =" filePath ?? ''"
405- :base-url =" getCodeUrl ()"
406- :base-path = " basePath "
404+ :base-url =" getCurrentCodeUrlWithPath ()"
405+ :base-route = " route "
407406 />
408407 </aside >
409408
@@ -558,8 +557,8 @@ defineOgImageComponent('Default', {
558557 <CodeDirectoryListing
559558 :tree =" fileTree.tree"
560559 :current-path =" filePath ?? ''"
561- :base-url =" getCodeUrl ()"
562- :base-path = " basePath "
560+ :base-url =" getCurrentCodeUrlWithPath ()"
561+ :base-route = " route "
563562 />
564563 </template >
565564 </div >
@@ -572,8 +571,8 @@ defineOgImageComponent('Default', {
572571 v-if =" fileTree"
573572 :tree =" fileTree.tree"
574573 :current-path =" filePath ?? ''"
575- :base-url =" getCodeUrl ()"
576- :base-path = " basePath "
574+ :base-url =" getCurrentCodeUrlWithPath ()"
575+ :base-route = " route "
577576 />
578577 </Teleport >
579578 </ClientOnly >
0 commit comments