Skip to content

Commit

Permalink
fix(shared): fix normalizeRoutePath
Browse files Browse the repository at this point in the history
  • Loading branch information
Mister-Hope committed Feb 5, 2024
1 parent 47b6d64 commit a3cc364
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 7 deletions.
24 changes: 17 additions & 7 deletions packages/shared/src/utils/normalizeRoutePath.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,38 @@
const HASH_REGEXP = /#.*$/u

/**
* Normalize the given path to the final route path
*/
export const normalizeRoutePath = (path: string): string => {
if (!path || path.endsWith('/')) {
export const normalizeRoutePath = (
path: string,
preserveHash = true,
): string => {
if (!path) {
return path
}

// convert README.md to index.html
let routePath = path.replace(/(^|\/)README.md$/i, '$1index.html')
const hash = preserveHash ? HASH_REGEXP.exec(path)?.[0] || '' : ''

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

// convert /foo/bar.md to /foo/bar.html
if (routePath.endsWith('.md')) {
routePath = routePath.substring(0, routePath.length - 3) + '.html'
}

// convert /foo/bar to /foo/bar.html
if (!routePath.endsWith('.html')) {
if (routePath && !routePath.endsWith('.html') && !routePath.endsWith('/')) {
routePath = routePath + '.html'
}

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

return routePath
return routePath + hash
}
40 changes: 40 additions & 0 deletions packages/shared/tests/normalizeRoutePath.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,32 @@ const testCases = [
['foo/index.html', 'foo/'],
['foo/index', 'foo/'],

// index with hash
['/#abc', '/#abc'],
['/README.md#abc', '/#abc'],
['/readme.md#abc', '/#abc'],
['/index.md#abc', '/#abc'],
['/index.html#abc', '/#abc'],
['/index#abc', '/#abc'],
['/foo/#abc', '/foo/#abc'],
['/foo/README.md#abc', '/foo/#abc'],
['/foo/readme.md#abc', '/foo/#abc'],
['/foo/index.md#abc', '/foo/#abc'],
['/foo/index.html#abc', '/foo/#abc'],
['/foo/index#abc', '/foo/#abc'],
['#abc', '#abc'],
['README.md#abc', 'index.html#abc'],
['readme.md#abc', 'index.html#abc'],
['index.md#abc', 'index.html#abc'],
['index.html#abc', 'index.html#abc'],
['index#abc', 'index.html#abc'],
['foo/#abc', 'foo/#abc'],
['foo/README.md#abc', 'foo/#abc'],
['foo/readme.md#abc', 'foo/#abc'],
['foo/index.md#abc', 'foo/#abc'],
['foo/index.html#abc', 'foo/#abc'],
['foo/index#abc', 'foo/#abc'],

// non-index
['/foo', '/foo.html'],
['/foo.md', '/foo.html'],
Expand All @@ -42,6 +68,20 @@ const testCases = [
['foo/bar.md', 'foo/bar.html'],
['foo/bar.html', 'foo/bar.html'],

// non-index with hash
['/foo#abc', '/foo.html#abc'],
['/foo.md#abc', '/foo.html#abc'],
['/foo.html#abc', '/foo.html#abc'],
['/foo/bar#abc', '/foo/bar.html#abc'],
['/foo/bar.md#abc', '/foo/bar.html#abc'],
['/foo/bar.html#abc', '/foo/bar.html#abc'],
['foo#abc', 'foo.html#abc'],
['foo.md#abc', 'foo.html#abc'],
['foo.html#abc', 'foo.html#abc'],
['foo/bar#abc', 'foo/bar.html#abc'],
['foo/bar.md#abc', 'foo/bar.html#abc'],
['foo/bar.html#abc', 'foo/bar.html#abc'],

// unexpected corner cases
['.md', '.html'],
['foo/.md', 'foo/.html'],
Expand Down

0 comments on commit a3cc364

Please sign in to comment.