mirror of
https://github.com/Tony0410/News-reader-pro.git
synced 2026-05-24 21:31:44 +08:00
24 lines
633 B
TypeScript
24 lines
633 B
TypeScript
export const normalizeUrl = (url: string) => {
|
|
let cleanUrl = url.trim();
|
|
if (!cleanUrl.startsWith('http://') && !cleanUrl.startsWith('https://')) {
|
|
return `https://${cleanUrl}`;
|
|
}
|
|
return cleanUrl;
|
|
};
|
|
|
|
export const getDisplayUrl = (url: string): { href: string; hostname: string } => {
|
|
const normalized = normalizeUrl(url);
|
|
|
|
try {
|
|
const parsed = new URL(normalized);
|
|
return { href: normalized, hostname: parsed.hostname };
|
|
} catch {
|
|
try {
|
|
const fallback = new URL(url);
|
|
return { href: url, hostname: fallback.hostname };
|
|
} catch {
|
|
return { href: url, hostname: url };
|
|
}
|
|
}
|
|
};
|