-
Notifications
You must be signed in to change notification settings - Fork 936
Expand file tree
/
Copy pathog.ts
More file actions
86 lines (69 loc) · 2.19 KB
/
og.ts
File metadata and controls
86 lines (69 loc) · 2.19 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
import { Base64 } from 'js-base64';
const stripEmojis = (str: string) =>
str
.replace(
/([\u2700-\u27BF]|[\uE000-\uF8FF]|\uD83C[\uDC00-\uDFFF]|\uD83D[\uDC00-\uDFFF]|[\u2011-\u26FF]|\uD83E[\uDD10-\uDDFF])/g,
'',
)
.replace(/\s+/g, ' ')
.trim();
export const getAutogeneratedPostOG = (post: any, publication: any) => {
const { title, readTimeInMinutes, reactionCount = 0, responseCount = 0, author } = post;
const ogUrl = `${publication.url}/api/og/post`;
const ogData: any = {};
try {
ogData.title = encodeURIComponent(stripEmojis(title));
ogData.author = encodeURIComponent(author!.name);
ogData.domain = new URL(publication.url).hostname;
if (author!.profilePicture) {
ogData.photo = author!.profilePicture;
}
if (readTimeInMinutes) {
ogData.readTime = readTimeInMinutes;
}
if (reactionCount > 0) {
ogData.reactions = reactionCount;
}
if (responseCount > 0) {
ogData.comments = responseCount;
}
} catch (e) {
console.log(e);
}
return `${ogUrl}?og=${Base64.encode(JSON.stringify(ogData))}`;
};
export const getAutogeneratedPublicationOG = (publication: any) => {
const { title, isTeam, favicon, author, followersCount, descriptionSEO } = publication;
const totalDocuments = publication.posts!.totalDocuments;
const logo = publication.preferences!.logo;
const ogUrl = `${publication.url}/api/og/home`;
const ogData: Record<string, any> = {};
try {
ogData.title = title
? encodeURIComponent(stripEmojis(title))
: `${author!.name}'s ${isTeam ? 'team' : ''} blog`;
ogData.domain = new URL(publication.url).hostname;
ogData.followers = isTeam ? followersCount : author!.followersCount;
if (author!.profilePicture && !isTeam) {
ogData.photo = author!.profilePicture;
}
if (logo) {
ogData.logo = logo;
}
if (isTeam) {
ogData.isTeam = isTeam;
}
if (descriptionSEO) {
ogData.meta = encodeURIComponent(stripEmojis(descriptionSEO)); // Vercel OG is not able to work with all kinds of emojis
}
if (favicon) {
ogData.favicon = favicon;
}
if (totalDocuments && totalDocuments > 0) {
ogData.articles = totalDocuments;
}
} catch (e) {
console.log(e);
}
return `${ogUrl}?og=${Base64.encode(JSON.stringify(ogData))}`;
};