mirror of
https://github.com/outline/outline.git
synced 2026-11-22 22:02:35 +03:00
24 lines
656 B
TypeScript
24 lines
656 B
TypeScript
export const normalizeURL = (urlString: string): string => {
|
|
urlString = urlString.trim();
|
|
|
|
const hasRelativeProtocol = urlString.startsWith("//");
|
|
const isRelativeURL = !hasRelativeProtocol && /^\.*\//.test(urlString);
|
|
|
|
if (!isRelativeURL) {
|
|
urlString = urlString.replace(/^(?!(?:\w+:)?\/\/)|^\/\//, "https://");
|
|
}
|
|
|
|
const urlObject = new URL(urlString);
|
|
|
|
// Remove 'www'
|
|
urlObject.hostname = urlObject.hostname.replace(/^www\./, "");
|
|
|
|
// Remove trailing `/`
|
|
const newURLString = urlObject.toString().replace(/\/$/, "");
|
|
|
|
// Remove protocol
|
|
const finalURL = newURLString.replace(/^(?:https?:)?\/\//, "");
|
|
|
|
return finalURL;
|
|
};
|