-
Notifications
You must be signed in to change notification settings - Fork 6
/
seo.ts
154 lines (144 loc) · 3.66 KB
/
seo.ts
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
// This file is copied from https://github.com/flexdinesh/blogster/blob/main/packages/shared/src/seo.ts
// I just modified it for my personal needs.
import { urlJoin } from '@/helpers/tools';
import options from '@/options';
export interface PageMeta {
title: string;
description: string;
baseUrl?: string;
ogImageUrl?: string;
ogImageAltText: string;
ogImageWidth?: number;
ogImageHeight?: number;
siteOwnerTwitterHandle?: string;
contentAuthorTwitterHandle?: string;
}
export interface PostMeta {
title: string;
description: string;
pageUrl?: string;
authorName?: string;
publishDate: string;
ogImageUrl?: string;
ogImageAltText: string;
ogImageWidth?: number;
ogImageHeight?: number;
siteOwnerTwitterHandle?: string;
contentAuthorTwitterHandle?: string;
}
export interface TwitterOgMeta {
title: string;
description?: string;
card: 'summary_large_image';
site?: string;
creator?: string;
image?: string;
imageAlt?: string;
}
export interface PageOgMeta {
title: string;
description?: string;
type: 'website';
url?: string;
image?: string;
imageAlt?: string;
imageWidth?: string;
imageHeight?: string;
}
export interface PostOgMeta {
title: string;
description?: string;
type: 'article';
url?: string;
author?: string;
siteName?: string;
publishDate: string;
image?: string;
imageAlt?: string;
imageWidth?: string;
imageHeight?: string;
}
const parseOgImageUrl = (ogImageUrl?: string): string =>
typeof ogImageUrl === 'undefined'
? options.defaultOpenGraph()
: ogImageUrl.startsWith('/')
? urlJoin(options.assetsPrefix(), ogImageUrl)
: ogImageUrl;
export const getPageMeta = ({
title,
description,
baseUrl,
ogImageUrl,
ogImageAltText,
ogImageWidth,
ogImageHeight,
siteOwnerTwitterHandle,
contentAuthorTwitterHandle,
}: PageMeta): { og: PageOgMeta; twitter: TwitterOgMeta } => {
if (!title) {
throw Error('title is required for page SEO');
}
const ogImageAbsoluteUrl = parseOgImageUrl(ogImageUrl);
return {
og: {
title: title,
description: description,
type: 'website',
url: baseUrl,
image: ogImageAbsoluteUrl,
imageAlt: ogImageAltText,
imageWidth: ogImageWidth ? String(ogImageWidth) : undefined,
imageHeight: ogImageHeight ? String(ogImageHeight) : undefined,
},
twitter: {
title: title,
description: description,
card: 'summary_large_image',
site: siteOwnerTwitterHandle,
creator: contentAuthorTwitterHandle || siteOwnerTwitterHandle,
image: ogImageAbsoluteUrl,
imageAlt: ogImageAltText,
},
};
};
export const getBlogPostMeta = ({
title,
description,
pageUrl,
authorName,
publishDate,
ogImageUrl,
ogImageAltText,
ogImageWidth,
ogImageHeight,
siteOwnerTwitterHandle,
contentAuthorTwitterHandle,
}: PostMeta): { og: PostOgMeta; twitter: TwitterOgMeta } => {
if (!title) {
throw Error('title is required for page SEO');
}
const ogImageAbsoluteUrl = parseOgImageUrl(ogImageUrl);
return {
og: {
title: title,
description: description,
type: 'article',
url: pageUrl,
author: authorName,
publishDate: publishDate,
image: ogImageAbsoluteUrl,
imageAlt: ogImageAltText,
imageWidth: ogImageWidth ? String(ogImageWidth) : undefined,
imageHeight: ogImageHeight ? String(ogImageHeight) : undefined,
},
twitter: {
title: title,
description: description,
card: 'summary_large_image',
site: siteOwnerTwitterHandle,
creator: contentAuthorTwitterHandle || siteOwnerTwitterHandle,
image: ogImageAbsoluteUrl,
imageAlt: ogImageAltText,
},
};
};