aboutsummaryrefslogtreecommitdiff
path: root/src/lib/User/BadgeWall/badgeLinks.ts
blob: 92cbe6ce2221a160fcbdc39d6abf061ee42f4c6c (plain) (blame)
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
export interface BadgeLink {
	href: string | null;
	label: string;
}

const safeHttpUrl = (value: string): string | null => {
	try {
		const url = new URL(value);

		return url.protocol === "http:" || url.protocol === "https:" ? value : null;
	} catch {
		return null;
	}
};

export const classifySource = (source: string): BadgeLink => {
	let label = source;
	const sourceLower = source.toLowerCase();

	if (sourceLower.includes("pixiv.net")) {
		label = "Pixiv";
	} else if (
		sourceLower.includes("twitter.com") ||
		sourceLower.includes("x.com")
	) {
		label = "X (Twitter)";
	} else if (sourceLower.includes("zerochan.net")) {
		label = "Zerochan";
	} else if (sourceLower.includes("imgur.com")) {
		label = "Imgur";
	} else if (sourceLower.includes("lofter.com")) {
		label = "Lofter";
	}

	return { href: safeHttpUrl(source), label };
};

export const classifyDesigner = (designer: string): BadgeLink => {
	let name = designer;
	let userLink = designer;
	const designerLower = designer.toLowerCase();
	const anilistUser = designer.match(
		/https?:\/\/anilist\.co\/user\/([^/]+)\/?/,
	);

	if (anilistUser) {
		name = anilistUser[1];
	} else if (designerLower.startsWith("@")) {
		name = designer.replace("@", "");
		userLink = `https://anilist.co/user/${name}/`;
	} else if (!designerLower.startsWith("http")) {
		userLink = `https://anilist.co/user/${name}/`;
	}

	return { href: safeHttpUrl(userLink), label: name };
};