sparkast/lib/normalizeURL.ts
alikia2x 263b82c06e feature: keyword2link suggestion
- better optimization for NLU model loading
- fix some bugs to ensure that the build passes
2024-07-16 21:29:17 +08:00

19 lines
593 B
TypeScript

export function normalizeURL(input: string): string {
try {
// try to create a URL object
const url = new URL(input, window.location.href);
// if the URL is valid, return it
return url.href;
} catch (error) {
// if the URL is invalid, try to add the protocol
const withHTTP = "http://" + input;
try {
const urlWithHTTP = new URL(withHTTP);
return urlWithHTTP.href;
} catch (error) {
// if the URL is still invalid, return the original input
return input;
}
}
}