Skip to content

Commit

Permalink
fix: fix normalizePath and resolvePageRedirects
Browse files Browse the repository at this point in the history
  • Loading branch information
meteorlxy committed Feb 2, 2024
1 parent c5a61de commit 521139e
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 30 deletions.
26 changes: 15 additions & 11 deletions packages/core/src/app/prepare/preparePagesMap.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ensureLeadingSlash } from '@vuepress/shared'
import { ensureLeadingSlash, normalizePath } from '@vuepress/shared'
import type { App, Page } from '../../types/index.js'

const HMR_CODE = `
Expand Down Expand Up @@ -31,23 +31,27 @@ const resolvePageRedirects = ({
// paths that should redirect to this page, use set to dedupe
const redirectsSet = new Set<string>()

// redirect from decoded path
redirectsSet.add(decodeURI(path))
// add redirect to the set when the redirect could not be normalized & encoded to the page path
const addRedirect = (redirect: string): void => {
const normalizedPath = normalizePath(redirect)
if (normalizedPath === path) return

const encodedPath = encodeURI(normalizedPath)
if (encodedPath === path) return

redirectsSet.add(redirect)
}

// redirect from inferred path
if (pathInferred !== null) {
redirectsSet.add(pathInferred)
addRedirect(pathInferred)
}

// redirect from none-markdown filename path
// markdown file path is omitted as it can be normalized to pathInferred
if (filePathRelative !== null && !filePathRelative.endsWith('.md')) {
redirectsSet.add(ensureLeadingSlash(filePathRelative))
// redirect from filename path
if (filePathRelative !== null) {
addRedirect(ensureLeadingSlash(filePathRelative))
}

// avoid redirect from the page path itself
redirectsSet.delete(path)

return Array.from(redirectsSet)
}

Expand Down
2 changes: 1 addition & 1 deletion packages/shared/src/utils/normalizePath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export const normalizePath = (path: string): string => {
: path

return convertedMdPath.endsWith('/index.html')
? convertedMdPath.substring(0, path.length - 10)
? convertedMdPath.substring(0, convertedMdPath.length - 10)
: convertedMdPath.endsWith('.html') || convertedMdPath.endsWith('/')
? convertedMdPath
: convertedMdPath + '.html'
Expand Down
41 changes: 23 additions & 18 deletions packages/shared/tests/normalizePath.spec.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
import { expect, it } from 'vitest'
import { normalizePath } from '../src/index.js'

it('normalizePath', () => {
const tests = [
['/', '/'],
['/index.html', '/'],
['/foo', '/foo.html'],
['/foo.md', '/foo.html'],
['/foo/', '/foo/'],
['/foo/README.md', '/foo/'],
['/foo/index.html', '/foo/'],
['/foo/bar', '/foo/bar.html'],
['/foo/bar/', '/foo/bar/'],
['/foo/bar/README.md', '/foo/bar/'],
['/foo/bar.md', '/foo/bar.html'],
['/foo/bar.html', '/foo/bar.html'],
]
const testCases = [
['/', '/'],
['/README.md', '/'],
['/index.md', '/'],
['/index.html', '/'],
['/foo', '/foo.html'],
['/foo.md', '/foo.html'],
['/foo/', '/foo/'],
['/foo/README.md', '/foo/'],
['/foo/index.md', '/foo/'],
['/foo/index.html', '/foo/'],
['/foo/bar', '/foo/bar.html'],
['/foo/bar/', '/foo/bar/'],
['/foo/bar/README.md', '/foo/bar/'],
['/foo/bar/index.md', '/foo/bar/'],
['/foo/bar/index.html', '/foo/bar/'],
['/foo/bar.md', '/foo/bar.html'],
['/foo/bar.html', '/foo/bar.html'],
]

tests.forEach(([path, expected]) => {
testCases.forEach(([path, expected]) =>
it(`should normalize "${path}" to "${expected}"`, () => {
expect(normalizePath(path)).toBe(expected)
})
})
}),
)

0 comments on commit 521139e

Please sign in to comment.