forked from Mergifyio/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate-og-images.mjs
73 lines (58 loc) · 1.92 KB
/
generate-og-images.mjs
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
import fs from 'fs'
import {createCanvas, loadImage, registerFont} from 'canvas'
import {resolve} from 'path';
const removeTrailingSlash = (str) => {
if (str.endsWith("/")) {
return str.slice(0, -1);
}
return str;
}
/** writeFileSync does not create dir if it don't exist, we need to do it ourselves */
const saveImage = (path, content) => {
let filepath = path.replace(/\\/g,'/'); // Normalize path in case of Windows
let root = '';
if (filepath[0] === '/') {
root = '/';
filepath = filepath.slice(1);
}
const folders = filepath.split('/').slice(0, -1);
folders.reduce(
(acc, folder) => {
const folderPath = acc + folder + '/';
if (!fs.existsSync(folderPath)) {
fs.mkdirSync(folderPath);
}
return folderPath
},
root
);
fs.writeFileSync(root + filepath, content);
}
export const generateImage = async (title, path) => {
if (title == null) return;
registerFont('./static/fonts/Poppins-Light.ttf', {family: 'Poppins', weight: 300})
const canvas = createCanvas(1200, 630)
const ctx = canvas.getContext("2d");
const image = await loadImage(resolve('./static/og-template.jpg'))
ctx.drawImage(image, 0, 0)
ctx.font = 'normal normal 300 64px Poppins';
ctx.fillStyle = 'black';
const words = title.split(" ");
let x = 74;
let y = 350;
const shouldBreakLine = ctx.measureText(title).width + x >= canvas.width;
if (shouldBreakLine) {
// Start with a lower Y if line should break, to give the next line space to draw
y = 280;
}
for (const word of words) {
if ((x + ctx.measureText(word).width) >= canvas.width) {
y += ctx.measureText("M").width + 20;
x = 74;
}
ctx.fillText(word, x, y);
x += ctx.measureText(word).width + ctx.measureText(" ").width;
}
const buffer = canvas.toBuffer('image/png')
saveImage(resolve(`./static/og-images/${path === '/' ? 'home' : removeTrailingSlash(path)}.png`), buffer)
};