Skip to content

Commit ab41ba1

Browse files
authored
fix: fix permalink redirecting (close #1516) (#1517)
1 parent 010c8bf commit ab41ba1

File tree

8 files changed

+128
-14
lines changed

8 files changed

+128
-14
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
permalink: /permalink-ascii-ascii/
3+
---
4+
5+
/permalink-ascii-ascii/
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
permalink: /永久链接-ascii-中文/
3+
---
4+
5+
/永久链接-ascii-中文/

e2e/docs/routes/permalinks/index.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
### ascii-ascii
2+
3+
- <a :href="$withBase('/permalink-ascii-ascii/')">permalink with base</a>
4+
- [permalink](/permalink-ascii-ascii/)
5+
- [filename](./ascii-ascii.md)
6+
7+
### ascii-non-ascii
8+
9+
- <a :href="$withBase('/永久链接-ascii-中文/')">permalink with base</a>
10+
- [permalink](/永久链接-ascii-中文/)
11+
- [filename](./ascii-non-ascii.md)
12+
13+
### 中文-ascii
14+
15+
- <a :href="$withBase('/permalink-non-ascii-ascii/')">permalink with base</a>
16+
- [permalink](/permalink-non-ascii-ascii/)
17+
- [filename](./中文-ascii.md)
18+
19+
### 中文-中文
20+
21+
- <a :href="$withBase('/永久链接-中文-中文/')">permalink with base</a>
22+
- [permalink](/永久链接-中文-中文/)
23+
- [filename](./中文-中文.md)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
permalink: /permalink-non-ascii-ascii/
3+
---
4+
5+
/permalink-non-ascii-ascii/
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
permalink: /永久链接-中文-中文/
3+
---
4+
5+
/永久链接-中文-中文/

e2e/tests/routes/permalinks.cy.ts

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
const PERMALINK_PREFIX = Cypress.env('E2E_BASE').replace(/\/$/, '')
2+
3+
const CONFIGS = [
4+
{
5+
id: 'ascii-ascii',
6+
filename: 'ascii-ascii.md',
7+
permalink: '/permalink-ascii-ascii/',
8+
},
9+
{
10+
id: 'ascii-non-ascii',
11+
filename: 'ascii-non-ascii.md',
12+
permalink: '/永久链接-ascii-中文/',
13+
},
14+
{
15+
id: '中文-ascii',
16+
filename: '中文-ascii.md',
17+
permalink: '/permalink-non-ascii-ascii/',
18+
},
19+
{
20+
id: '中文-中文',
21+
filename: '中文-中文.md',
22+
permalink: '/永久链接-中文-中文/',
23+
},
24+
]
25+
26+
it('should support visiting permalinks directly', () => {
27+
for (const { permalink } of CONFIGS) {
28+
cy.visit(encodeURI(permalink))
29+
cy.get('.e2e-theme-content p').should('have.text', permalink)
30+
}
31+
})
32+
33+
it('should support rendering link by permalink and navigate to it correctly', () => {
34+
for (const { id, permalink } of CONFIGS) {
35+
cy.visit('/routes/permalinks/')
36+
37+
// absolute link that does not end with '.md' will not be prepended with `base`
38+
cy.get(`.e2e-theme-content #${id} + ul > li a`)
39+
.should('have.length', 3)
40+
.eq(1)
41+
.should('have.attr', 'href', encodeURI(permalink))
42+
43+
// `withBase` won't encode the url
44+
cy.get(`.e2e-theme-content #${id} + ul > li a`)
45+
.should('have.length', 3)
46+
.eq(0)
47+
.should('have.attr', 'href', `${PERMALINK_PREFIX}${permalink}`)
48+
.click()
49+
50+
cy.location('pathname').should(
51+
'eq',
52+
encodeURI(`${PERMALINK_PREFIX}${permalink}`),
53+
)
54+
55+
cy.get('.e2e-theme-content p').should('have.text', permalink)
56+
}
57+
})
58+
59+
it('should support rendering link by filename and navigate to it correctly', () => {
60+
for (const { id, permalink } of CONFIGS) {
61+
cy.visit('/routes/permalinks/')
62+
63+
cy.get(`.e2e-theme-content #${id} + ul > li a`)
64+
.should('have.length', 3)
65+
.eq(2)
66+
.should('have.attr', 'href', encodeURI(`${PERMALINK_PREFIX}${permalink}`))
67+
.click()
68+
69+
cy.location('pathname').should(
70+
'eq',
71+
encodeURI(`${PERMALINK_PREFIX}${permalink}`),
72+
)
73+
74+
cy.get('.e2e-theme-content p').should('have.text', permalink)
75+
}
76+
})

packages/client/src/router/resolveRoutePath.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,9 @@ export const resolveRoutePath = (path: string): string => {
1414
if (routes.value[encodedPath]) return encodedPath
1515

1616
// redirected path or fallback to the normalized path
17-
return redirects.value[normalizedPath] || normalizedPath
17+
return (
18+
redirects.value[normalizedPath] ||
19+
redirects.value[encodedPath] ||
20+
normalizedPath
21+
)
1822
}

packages/core/src/app/prepare/prepareRoutes.ts

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ensureLeadingSlash, normalizeRoutePath } from '@vuepress/shared'
1+
import { normalizeRoutePath } from '@vuepress/shared'
22
import type { App, Page } from '../../types/index.js'
33

44
const HMR_CODE = `
@@ -23,11 +23,7 @@ if (import.meta.hot) {
2323
/**
2424
* Resolve page redirects
2525
*/
26-
const resolvePageRedirects = ({
27-
path,
28-
pathInferred,
29-
filePathRelative,
30-
}: Page): string[] => {
26+
const resolvePageRedirects = ({ path, pathInferred }: Page): string[] => {
3127
// paths that should redirect to this page, use set to dedupe
3228
const redirectsSet = new Set<string>()
3329

@@ -42,14 +38,9 @@ const resolvePageRedirects = ({
4238
redirectsSet.add(redirect)
4339
}
4440

45-
// redirect from inferred path
41+
// redirect from inferred path, notice that the inferred path is not uri-encoded
4642
if (pathInferred !== null) {
47-
addRedirect(pathInferred)
48-
}
49-
50-
// redirect from filename path
51-
if (filePathRelative !== null) {
52-
addRedirect(ensureLeadingSlash(filePathRelative))
43+
addRedirect(encodeURI(pathInferred))
5344
}
5445

5546
return Array.from(redirectsSet)

0 commit comments

Comments
 (0)