Skip to content

Commit fa7b93b

Browse files
committed
chore: tweaks
1 parent 46bd7ba commit fa7b93b

File tree

7 files changed

+44
-39
lines changed

7 files changed

+44
-39
lines changed

packages/client/src/router/resolveRoute.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { resolvePathInfo } from '@vuepress/shared'
1+
import { splitPath } from '@vuepress/shared'
22
import { routes } from '../internal/routes.js'
33
import type { Route, RouteMeta } from '../types/index.js'
44
import { resolveRoutePath } from './resolveRoutePath.js'
@@ -17,7 +17,7 @@ export const resolveRoute = <T extends RouteMeta = RouteMeta>(
1717
currentPath?: string,
1818
): ResolvedRoute<T> => {
1919
// get only the pathname from the path
20-
const [pathname, hashAndQueries] = resolvePathInfo(path)
20+
const { pathname, hashAndQueries } = splitPath(path)
2121

2222
// resolve the route path
2323
const routePath = resolveRoutePath(pathname, currentPath)
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { resolvePathInfo } from '@vuepress/shared'
1+
import { splitPath } from '@vuepress/shared'
22
import { resolveRoutePath } from './resolveRoutePath.js'
33

44
/**
@@ -8,7 +8,6 @@ export const resolveRouteFullPath = (
88
path: string,
99
currentPath?: string,
1010
): string => {
11-
const [pathname, hashAndQueries] = resolvePathInfo(path)
12-
11+
const { pathname, hashAndQueries } = splitPath(path)
1312
return resolveRoutePath(pathname, currentPath) + hashAndQueries
1413
}

packages/shared/src/utils/routes/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ export * from './inferRoutePath'
22
export * from './normalizeRoutePath.js'
33
export * from './resolveLocalePath.js'
44
export * from './resolveRoutePathFromUrl.js'
5-
export * from './resolvePathInfo.js'
5+
export * from './splitPath.js'

packages/shared/src/utils/routes/resolvePathInfo.ts

Lines changed: 0 additions & 12 deletions
This file was deleted.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const SPLIT_CHAR_REGEXP = /(#|\?)/
2+
3+
/**
4+
* Split a path into pathname and hashAndQueries
5+
*/
6+
export const splitPath = (
7+
path: string,
8+
): {
9+
pathname: string
10+
hashAndQueries: string
11+
} => {
12+
const [pathname, ...hashAndQueries] = path.split(SPLIT_CHAR_REGEXP)
13+
return {
14+
pathname,
15+
hashAndQueries: hashAndQueries.join(''),
16+
}
17+
}

packages/shared/tests/routes/resolvePathInfo.spec.ts

Lines changed: 0 additions & 21 deletions
This file was deleted.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { expect, it } from 'vitest'
2+
import { splitPath } from '../../src/index.js'
3+
4+
const testCases: [string, ReturnType<typeof splitPath>][] = [
5+
['/a/b/c/', { pathname: '/a/b/c/', hashAndQueries: '' }],
6+
['/a/b/c/?a=1', { pathname: '/a/b/c/', hashAndQueries: '?a=1' }],
7+
['/a/b/c/#b', { pathname: '/a/b/c/', hashAndQueries: '#b' }],
8+
['/a/b/c/?a=1#b', { pathname: '/a/b/c/', hashAndQueries: '?a=1#b' }],
9+
['a/index.html', { pathname: 'a/index.html', hashAndQueries: '' }],
10+
['/a/index.html?a=1', { pathname: '/a/index.html', hashAndQueries: '?a=1' }],
11+
['/a/index.html#a', { pathname: '/a/index.html', hashAndQueries: '#a' }],
12+
[
13+
'/a/index.html?a=1#b',
14+
{ pathname: '/a/index.html', hashAndQueries: '?a=1#b' },
15+
],
16+
]
17+
18+
testCases.forEach(([source, expected]) => {
19+
it(`${source} -> ${expected}`, () => {
20+
expect(splitPath(source)).toEqual(expected)
21+
})
22+
})

0 commit comments

Comments
 (0)