-
Notifications
You must be signed in to change notification settings - Fork 920
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: unify api name and avoid unnecessary recalculation
- Loading branch information
1 parent
abd8dbd
commit edd2fc1
Showing
11 changed files
with
78 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { resolveRoutePathInfo } from '@vuepress/shared' | ||
import { resolveRoutePath } from './resolveRoutePath.js' | ||
|
||
/** | ||
* Resolve route full path with given raw path | ||
*/ | ||
export const resolveRouteFullPath = ( | ||
path: string, | ||
currentPath?: string, | ||
): string => { | ||
const [pathname, hashAndQueries] = resolveRoutePathInfo(path) | ||
|
||
return resolveRoutePath(pathname, currentPath) + hashAndQueries | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,34 @@ | ||
import { normalizeRoutePath, resolveRouteFullPath } from '@vuepress/shared' | ||
import { normalizeRoutePath } from '@vuepress/shared' | ||
import { redirects, routes } from '../internal/routes.js' | ||
|
||
/** | ||
* Resolve route path with given raw path | ||
*/ | ||
export const resolveRoutePath = ( | ||
path: string, | ||
pathname: string, | ||
currentPath?: string, | ||
): string => { | ||
// normalized path | ||
const normalizedPath = normalizeRoutePath(path, currentPath) | ||
const [normalizedPathname, queryAndHash] = | ||
resolveRouteFullPath(normalizedPath) | ||
const normalizedRoutePath = normalizeRoutePath(pathname, currentPath) | ||
|
||
if (routes.value[normalizedPathname]) return normalizedPath | ||
// check if the normalized path is in routes | ||
if (routes.value[normalizedRoutePath]) return normalizedRoutePath | ||
|
||
// encoded path | ||
const encodedPathname = encodeURI(normalizedPathname) | ||
if (routes.value[encodedPathname]) return encodeURI(normalizedPath) | ||
// check encoded path | ||
const encodedRoutePath = encodeURI(normalizedRoutePath) | ||
|
||
// redirected path | ||
const redirectPath = | ||
redirects.value[normalizedPathname] || redirects.value[encodedPathname] | ||
if (redirectPath) return redirectPath + queryAndHash | ||
if (routes.value[encodedRoutePath]) { | ||
return encodedRoutePath | ||
} | ||
|
||
// fallback to the normalized path | ||
return normalizedPath | ||
// check redirected path with normalized path and encoded path | ||
const redirectedRoutePath = | ||
redirects.value[normalizedRoutePath] || redirects.value[encodedRoutePath] | ||
|
||
if (redirectedRoutePath) { | ||
return redirectedRoutePath | ||
} | ||
|
||
// default to normalized route path | ||
return normalizedRoutePath | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,20 @@ | ||
import { inferRoutePath } from './inferRoutePath.js' | ||
import { resolveRouteFullPath } from './resolveRouteFullPath.js' | ||
|
||
const FAKE_HOST = 'http://.' | ||
|
||
/** | ||
* Normalize the given path to the final route path | ||
* Normalize the given pathname path to the final route path | ||
*/ | ||
export const normalizeRoutePath = (path: string, current?: string): string => { | ||
if (!path.startsWith('/') && current) { | ||
export const normalizeRoutePath = ( | ||
pathname: string, | ||
current?: string, | ||
): string => { | ||
if (!pathname.startsWith('/') && current) { | ||
// the relative path should be resolved against the current path | ||
const loc = current.slice(0, current.lastIndexOf('/')) | ||
|
||
const { pathname, search, hash } = new URL(`${loc}/${path}`, FAKE_HOST) | ||
|
||
return inferRoutePath(pathname) + search + hash | ||
return inferRoutePath(new URL(`${loc}/${pathname}`, FAKE_HOST).pathname) | ||
} | ||
|
||
const [pathname, queryAndHash] = resolveRouteFullPath(path) | ||
|
||
return inferRoutePath(pathname) + queryAndHash | ||
return inferRoutePath(pathname) | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
const SPLIT_CHAR_REGEXP = /(#|\?)/ | ||
|
||
/** | ||
* Extract pathname / hash and queries from a full route path | ||
*/ | ||
export const resolveRoutePathInfo = ( | ||
path: string, | ||
): [pathname: string, hashAndQueries: string] => { | ||
const [pathname, ...hashAndQueries] = path.split(SPLIT_CHAR_REGEXP) | ||
|
||
return [pathname, hashAndQueries.join('')] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters