-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathsocials-transformer.ts
More file actions
166 lines (151 loc) · 3.83 KB
/
socials-transformer.ts
File metadata and controls
166 lines (151 loc) · 3.83 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import { z } from "astro:content";
import { type ProfileMatch, SocialLinks } from "social-links";
export const socialsSchema = z.union([
z.string().url(),
z.object({
icon: z.string().optional(),
platform: z.string().optional(),
url: z.string().url(),
username: z.string().optional(),
}),
]);
export type SocialsSchema = z.infer<typeof socialsSchema>;
export type SocialsData = {
icon: string | null;
url: string;
username: string | null;
platform: WEBSITE_TYPES;
};
export type WEBSITE_TYPES =
| "github"
| "tumblr"
| "twitter"
| "npm"
| "web"
| "bsky"
| "twitch"
| "mastodon"
| "dreamwidth"
| "ko-fi"
| "patreon"
| "youtube"
| "kickstarter"
| "inprnt"
| "neocities";
export const transformSocial = (social: SocialsSchema) => {
if (typeof social == "string") {
return extractSocialData({ url: social });
}
const { icon, url, platform, username } = social;
const data = extractSocialData({ url });
return {
...data,
icon:
icon ??
(platform === undefined
? data.icon
: getSocialIcon(platform as WEBSITE_TYPES)),
url,
platform,
username,
};
};
const socialLinks = new SocialLinks();
const tumblrMatches: ProfileMatch[] = [
{
match: "https?://([a-z0-9-]+).tumblr.com/?",
// TODO: more may be necessary for things like extracting usernames
group: 1,
},
{
match: "https?://www.tumblr.com/([a-z0-9-]+)",
// TODO: more may be necessary for things like extracting usernames
group: 1,
},
];
socialLinks.addProfile("tumblr", tumblrMatches);
const kofiMatches: ProfileMatch[] = [
{
match: "https?://ko-fi.com/([a-z0-9-_]+)",
group: 1,
},
];
socialLinks.addProfile("ko-fi", kofiMatches);
const inprntMatches: ProfileMatch[] = [
{
match: "https?://(?:www.)?inprnt.com/gallery/([a-z0-9-]+)/?",
group: 1,
},
];
socialLinks.addProfile("inprnt", inprntMatches);
const neocitiesMatches: ProfileMatch[] = [
{
match: "https?://([a-z0-9-]+).neocities.org",
group: 1,
},
];
socialLinks.addProfile("neocities", neocitiesMatches);
const blueSkyMatches: ProfileMatch[] = [
{
match: "https?://([a-z0-9-]+).bsky.social",
group: 1,
},
];
socialLinks.addProfile("bsky", blueSkyMatches);
const ao3Matches: ProfileMatch[] = [
{
match: "https?://archiveofourown.org/users/([a-z0-9-]+)",
group: 1,
},
];
socialLinks.addProfile("archiveofourown", ao3Matches);
const dreamwidthMatches: ProfileMatch[] = [
{
match: "https?://([a-z0-9-]+).dreamwidth.org",
group: 1,
},
];
socialLinks.addProfile("dreamwidth", dreamwidthMatches);
const furaffinityMatches: ProfileMatch[] = [
{
match: "https?://www.furaffinity.net/user/([a-z0-9-]+)",
group: 1,
},
];
socialLinks.addProfile("furaffinity", furaffinityMatches);
const carrdMatches: ProfileMatch[] = [
{
match: "https?://([a-z0-9-]+).carrd.co/?",
group: 1,
},
];
socialLinks.addProfile("carrd", carrdMatches);
const getSocialIcon = (platform: WEBSITE_TYPES) => {
if (platform === "inprnt") {
return "lucide:shopping-bag";
}
if (platform === "neocities") {
return "lucide:cat";
}
if (platform === "bsky") {
return "simple-icons:bluesky";
}
if (platform === "dreamwidth") {
return "simple-icons:livejournal";
}
return "simple-icons:" + platform.replaceAll("-", "");
};
export const extractSocialData = ({ url }: { url: string }): SocialsData => {
const lowerUrl = url.toLowerCase();
const socialLinkAttempt = socialLinks.detectProfile(lowerUrl);
if (socialLinkAttempt) {
return {
url,
platform: socialLinkAttempt as WEBSITE_TYPES,
username: socialLinks.getProfileId(socialLinkAttempt, lowerUrl),
icon: getSocialIcon(socialLinkAttempt as WEBSITE_TYPES),
};
}
// If you cannot find it, return the original url
return { url, platform: "web", username: null, icon: null };
};