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'
1
2
import { routes } from '../internal/routes.js'
2
3
import type { Route , RouteMeta } from '../types/index.js'
3
4
import { resolveRoutePath } from './resolveRoutePath.js'
@@ -16,7 +17,7 @@ export const resolveRoute = <T extends RouteMeta = RouteMeta>(
16
17
currentPath ?: string ,
17
18
) : ResolvedRoute < T > => {
18
19
const routePath = resolveRoutePath ( path , currentPath )
19
- const pathname = routePath . split ( / # | \? / ) [ 0 ]
20
+ const pathname = resolveRoutePathname ( routePath )
20
21
const route = routes . value [ pathname ] ?? {
21
22
...routes . value [ '/404.html' ] ,
22
23
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'
2
2
import { redirects , routes } from '../internal/routes.js'
3
3
4
4
/**
@@ -10,16 +10,16 @@ export const resolveRoutePath = (
10
10
) : string => {
11
11
// normalized path
12
12
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
15
15
// 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 )
18
18
19
19
// redirected path or fallback to the normalized path
20
20
return (
21
- redirects . value [ normalizedPath ] ||
22
- redirects . value [ encodedPath ] ||
21
+ redirects . value [ normalizedPathname ] ||
22
+ redirects . value [ encodedPathname ] ||
23
23
normalizedPath
24
24
)
25
25
}
Original file line number Diff line number Diff line change @@ -15,4 +15,5 @@ export * from './removeLeadingSlash.js'
15
15
export * from './resolveHeadIdentifier.js'
16
16
export * from './resolveLocalePath.js'
17
17
export * from './resolveRoutePathFromUrl.js'
18
+ export * from './resolveRoutePathname.js'
18
19
export * 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