scrivito
Version:
Scrivito is a professional, yet easy to use SaaS Enterprise Content Management Service, built for digital agencies and medium to large businesses. It is completely maintenance-free, cost-effective, and has unprecedented performance and security.
39 lines (29 loc) • 663 B
text/typescript
import * as URI from 'urijs';
export function sanitizeUrl(rawUrl: string): string {
const url = rawUrl.trim();
let uri: URI;
try {
uri = URI(url);
} catch {
return url;
}
if (!uri.protocol() && url.match(/^[^/@]+@[^/@]+$/)) {
return `mailto:${url}`;
}
if (!(uri.protocol() || url.startsWith('/')) && url.includes('.')) {
const hostname = getHostname(url);
if (hostname && !hostname.includes('_')) {
return `https://${url}`;
}
}
return url;
}
function getHostname(urlString: string) {
let url: URL;
try {
url = new URL(`https://${urlString}`);
} catch {
return;
}
return url.hostname;
}