File tree Expand file tree Collapse file tree 5 files changed +39
-8
lines changed Expand file tree Collapse file tree 5 files changed +39
-8
lines changed Original file line number Diff line number Diff line change 1+ import { resolveRoutePathname } from '@vuepress/shared'
12import { routes } from '../internal/routes.js'
23import type { Route , RouteMeta } from '../types/index.js'
34import { resolveRoutePath } from './resolveRoutePath.js'
@@ -16,7 +17,7 @@ export const resolveRoute = <T extends RouteMeta = RouteMeta>(
1617 currentPath ?: string ,
1718) : ResolvedRoute < T > => {
1819 const routePath = resolveRoutePath ( path , currentPath )
19- const pathname = routePath . split ( / # | \? / ) [ 0 ]
20+ const pathname = resolveRoutePathname ( routePath )
2021 const route = routes . value [ pathname ] ?? {
2122 ...routes . value [ '/404.html' ] ,
2223 notFound : true ,
Original file line number Diff line number Diff line change 1- import { normalizeRoutePath } from '@vuepress/shared'
1+ import { normalizeRoutePath , resolveRoutePathname } from '@vuepress/shared'
22import { redirects , routes } from '../internal/routes.js'
33
44/**
@@ -10,16 +10,16 @@ export const resolveRoutePath = (
1010) : string => {
1111 // normalized path
1212 const normalizedPath = normalizeRoutePath ( path , currentPath )
13- if ( routes . value [ normalizedPath ] ) return normalizedPath
14-
13+ const normalizedPathname = resolveRoutePathname ( normalizedPath )
14+ if ( routes . value [ normalizedPathname ] ) return normalizedPath
1515 // encoded path
16- const encodedPath = encodeURI ( normalizedPath )
17- if ( routes . value [ encodedPath ] ) return encodedPath
16+ const encodedPathname = encodeURI ( normalizedPathname )
17+ if ( routes . value [ encodedPathname ] ) return encodeURI ( normalizedPath )
1818
1919 // redirected path or fallback to the normalized path
2020 return (
21- redirects . value [ normalizedPath ] ||
22- redirects . value [ encodedPath ] ||
21+ redirects . value [ normalizedPathname ] ||
22+ redirects . value [ encodedPathname ] ||
2323 normalizedPath
2424 )
2525}
Original file line number Diff line number Diff line change @@ -15,4 +15,5 @@ export * from './removeLeadingSlash.js'
1515export * from './resolveHeadIdentifier.js'
1616export * from './resolveLocalePath.js'
1717export * from './resolveRoutePathFromUrl.js'
18+ export * from './resolveRoutePathname.js'
1819export * from './typeGuards.js'
Original file line number Diff line number Diff line change 1+ const SPLIT_CHAR_RE = / # | \? /
2+
3+ /**
4+ * Resolve the route pathname
5+ */
6+ export const resolveRoutePathname = ( path : string ) : string => {
7+ return path . split ( SPLIT_CHAR_RE ) [ 0 ]
8+ }
Original file line number Diff line number Diff line change 1+ import { describe , expect , it } from 'vitest'
2+ import { resolveRoutePathname } from '../src/index.js'
3+
4+ const testCases = [
5+ [ '/a/b/c/' , '/a/b/c/' ] ,
6+ [ '/a/b/c/?a=1' , '/a/b/c/' ] ,
7+ [ '/a/b/c/#b' , '/a/b/c/' ] ,
8+ [ '/a/b/c/?a=1#b' , '/a/b/c/' ] ,
9+ [ 'a/index.html' , 'a/index.html' ] ,
10+ [ '/a/index.html?a=1' , '/a/index.html' ] ,
11+ [ '/a/index.html#a' , '/a/index.html' ] ,
12+ [ '/a/index.html?a=1#b' , '/a/index.html' ] ,
13+ ]
14+
15+ describe ( 'should resolve route pathname correctly' , ( ) => {
16+ testCases . forEach ( ( [ source , expected ] ) => {
17+ it ( `${ source } -> ${ expected } ` , ( ) => {
18+ expect ( resolveRoutePathname ( source ) ) . toBe ( expected )
19+ } )
20+ } )
21+ } )
You can’t perform that action at this time.
0 commit comments