Skip to content

Commit

Permalink
refactor(shared): tweak normalizeRoutePath structure
Browse files Browse the repository at this point in the history
  • Loading branch information
meteorlxy committed Feb 5, 2024
1 parent 473f115 commit dcd9689
Showing 1 changed file with 4 additions and 6 deletions.
10 changes: 4 additions & 6 deletions packages/shared/src/utils/normalizeRoutePath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,11 @@
*/
export const normalizeRoutePath = (path: string): string => {
// split pathname and query/hash
const [pathname, ...rest] = path.split(/(\?|#)/)
const [pathname, ...queryAndHash] = path.split(/(\?|#)/)

// if the pathname is empty or ends with `/`, return as is
if (!pathname || pathname.endsWith('/')) return path

// join query and hash
const queryAndHash = rest.length > 0 ? rest.join('') : ''

// convert README.md to index.html
let routePath = pathname.replace(/(^|\/)README.md$/i, '$1index.html')

Expand All @@ -25,8 +22,9 @@ export const normalizeRoutePath = (path: string): string => {

// convert /foo/index.html to /foo/
if (routePath.endsWith('/index.html')) {
return routePath.substring(0, routePath.length - 10) + queryAndHash
routePath = routePath.substring(0, routePath.length - 10)
}

return routePath + queryAndHash
// add query and hash back
return routePath + queryAndHash.join('')
}

0 comments on commit dcd9689

Please sign in to comment.