File tree Expand file tree Collapse file tree 3 files changed +26
-24
lines changed
packages/shared/src/utils Expand file tree Collapse file tree 3 files changed +26
-24
lines changed Original file line number Diff line number Diff line change @@ -6,6 +6,7 @@ export * from './isLinkExternal.js'
66export * from './isLinkHttp.js'
77export * from './isLinkWithProtocol.js'
88export * from './isPlainObject.js'
9+ export * from './inferRoutePath.js'
910export * from './normalizeRoutePath.js'
1011export * from './omit.js'
1112export * from './removeEndingSlash.js'
Original file line number Diff line number Diff line change 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 ( / ( ^ | \/ ) R E A D M E .m d $ / 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+ }
Original file line number Diff line number Diff line change 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 ( / ( ^ | \/ ) R E A D M E .m d $ / 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
You can’t perform that action at this time.
0 commit comments