Skip to content

Commit f2247bb

Browse files
committedFeb 4, 2024
fix(shared): improve normalizeRoutePath edge cases handling
1 parent bfadfcf commit f2247bb

File tree

2 files changed

+56
-16
lines changed

2 files changed

+56
-16
lines changed
 

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

+22-10
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,27 @@
22
* Normalize the given path to the final route path
33
*/
44
export const normalizeRoutePath = (path: string): string => {
5-
const convertedMdPath = path.endsWith('README.md')
6-
? path.substring(0, path.length - 9)
7-
: path.endsWith('.md')
8-
? path.substring(0, path.length - 3) + '.html'
9-
: path
5+
if (!path || path.endsWith('/')) {
6+
return path
7+
}
108

11-
return convertedMdPath.endsWith('/index.html')
12-
? convertedMdPath.substring(0, convertedMdPath.length - 10)
13-
: convertedMdPath.endsWith('.html') || convertedMdPath.endsWith('/')
14-
? convertedMdPath
15-
: convertedMdPath + '.html'
9+
// convert README.md to index.html
10+
let routePath = path.replace(/(^|\/)README.md$/i, '$1index.html')
11+
12+
// convert /foo/bar.md to /foo/bar.html
13+
if (routePath.endsWith('.md')) {
14+
routePath = routePath.substring(0, routePath.length - 3) + '.html'
15+
}
16+
17+
// convert /foo/bar to /foo/bar.html
18+
if (!routePath.endsWith('.html')) {
19+
routePath = routePath + '.html'
20+
}
21+
22+
// convert /foo/index.html to /foo/
23+
if (routePath.endsWith('/index.html')) {
24+
return routePath.substring(0, routePath.length - 10)
25+
}
26+
27+
return routePath
1628
}

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

+34-6
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,51 @@ import { expect, it } from 'vitest'
22
import { normalizeRoutePath } from '../src/index.js'
33

44
const testCases = [
5+
// index
56
['/', '/'],
67
['/README.md', '/'],
8+
['/readme.md', '/'],
79
['/index.md', '/'],
810
['/index.html', '/'],
9-
['/foo', '/foo.html'],
10-
['/foo.md', '/foo.html'],
11+
['/index', '/'],
1112
['/foo/', '/foo/'],
1213
['/foo/README.md', '/foo/'],
14+
['/foo/readme.md', '/foo/'],
1315
['/foo/index.md', '/foo/'],
1416
['/foo/index.html', '/foo/'],
17+
['/foo/index', '/foo/'],
18+
['', ''],
19+
['README.md', 'index.html'],
20+
['readme.md', 'index.html'],
21+
['index.md', 'index.html'],
22+
['index.html', 'index.html'],
23+
['index', 'index.html'],
24+
['foo/', 'foo/'],
25+
['foo/README.md', 'foo/'],
26+
['foo/readme.md', 'foo/'],
27+
['foo/index.md', 'foo/'],
28+
['foo/index.html', 'foo/'],
29+
['foo/index', 'foo/'],
30+
31+
// non-index
32+
['/foo', '/foo.html'],
33+
['/foo.md', '/foo.html'],
34+
['/foo.html', '/foo.html'],
1535
['/foo/bar', '/foo/bar.html'],
16-
['/foo/bar/', '/foo/bar/'],
17-
['/foo/bar/README.md', '/foo/bar/'],
18-
['/foo/bar/index.md', '/foo/bar/'],
19-
['/foo/bar/index.html', '/foo/bar/'],
2036
['/foo/bar.md', '/foo/bar.html'],
2137
['/foo/bar.html', '/foo/bar.html'],
38+
['foo', 'foo.html'],
39+
['foo.md', 'foo.html'],
40+
['foo.html', 'foo.html'],
41+
['foo/bar', 'foo/bar.html'],
42+
['foo/bar.md', 'foo/bar.html'],
43+
['foo/bar.html', 'foo/bar.html'],
44+
45+
// unexpected corner cases
46+
['.md', '.html'],
47+
['foo/.md', 'foo/.html'],
48+
['/.md', '/.html'],
49+
['/foo/.md', '/foo/.html'],
2250
]
2351

2452
testCases.forEach(([path, expected]) =>

0 commit comments

Comments
 (0)
Please sign in to comment.