2022-03-22 15:54:05 +01:00
|
|
|
export function replaceBasePath(pathname: string, basePath: string): string {
|
|
|
|
// ensure basePath is only stripped if it matches exactly
|
|
|
|
// and doesn't contain extra chars e.g. basePath /docs
|
|
|
|
// should replace for /docs, /docs/, /docs/a but not /docsss
|
|
|
|
if (hasBasePath(pathname, basePath)) {
|
2022-03-24 23:35:33 +01:00
|
|
|
pathname = pathname.slice(basePath.length)
|
2022-03-22 15:54:05 +01:00
|
|
|
if (!pathname.startsWith('/')) pathname = `/${pathname}`
|
|
|
|
}
|
|
|
|
return pathname
|
|
|
|
}
|
|
|
|
|
|
|
|
export function hasBasePath(pathname: string, basePath: string): boolean {
|
|
|
|
return (
|
|
|
|
typeof pathname === 'string' &&
|
|
|
|
(pathname === basePath || pathname.startsWith(basePath + '/'))
|
|
|
|
)
|
|
|
|
}
|