-
Notifications
You must be signed in to change notification settings - Fork 924
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'resolve-route' into route-option
- Loading branch information
Showing
12 changed files
with
152 additions
and
30 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# Resolve Route FullPath | ||
|
||
## Includes Query And Hash | ||
|
||
- Search Query: {{ JSON.stringify(resolveRoute('/?query=1')) }} | ||
- Hash: {{ JSON.stringify(resolveRoute('/#hash')) }} | ||
- Search Query And Hash: {{ JSON.stringify(resolveRoute('/?query=1#hash')) }} | ||
- Permalink And Search Query: {{ JSON.stringify(resolveRoute('/routes/permalinks/ascii-non-ascii.md?query=1')) }} | ||
|
||
<script setup> | ||
import { resolveRoute } from 'vuepress/client' | ||
</script> |
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,35 @@ | ||
import { expect, test } from '@playwright/test' | ||
|
||
const testCases = [ | ||
{ | ||
path: '/?query=1', | ||
notFound: false, | ||
}, | ||
{ | ||
path: '/#hash', | ||
notFound: false, | ||
}, | ||
{ | ||
path: '/?query=1#hash', | ||
notFound: false, | ||
}, | ||
{ | ||
path: encodeURI('/永久链接-ascii-中文/?query=1'), | ||
notFound: false, | ||
}, | ||
] | ||
|
||
test('should resolve routes when including both the query and hash', async ({ | ||
page, | ||
}) => { | ||
const listItemsLocator = await page | ||
.locator('.e2e-theme-content #includes-query-and-hash + ul > li') | ||
.all() | ||
|
||
for (const [index, li] of listItemsLocator.entries()) { | ||
const textContent = await li.textContent() | ||
const resolvedRoute = JSON.parse(/: (\{.*\})\s*$/.exec(textContent!)![1]) | ||
expect(resolvedRoute.path).toEqual(testCases[index].path) | ||
expect(resolvedRoute.notFound).toEqual(testCases[index].notFound) | ||
} | ||
}) |
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
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,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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { describe, expect, it } from 'vitest' | ||
import { resolveRoutePathInfo } from '../../src/index.js' | ||
|
||
const testCases: [string, [string, string]][] = [ | ||
['/a/b/c/', ['/a/b/c/', '']], | ||
['/a/b/c/?a=1', ['/a/b/c/', '?a=1']], | ||
['/a/b/c/#b', ['/a/b/c/', '#b']], | ||
['/a/b/c/?a=1#b', ['/a/b/c/', '?a=1#b']], | ||
['a/index.html', ['a/index.html', '']], | ||
['/a/index.html?a=1', ['/a/index.html', '?a=1']], | ||
['/a/index.html#a', ['/a/index.html', '#a']], | ||
['/a/index.html?a=1#b', ['/a/index.html', '?a=1#b']], | ||
] | ||
|
||
describe('should resolve route path info correctly', () => { | ||
testCases.forEach(([source, expected]) => { | ||
it(`${source} -> ${expected}`, () => { | ||
expect(resolveRoutePathInfo(source)).toEqual(expected) | ||
}) | ||
}) | ||
}) |