Skip to content

Commit

Permalink
chore: tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
meteorlxy committed May 23, 2024
1 parent 46bd7ba commit fa7b93b
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 39 deletions.
4 changes: 2 additions & 2 deletions packages/client/src/router/resolveRoute.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { resolvePathInfo } from '@vuepress/shared'
import { splitPath } from '@vuepress/shared'
import { routes } from '../internal/routes.js'
import type { Route, RouteMeta } from '../types/index.js'
import { resolveRoutePath } from './resolveRoutePath.js'
Expand All @@ -17,7 +17,7 @@ export const resolveRoute = <T extends RouteMeta = RouteMeta>(
currentPath?: string,
): ResolvedRoute<T> => {
// get only the pathname from the path
const [pathname, hashAndQueries] = resolvePathInfo(path)
const { pathname, hashAndQueries } = splitPath(path)

// resolve the route path
const routePath = resolveRoutePath(pathname, currentPath)
Expand Down
5 changes: 2 additions & 3 deletions packages/client/src/router/resolveRouteFullPath.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { resolvePathInfo } from '@vuepress/shared'
import { splitPath } from '@vuepress/shared'
import { resolveRoutePath } from './resolveRoutePath.js'

/**
Expand All @@ -8,7 +8,6 @@ export const resolveRouteFullPath = (
path: string,
currentPath?: string,
): string => {
const [pathname, hashAndQueries] = resolvePathInfo(path)

const { pathname, hashAndQueries } = splitPath(path)
return resolveRoutePath(pathname, currentPath) + hashAndQueries
}
2 changes: 1 addition & 1 deletion packages/shared/src/utils/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ export * from './inferRoutePath'
export * from './normalizeRoutePath.js'
export * from './resolveLocalePath.js'
export * from './resolveRoutePathFromUrl.js'
export * from './resolvePathInfo.js'
export * from './splitPath.js'
12 changes: 0 additions & 12 deletions packages/shared/src/utils/routes/resolvePathInfo.ts

This file was deleted.

17 changes: 17 additions & 0 deletions packages/shared/src/utils/routes/splitPath.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const SPLIT_CHAR_REGEXP = /(#|\?)/

/**
* Split a path into pathname and hashAndQueries
*/
export const splitPath = (
path: string,
): {
pathname: string
hashAndQueries: string
} => {
const [pathname, ...hashAndQueries] = path.split(SPLIT_CHAR_REGEXP)
return {
pathname,
hashAndQueries: hashAndQueries.join(''),
}
}
21 changes: 0 additions & 21 deletions packages/shared/tests/routes/resolvePathInfo.spec.ts

This file was deleted.

22 changes: 22 additions & 0 deletions packages/shared/tests/routes/splitPath.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { expect, it } from 'vitest'
import { splitPath } from '../../src/index.js'

const testCases: [string, ReturnType<typeof splitPath>][] = [
['/a/b/c/', { pathname: '/a/b/c/', hashAndQueries: '' }],
['/a/b/c/?a=1', { pathname: '/a/b/c/', hashAndQueries: '?a=1' }],
['/a/b/c/#b', { pathname: '/a/b/c/', hashAndQueries: '#b' }],
['/a/b/c/?a=1#b', { pathname: '/a/b/c/', hashAndQueries: '?a=1#b' }],
['a/index.html', { pathname: 'a/index.html', hashAndQueries: '' }],
['/a/index.html?a=1', { pathname: '/a/index.html', hashAndQueries: '?a=1' }],
['/a/index.html#a', { pathname: '/a/index.html', hashAndQueries: '#a' }],
[
'/a/index.html?a=1#b',
{ pathname: '/a/index.html', hashAndQueries: '?a=1#b' },
],
]

testCases.forEach(([source, expected]) => {
it(`${source} -> ${expected}`, () => {
expect(splitPath(source)).toEqual(expected)
})
})

0 comments on commit fa7b93b

Please sign in to comment.