-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathImageRenderers.ts
265 lines (234 loc) · 8.18 KB
/
ImageRenderers.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
import React, { ReactSVGElement } from 'react';
import type { DocsPageData, ImageRenderer, ImageGeneratorOptions, BlogPageData, PageData } from '@kas-tle/docusaurus-og';
import { join } from 'path';
import { readFileSync } from 'fs'
import { faDownload, faHouse, faBlog, faBook, IconDefinition } from '@fortawesome/free-solid-svg-icons';
const geyserLogo = join(__dirname, '../../static/img/icons/geyser.png');
const geyserLogoBase64 = `data:image/png;base64,${readFileSync(geyserLogo).toString('base64')}`;
const titleElement = ({ children }) => React.createElement(
'label',
{
style: {
fontSize: 72,
fontWeight: 800,
letterSpacing: 1,
margin: '25px 225px 10px 0px',
color: '#e3e3e3',
wordBreak: 'break-word',
}
},
children
);
const docsSvgElement = faSvg(faBook);
const blogSvgElement = faSvg(faBlog);
const homeSvgElement = faSvg(faHouse);
const downloadSvgElement = faSvg(faDownload);
const geyserLogoElement = React.createElement(
'img',
{
src: geyserLogoBase64,
style: {
position: 'absolute',
top: 20,
right: 20,
width: 225,
height: 225,
borderRadius: '50%',
border: '10px solid #e3e3e3'
},
},
)
const headerElement = (header: string, svg: ReactSVGElement) => React.createElement(
'div',
{
style: {
display: 'flex',
alignItems: 'center',
position: 'absolute',
top: 20,
left: 40,
},
},
svg,
React.createElement(
'label',
{
style: {
fontSize: 32,
fontWeight: 600,
letterSpacing: 1,
color: '#e3e3e3',
},
},
header,
),
)
const rootDivStyle: React.CSSProperties = {
display: 'flex',
flexDirection: 'column',
height: '100%',
width: '100%',
padding: '10px 40px',
justifyContent: 'center',
fontFamily: 'Montserrat',
fontSize: 32,
fontWeight: 400,
backgroundColor: '#0f1b23',
color: '#e3e3e3',
borderBottom: '2rem solid rgb(37, 194, 160)',
};
export const docOgRenderer: ImageRenderer<DocsPageData> = async (data, context) => {
const element = React.createElement(
'div',
{ style: rootDivStyle },
React.createElement(titleElement, null, data.metadata.title),
React.createElement('div', null, data.metadata.description),
geyserLogoElement,
headerElement(docSectionPath(data.metadata.slug, context.siteConfig.title), docsSvgElement),
);
return [
element,
await imageGeneratorOptions(),
];
};
export const blogOgRenderer: ImageRenderer<BlogPageData> = async (data, context) => {
const isPost = data.pageType === 'post';
let posterImageElement = undefined;
if (isPost && data.data.metadata.authors.length > 0) {
// Create line of all author avatar images in top right corner
const authorImages = data.data.metadata.authors.map(async author => React.createElement(
'img',
{
src: await encodeRemoteImage(author.imageURL),
style: {
width: 112.5,
height: 112.5,
marginRight: 10,
borderRadius: '50%',
border: '10px solid #e3e3e3',
},
},
));
posterImageElement = React.createElement(
'div',
{
style: {
display: 'flex',
position: 'absolute',
top: 20,
right: 175,
},
},
...await Promise.all(authorImages),
);
}
const element = React.createElement(
'div',
{ style: rootDivStyle },
React.createElement(titleElement, null, isPost ? data.data.metadata.title : context.siteConfig.title + ' Blog'),
React.createElement('div', null, isPost ? data.data.metadata.description : ''),
geyserLogoElement,
posterImageElement,
headerElement(context.siteConfig.title + ' / Blog' + (isPost ? ` / ${new Date(data.data.metadata.date).toISOString().split('T')[0].replaceAll('-','.')}` : ''), blogSvgElement),
);
return [
element,
await imageGeneratorOptions(),
];
};
export const pageOgRenderer: ImageRenderer<PageData> = async (data, context) => {
const extraTitleLength = (' ' + context.siteConfig.titleDelimiter + ' ' + context.siteConfig.title).length;
await data.document.load();
const title = (data.document.loaded && data.document.root.querySelector('title')?.textContent).slice(0, -extraTitleLength) || context.siteConfig.title;
const description = (data.document.loaded && data.document.root.querySelector('meta[name="description"]').getAttribute('content')) || context.siteConfig.tagline;
const element = React.createElement(
'div',
{ style: rootDivStyle },
React.createElement(titleElement, null, title),
React.createElement('div', null, description),
geyserLogoElement,
headerElement(context.siteConfig.title + ' / ' + title, pageSvg(data.metadata.permalink)),
);
return [
element,
await imageGeneratorOptions(),
];
};
const imageGeneratorOptions = async (): Promise<ImageGeneratorOptions> => {
return {
width: 1200,
height: 600,
fonts: [
{
name: 'Montserrat',
data: await getTtfFont('Montserrat', ['ital', 'wght'], [0, 400]),
weight: 400,
style: 'normal',
},
{
name: 'Montserrat',
data: await getTtfFont('Montserrat', ['ital', 'wght'], [0, 600]),
weight: 600,
style: 'normal',
},
{
name: 'Montserrat',
data: await getTtfFont('Montserrat', ['ital', 'wght'], [0, 800]),
weight: 800,
style: 'normal',
},
],
};
}
function docSectionPath(slug: string, title: string) {
let section = slug.split('/')[1].toString();
// Override some sections by slug
switch (section) {
case 'api':
section = 'REST APIs';
break;
}
section = section.charAt(0).toUpperCase() + section.slice(1);
return `${title} / ${section}`;
}
function pageSvg(permalink: string) {
switch (permalink.slice(1, -1)) {
case 'download':
return downloadSvgElement;
default:
return homeSvgElement;
}
}
async function getTtfFont(family: string, axes: string[], value: number[]): Promise<ArrayBuffer> {
const familyParam = axes.join(',') + '@' + value.join(',');
// Get css style sheet with user agent Mozilla/5.0 Firefox/1.0 to ensure TTF is returned
const cssCall = await fetch(`https://fonts.googleapis.com/css2?family=${family}:${familyParam}&display=swap`, {
headers: {
'User-Agent': 'Mozilla/5.0 Firefox/1.0',
},
});
const css = await cssCall.text();
const ttfUrl = css.match(/url\(([^)]+)\)/)?.[1];
return await fetch(ttfUrl).then(res => res.arrayBuffer());
}
async function encodeRemoteImage(url: string): Promise<string> {
try {
const res = await fetch(url);
const buffer = await res.arrayBuffer().then(buf => Buffer.from(buf));
return `data:${res.headers.get('content-type')};base64,${buffer.toString('base64')}`;
} catch (error) {
return geyserLogoBase64;
}
}
function faSvg(icon: IconDefinition): ReactSVGElement {
// create all path elements from icon.icon[4] contents (could be string or array of strings)
const paths = typeof icon.icon[4] === 'string' ? [icon.icon[4]] : icon.icon[4];
const svgPaths = paths.map((path: string) => React.createElement('path', { d: path, fill: '#e3e3e3' }));
const viewBox = '0 0 ' + icon.icon[0] + ' ' + icon.icon[1];
const element = React.createElement('svg', {
width: '50px',
viewBox,
style: { marginRight: 10 },
}, ...svgPaths);
return element;
}