Skip to content

Commit d94d9c5

Browse files
committed
chore: split function
1 parent 62cec2e commit d94d9c5

File tree

3 files changed

+26
-24
lines changed

3 files changed

+26
-24
lines changed

packages/shared/src/utils/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export * from './isLinkExternal.js'
66
export * from './isLinkHttp.js'
77
export * from './isLinkWithProtocol.js'
88
export * from './isPlainObject.js'
9+
export * from './inferRoutePath.js'
910
export * from './normalizeRoutePath.js'
1011
export * from './omit.js'
1112
export * from './removeEndingSlash.js'
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
export const inferRoutePath = (path: string): string => {
2+
// if the pathname is empty or ends with `/`, return as is
3+
if (!path || path.endsWith('/')) return path
4+
5+
// convert README.md to index.html
6+
let routePath = path.replace(/(^|\/)README.md$/i, '$1index.html')
7+
8+
// convert /foo/bar.md to /foo/bar.html
9+
if (routePath.endsWith('.md')) {
10+
routePath = routePath.substring(0, routePath.length - 3) + '.html'
11+
}
12+
// convert /foo/bar to /foo/bar.html
13+
else if (!routePath.endsWith('.html')) {
14+
routePath = routePath + '.html'
15+
}
16+
17+
// convert /foo/index.html to /foo/
18+
if (routePath.endsWith('/index.html')) {
19+
routePath = routePath.substring(0, routePath.length - 10)
20+
}
21+
22+
return routePath
23+
}

packages/shared/src/utils/normalizeRoutePath.ts

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,6 @@
1-
const FAKE_HOST = 'http://.'
2-
3-
export const inferRoutePath = (path: string): string => {
4-
// if the pathname is empty or ends with `/`, return as is
5-
if (!path || path.endsWith('/')) return path
6-
7-
// convert README.md to index.html
8-
let routePath = path.replace(/(^|\/)README.md$/i, '$1index.html')
9-
10-
// convert /foo/bar.md to /foo/bar.html
11-
if (routePath.endsWith('.md')) {
12-
routePath = routePath.substring(0, routePath.length - 3) + '.html'
13-
}
14-
// convert /foo/bar to /foo/bar.html
15-
else if (!routePath.endsWith('.html')) {
16-
routePath = routePath + '.html'
17-
}
18-
19-
// convert /foo/index.html to /foo/
20-
if (routePath.endsWith('/index.html')) {
21-
routePath = routePath.substring(0, routePath.length - 10)
22-
}
1+
import { inferRoutePath } from './inferRoutePath.js'
232

24-
return routePath
25-
}
3+
const FAKE_HOST = 'http://.'
264

275
/**
286
* Normalize the given path to the final route path

0 commit comments

Comments
 (0)