21 lines
711 B
TypeScript

const configuredBasePath = process.env.NEXT_PUBLIC_PASCAL_BASE_PATH?.trim() ?? ''
const EDITOR_BASE_PATH = configuredBasePath
? `/${configuredBasePath.replace(/^\/+|\/+$/g, '')}`
: ''
export function withEditorBasePath(pathname: string, basePath = EDITOR_BASE_PATH): string {
if (/^(?:[a-z]+:)?\/\//i.test(pathname) || pathname.startsWith('data:')) return pathname
const path = pathname.startsWith('/') ? pathname : `/${pathname}`
const normalizedBasePath = basePath ? `/${basePath.replace(/^\/+|\/+$/g, '')}` : ''
if (
!normalizedBasePath ||
path === normalizedBasePath ||
path.startsWith(`${normalizedBasePath}/`)
) {
return path
}
return `${normalizedBasePath}${path}`
}