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

View file

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