Skip to content

Commit

Permalink
fix: fix permalink redirecting (close #1516) (#1517)
Browse files Browse the repository at this point in the history
  • Loading branch information
meteorlxy authored Mar 22, 2024
1 parent 010c8bf commit ab41ba1
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 14 deletions.
5 changes: 5 additions & 0 deletions e2e/docs/routes/permalinks/ascii-ascii.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
permalink: /permalink-ascii-ascii/
---

/permalink-ascii-ascii/
5 changes: 5 additions & 0 deletions e2e/docs/routes/permalinks/ascii-non-ascii.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
permalink: /永久链接-ascii-中文/
---

/永久链接-ascii-中文/
23 changes: 23 additions & 0 deletions e2e/docs/routes/permalinks/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
### ascii-ascii

- <a :href="$withBase('/permalink-ascii-ascii/')">permalink with base</a>
- [permalink](/permalink-ascii-ascii/)
- [filename](./ascii-ascii.md)

### ascii-non-ascii

- <a :href="$withBase('/永久链接-ascii-中文/')">permalink with base</a>
- [permalink](/永久链接-ascii-中文/)
- [filename](./ascii-non-ascii.md)

### 中文-ascii

- <a :href="$withBase('/permalink-non-ascii-ascii/')">permalink with base</a>
- [permalink](/permalink-non-ascii-ascii/)
- [filename](./中文-ascii.md)

### 中文-中文

- <a :href="$withBase('/永久链接-中文-中文/')">permalink with base</a>
- [permalink](/永久链接-中文-中文/)
- [filename](./中文-中文.md)
5 changes: 5 additions & 0 deletions e2e/docs/routes/permalinks/中文-ascii.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
permalink: /permalink-non-ascii-ascii/
---

/permalink-non-ascii-ascii/
5 changes: 5 additions & 0 deletions e2e/docs/routes/permalinks/中文-中文.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
permalink: /永久链接-中文-中文/
---

/永久链接-中文-中文/
76 changes: 76 additions & 0 deletions e2e/tests/routes/permalinks.cy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
const PERMALINK_PREFIX = Cypress.env('E2E_BASE').replace(/\/$/, '')

const CONFIGS = [
{
id: 'ascii-ascii',
filename: 'ascii-ascii.md',
permalink: '/permalink-ascii-ascii/',
},
{
id: 'ascii-non-ascii',
filename: 'ascii-non-ascii.md',
permalink: '/永久链接-ascii-中文/',
},
{
id: '中文-ascii',
filename: '中文-ascii.md',
permalink: '/permalink-non-ascii-ascii/',
},
{
id: '中文-中文',
filename: '中文-中文.md',
permalink: '/永久链接-中文-中文/',
},
]

it('should support visiting permalinks directly', () => {
for (const { permalink } of CONFIGS) {
cy.visit(encodeURI(permalink))
cy.get('.e2e-theme-content p').should('have.text', permalink)
}
})

it('should support rendering link by permalink and navigate to it correctly', () => {
for (const { id, permalink } of CONFIGS) {
cy.visit('/routes/permalinks/')

// absolute link that does not end with '.md' will not be prepended with `base`
cy.get(`.e2e-theme-content #${id} + ul > li a`)
.should('have.length', 3)
.eq(1)
.should('have.attr', 'href', encodeURI(permalink))

// `withBase` won't encode the url
cy.get(`.e2e-theme-content #${id} + ul > li a`)
.should('have.length', 3)
.eq(0)
.should('have.attr', 'href', `${PERMALINK_PREFIX}${permalink}`)
.click()

cy.location('pathname').should(
'eq',
encodeURI(`${PERMALINK_PREFIX}${permalink}`),
)

cy.get('.e2e-theme-content p').should('have.text', permalink)
}
})

it('should support rendering link by filename and navigate to it correctly', () => {
for (const { id, permalink } of CONFIGS) {
cy.visit('/routes/permalinks/')

cy.get(`.e2e-theme-content #${id} + ul > li a`)
.should('have.length', 3)
.eq(2)
.should('have.attr', 'href', encodeURI(`${PERMALINK_PREFIX}${permalink}`))
.click()

cy.location('pathname').should(
'eq',
encodeURI(`${PERMALINK_PREFIX}${permalink}`),
)

cy.get('.e2e-theme-content p').should('have.text', permalink)
}
})
6 changes: 5 additions & 1 deletion packages/client/src/router/resolveRoutePath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,9 @@ export const resolveRoutePath = (path: string): string => {
if (routes.value[encodedPath]) return encodedPath

// redirected path or fallback to the normalized path
return redirects.value[normalizedPath] || normalizedPath
return (
redirects.value[normalizedPath] ||
redirects.value[encodedPath] ||
normalizedPath
)
}
17 changes: 4 additions & 13 deletions packages/core/src/app/prepare/prepareRoutes.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ensureLeadingSlash, normalizeRoutePath } from '@vuepress/shared'
import { normalizeRoutePath } from '@vuepress/shared'
import type { App, Page } from '../../types/index.js'

const HMR_CODE = `
Expand All @@ -23,11 +23,7 @@ if (import.meta.hot) {
/**
* Resolve page redirects
*/
const resolvePageRedirects = ({
path,
pathInferred,
filePathRelative,
}: Page): string[] => {
const resolvePageRedirects = ({ path, pathInferred }: Page): string[] => {
// paths that should redirect to this page, use set to dedupe
const redirectsSet = new Set<string>()

Expand All @@ -42,14 +38,9 @@ const resolvePageRedirects = ({
redirectsSet.add(redirect)
}

// redirect from inferred path
// redirect from inferred path, notice that the inferred path is not uri-encoded
if (pathInferred !== null) {
addRedirect(pathInferred)
}

// redirect from filename path
if (filePathRelative !== null) {
addRedirect(ensureLeadingSlash(filePathRelative))
addRedirect(encodeURI(pathInferred))
}

return Array.from(redirectsSet)
Expand Down

0 comments on commit ab41ba1

Please sign in to comment.