Files
SE-LTD/shared/sidebar-links.mjs
T

20 lines
653 B
JavaScript

export function normalizeSidebarLink(link) {
const label = typeof link?.label === "string" ? link.label.trim() : "";
if (!label) throw new Error("Bitte einen Namen für den Link eingeben.");
const address = typeof link.url === "string" ? link.url.trim() : "";
let url;
if (/^https?:\/\//i.test(address)) {
try {
url = new URL(address);
} catch {
// Report malformed addresses with the same message as unsupported schemes.
}
}
if (!url || url.username || url.password) {
throw new Error("Bitte eine vollständige HTTP- oder HTTPS-Adresse ohne Zugangsdaten eingeben.");
}
return { label, url: url.href };
}