Skip to content

Commit c1dbc2c

Browse files
committedFeb 5, 2024
fix(shared): handle query and hash in normalizeRoutePath
1 parent 47b6d64 commit c1dbc2c

File tree

2 files changed

+49
-14
lines changed

2 files changed

+49
-14
lines changed
 

‎packages/shared/src/utils/normalizeRoutePath.ts

+12-8
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,31 @@
22
* Normalize the given path to the final route path
33
*/
44
export const normalizeRoutePath = (path: string): string => {
5-
if (!path || path.endsWith('/')) {
6-
return path
7-
}
5+
// split pathname and query/hash
6+
const [pathname, ...rest] = path.split(/(\?|#)/)
7+
8+
// if the pathname is empty or ends with `/`, return as is
9+
if (!pathname || pathname.endsWith('/')) return path
10+
11+
// join query and hash
12+
const queryAndHash = rest.length > 0 ? rest.join('') : ''
813

914
// convert README.md to index.html
10-
let routePath = path.replace(/(^|\/)README.md$/i, '$1index.html')
15+
let routePath = pathname.replace(/(^|\/)README.md$/i, '$1index.html')
1116

1217
// convert /foo/bar.md to /foo/bar.html
1318
if (routePath.endsWith('.md')) {
1419
routePath = routePath.substring(0, routePath.length - 3) + '.html'
1520
}
16-
1721
// convert /foo/bar to /foo/bar.html
18-
if (!routePath.endsWith('.html')) {
22+
else if (!routePath.endsWith('.html')) {
1923
routePath = routePath + '.html'
2024
}
2125

2226
// convert /foo/index.html to /foo/
2327
if (routePath.endsWith('/index.html')) {
24-
return routePath.substring(0, routePath.length - 10)
28+
return routePath.substring(0, routePath.length - 10) + queryAndHash
2529
}
2630

27-
return routePath
31+
return routePath + queryAndHash
2832
}

‎packages/shared/tests/normalizeRoutePath.spec.ts

+37-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { expect, it } from 'vitest'
1+
import { describe, expect, it } from 'vitest'
22
import { normalizeRoutePath } from '../src/index.js'
33

44
const testCases = [
@@ -49,8 +49,39 @@ const testCases = [
4949
['/foo/.md', '/foo/.html'],
5050
]
5151

52-
testCases.forEach(([path, expected]) =>
53-
it(`should normalize "${path}" to "${expected}"`, () => {
54-
expect(normalizeRoutePath(path)).toBe(expected)
55-
}),
56-
)
52+
describe('should normalize clean paths correctly', () =>
53+
testCases.forEach(([path, expected]) =>
54+
it(`"${path}" -> "${expected}"`, () => {
55+
expect(normalizeRoutePath(path)).toBe(expected)
56+
}),
57+
))
58+
59+
describe('should normalize paths with query correctly', () =>
60+
testCases
61+
.map(([path, expected]) => [`${path}?foo=bar`, `${expected}?foo=bar`])
62+
.forEach(([path, expected]) =>
63+
it(`"${path}" -> "${expected}"`, () => {
64+
expect(normalizeRoutePath(path)).toBe(expected)
65+
}),
66+
))
67+
68+
describe('should normalize paths with hash correctly', () =>
69+
testCases
70+
.map(([path, expected]) => [`${path}#foobar`, `${expected}#foobar`])
71+
.forEach(([path, expected]) =>
72+
it(`"${path}" -> "${expected}"`, () => {
73+
expect(normalizeRoutePath(path)).toBe(expected)
74+
}),
75+
))
76+
77+
describe('should normalize paths with query and hash correctly', () =>
78+
testCases
79+
.map(([path, expected]) => [
80+
`${path}?foo=1&bar=2#foobar`,
81+
`${expected}?foo=1&bar=2#foobar`,
82+
])
83+
.forEach(([path, expected]) =>
84+
it(`"${path}" -> "${expected}"`, () => {
85+
expect(normalizeRoutePath(path)).toBe(expected)
86+
}),
87+
))

0 commit comments

Comments
 (0)
Please sign in to comment.