@@ -26,3 +26,20 @@ export function extractDomain(url: string) {
|
||||
const match = url.match(/^(?:https?:)?(?:\/\/)?(?:[^@\n]+@)?([^:\/\n]+)/im);
|
||||
return match ? match[1] : null;
|
||||
}
|
||||
|
||||
export function maybeMakeRelative(urlStr: string, baseStr: string): string {
|
||||
try {
|
||||
const baseObj = new URL(baseStr);
|
||||
const urlObj = new URL(urlStr);
|
||||
/* in all places where maybeMakeRelative is used, baseStr is the
|
||||
* instance's public URL, which can't have path components, so the
|
||||
* relative URL will always have the whole path from the urlStr
|
||||
*/
|
||||
if (urlObj.origin === baseObj.origin) {
|
||||
return urlObj.pathname + urlObj.search + urlObj.hash;
|
||||
}
|
||||
return urlStr;
|
||||
} catch (e) {
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user