~james/core

core/web/src/lib/components/profile/profileLinks.ts -rw-r--r-- 940 bytes
452f17b7 — james chore(import): snapshot james.dad.town/core at f70c71c7696a 17 hours ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import { isDid } from "@atcute/lexicons/syntax";

export interface ProfileLinkTarget {
	href: string;
	label: string;
	external: boolean;
}

const ATPROTO_BROWSER = "https://pdsls.dev";

export const profileLinkTarget = (raw: string): ProfileLinkTarget | null => {
	const link = raw.trim();
	if (isDid(link)) return { href: `/${link}`, label: link, external: false };
	if (link.startsWith("at://")) {
		const [authority, ...rest] = link.slice("at://".length).split("/").filter(Boolean);
		if (!authority) return null;
		if (rest.length === 0) return { href: `/${authority}`, label: authority, external: false };
		return {
			href: `${ATPROTO_BROWSER}/${link}`,
			label: link.slice("at://".length),
			external: true
		};
	}
	try {
		const url = new URL(link);
		if (url.protocol !== "http:" && url.protocol !== "https:") return null;
		return { href: url.toString(), label: url.hostname, external: true };
	} catch {
		return null;
	}
};