-
Notifications
You must be signed in to change notification settings - Fork 921
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'normalizeRoutePath' into route-link
- Loading branch information
Showing
6 changed files
with
99 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
export const inferRoutePath = (path: string): string => { | ||
// if the pathname is empty or ends with `/`, return as is | ||
if (!path || path.endsWith('/')) return path | ||
|
||
// convert README.md to index.html | ||
let routePath = path.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 | ||
else if (!routePath.endsWith('.html')) { | ||
routePath = routePath + '.html' | ||
} | ||
|
||
// convert /foo/index.html to /foo/ | ||
if (routePath.endsWith('/index.html')) { | ||
routePath = routePath.substring(0, routePath.length - 10) | ||
} | ||
|
||
return routePath | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { describe, expect, it } from 'vitest' | ||
import { inferRoutePath } from '../src/index.js' | ||
|
||
const testCases = [ | ||
// absolute index | ||
['/', '/'], | ||
['/README.md', '/'], | ||
['/readme.md', '/'], | ||
['/index.md', '/'], | ||
['/index.html', '/'], | ||
['/index', '/'], | ||
['/foo/', '/foo/'], | ||
['/foo/README.md', '/foo/'], | ||
['/foo/readme.md', '/foo/'], | ||
['/foo/index.md', '/foo/'], | ||
['/foo/index.html', '/foo/'], | ||
['/foo/index', '/foo/'], | ||
['README.md', 'index.html'], | ||
['readme.md', 'index.html'], | ||
['index.md', 'index.html'], | ||
['index.html', 'index.html'], | ||
['index', 'index.html'], | ||
|
||
// absolute non-index | ||
['/foo', '/foo.html'], | ||
['/foo.md', '/foo.html'], | ||
['/foo.html', '/foo.html'], | ||
['/foo/bar', '/foo/bar.html'], | ||
['/foo/bar.md', '/foo/bar.html'], | ||
['/foo/bar.html', '/foo/bar.html'], | ||
|
||
// relative index without current | ||
['foo/', 'foo/'], | ||
['foo/README.md', 'foo/'], | ||
['foo/readme.md', 'foo/'], | ||
['foo/index.md', 'foo/'], | ||
['foo/index.html', 'foo/'], | ||
['foo/index', 'foo/'], | ||
|
||
// relative non index without current | ||
['foo', 'foo.html'], | ||
['foo.md', 'foo.html'], | ||
['foo.html', 'foo.html'], | ||
['foo/bar', 'foo/bar.html'], | ||
['foo/bar.md', 'foo/bar.html'], | ||
['foo/bar.html', 'foo/bar.html'], | ||
|
||
// unexpected corner cases | ||
['', ''], | ||
['.md', '.html'], | ||
['foo/.md', 'foo/.html'], | ||
['/.md', '/.html'], | ||
['/foo/.md', '/foo/.html'], | ||
] | ||
|
||
describe('should normalize clean paths correctly', () => { | ||
testCases.forEach(([path, expected]) => | ||
it(`"${path}" -> "${expected}"`, () => { | ||
expect(inferRoutePath(path)).toBe(expected) | ||
}), | ||
) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters