-
Notifications
You must be signed in to change notification settings - Fork 920
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(shared): support relative links in normalizeRoutePath (#1544)
Co-authored-by: Xinyu Liu <[email protected]>
- Loading branch information
1 parent
bab6ae9
commit c443a95
Showing
5 changed files
with
324 additions
and
109 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,26 @@ | ||
/** | ||
* Infer route path according to the given (markdown file) path | ||
*/ | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,21 @@ | ||
import { inferRoutePath } from './inferRoutePath.js' | ||
|
||
const FAKE_HOST = 'http://.' | ||
|
||
/** | ||
* Normalize the given path to the final route path | ||
*/ | ||
export const normalizeRoutePath = (path: string): string => { | ||
// split pathname and query/hash | ||
const [pathname, ...queryAndHash] = path.split(/(\?|#)/) | ||
|
||
// if the pathname is empty or ends with `/`, return as is | ||
if (!pathname || pathname.endsWith('/')) return path | ||
export const normalizeRoutePath = (path: string, current?: string): string => { | ||
if (!path.startsWith('/') && current) { | ||
// the relative path should be resolved against the current path | ||
const loc = current.slice(0, current.lastIndexOf('/')) | ||
|
||
// convert README.md to index.html | ||
let routePath = pathname.replace(/(^|\/)README.md$/i, '$1index.html') | ||
const { pathname, search, hash } = new URL(`${loc}/${path}`, FAKE_HOST) | ||
|
||
// 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' | ||
return inferRoutePath(pathname) + search + hash | ||
} | ||
|
||
// convert /foo/index.html to /foo/ | ||
if (routePath.endsWith('/index.html')) { | ||
routePath = routePath.substring(0, routePath.length - 10) | ||
} | ||
const [pathname, ...queryAndHash] = path.split(/(\?|#)/) | ||
|
||
// add query and hash back | ||
return routePath + queryAndHash.join('') | ||
return inferRoutePath(pathname) + queryAndHash.join('') | ||
} |
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) | ||
}), | ||
) | ||
}) |
Oops, something went wrong.