Skip to content

Commit c443a95

Browse files
feat(shared): support relative links in normalizeRoutePath (#1544)
Co-authored-by: Xinyu Liu <[email protected]>
1 parent bab6ae9 commit c443a95

File tree

5 files changed

+324
-109
lines changed

5 files changed

+324
-109
lines changed

packages/shared/src/utils/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ export * from './dedupeHead.js'
22
export * from './ensureLeadingSlash.js'
33
export * from './ensureEndingSlash.js'
44
export * from './formatDateString.js'
5+
export * from './inferRoutePath.js'
56
export * from './isLinkExternal.js'
67
export * from './isLinkHttp.js'
78
export * from './isLinkWithProtocol.js'
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Infer route path according to the given (markdown file) path
3+
*/
4+
export const inferRoutePath = (path: string): string => {
5+
// if the pathname is empty or ends with `/`, return as is
6+
if (!path || path.endsWith('/')) return path
7+
8+
// convert README.md to index.html
9+
let routePath = path.replace(/(^|\/)README.md$/i, '$1index.html')
10+
11+
// convert /foo/bar.md to /foo/bar.html
12+
if (routePath.endsWith('.md')) {
13+
routePath = routePath.substring(0, routePath.length - 3) + '.html'
14+
}
15+
// convert /foo/bar to /foo/bar.html
16+
else if (!routePath.endsWith('.html')) {
17+
routePath = routePath + '.html'
18+
}
19+
20+
// convert /foo/index.html to /foo/
21+
if (routePath.endsWith('/index.html')) {
22+
routePath = routePath.substring(0, routePath.length - 10)
23+
}
24+
25+
return routePath
26+
}
Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,21 @@
1+
import { inferRoutePath } from './inferRoutePath.js'
2+
3+
const FAKE_HOST = 'http://.'
4+
15
/**
26
* Normalize the given path to the final route path
37
*/
4-
export const normalizeRoutePath = (path: string): string => {
5-
// split pathname and query/hash
6-
const [pathname, ...queryAndHash] = path.split(/(\?|#)/)
7-
8-
// if the pathname is empty or ends with `/`, return as is
9-
if (!pathname || pathname.endsWith('/')) return path
8+
export const normalizeRoutePath = (path: string, current?: string): string => {
9+
if (!path.startsWith('/') && current) {
10+
// the relative path should be resolved against the current path
11+
const loc = current.slice(0, current.lastIndexOf('/'))
1012

11-
// convert README.md to index.html
12-
let routePath = pathname.replace(/(^|\/)README.md$/i, '$1index.html')
13+
const { pathname, search, hash } = new URL(`${loc}/${path}`, FAKE_HOST)
1314

14-
// convert /foo/bar.md to /foo/bar.html
15-
if (routePath.endsWith('.md')) {
16-
routePath = routePath.substring(0, routePath.length - 3) + '.html'
17-
}
18-
// convert /foo/bar to /foo/bar.html
19-
else if (!routePath.endsWith('.html')) {
20-
routePath = routePath + '.html'
15+
return inferRoutePath(pathname) + search + hash
2116
}
2217

23-
// convert /foo/index.html to /foo/
24-
if (routePath.endsWith('/index.html')) {
25-
routePath = routePath.substring(0, routePath.length - 10)
26-
}
18+
const [pathname, ...queryAndHash] = path.split(/(\?|#)/)
2719

28-
// add query and hash back
29-
return routePath + queryAndHash.join('')
20+
return inferRoutePath(pathname) + queryAndHash.join('')
3021
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import { describe, expect, it } from 'vitest'
2+
import { inferRoutePath } from '../src/index.js'
3+
4+
const testCases = [
5+
// absolute index
6+
['/', '/'],
7+
['/README.md', '/'],
8+
['/readme.md', '/'],
9+
['/index.md', '/'],
10+
['/index.html', '/'],
11+
['/index', '/'],
12+
['/foo/', '/foo/'],
13+
['/foo/README.md', '/foo/'],
14+
['/foo/readme.md', '/foo/'],
15+
['/foo/index.md', '/foo/'],
16+
['/foo/index.html', '/foo/'],
17+
['/foo/index', '/foo/'],
18+
['README.md', 'index.html'],
19+
['readme.md', 'index.html'],
20+
['index.md', 'index.html'],
21+
['index.html', 'index.html'],
22+
['index', 'index.html'],
23+
24+
// absolute non-index
25+
['/foo', '/foo.html'],
26+
['/foo.md', '/foo.html'],
27+
['/foo.html', '/foo.html'],
28+
['/foo/bar', '/foo/bar.html'],
29+
['/foo/bar.md', '/foo/bar.html'],
30+
['/foo/bar.html', '/foo/bar.html'],
31+
32+
// relative index without current
33+
['foo/', 'foo/'],
34+
['foo/README.md', 'foo/'],
35+
['foo/readme.md', 'foo/'],
36+
['foo/index.md', 'foo/'],
37+
['foo/index.html', 'foo/'],
38+
['foo/index', 'foo/'],
39+
40+
// relative non index without current
41+
['foo', 'foo.html'],
42+
['foo.md', 'foo.html'],
43+
['foo.html', 'foo.html'],
44+
['foo/bar', 'foo/bar.html'],
45+
['foo/bar.md', 'foo/bar.html'],
46+
['foo/bar.html', 'foo/bar.html'],
47+
48+
// unexpected corner cases
49+
['', ''],
50+
['.md', '.html'],
51+
['foo/.md', 'foo/.html'],
52+
['/.md', '/.html'],
53+
['/foo/.md', '/foo/.html'],
54+
]
55+
56+
describe('should normalize clean paths correctly', () => {
57+
testCases.forEach(([path, expected]) =>
58+
it(`"${path}" -> "${expected}"`, () => {
59+
expect(inferRoutePath(path)).toBe(expected)
60+
}),
61+
)
62+
})

0 commit comments

Comments
 (0)