Re-implement domain normalization

This commit is contained in:
Nikita Karamov 2023-03-27 21:23:49 +02:00
parent a5314e5923
commit 23fdf5ab07
No known key found for this signature in database
GPG key ID: 41D6F71EE78E77CD
2 changed files with 10 additions and 5 deletions

View file

@ -65,7 +65,7 @@ const { prefilledInstance } = Astro.props;
</style>
<script>
import { extractHost, normalizeURL } from "@scripts/util";
import { getUrlDomain, normalizeURL } from "@scripts/util";
import {
savedInstances as instanceStore,
saveInstance,
@ -88,7 +88,7 @@ const { prefilledInstance } = Astro.props;
return [];
}
const host = extractHost(instance);
const host = getUrlDomain(instance);
if (index == 0 && !$instance.value) {
$instance.value = host;
}

View file

@ -19,9 +19,14 @@ export const normalizeURL = (url: string): string => {
return url;
};
export const extractHost = (url: string): string => {
if (!(url.startsWith("https://") || url.startsWith("http://"))) {
url = "https://" + url;
export const getUrlDomain = (url: string | URL): string => {
if (typeof url === "string") {
url = url.trim();
if (!/^https?:\/\//.test(url)) {
url = `https://${url}`;
}
}
return new URL(url).host;
};