/
og-image.tsx
135 lines (129 loc) · 3.58 KB
/
og-image.tsx
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
import satori from "satori";
import sharp from "sharp";
import { readFileSync, writeFileSync, mkdir, existsSync } from "fs";
import type { AstroIntegration } from "astro";
import { globSync } from "glob";
import grayMatter from "gray-matter";
import path from "path";
import { fileURLToPath } from "url";
import { loadDefaultJapaneseParser } from "budoux";
const parser = loadDefaultJapaneseParser();
// 参考: https://zenn.dev/ikuma/scraps/2bd2b9dc3605d7
const generate = async (
title: string,
{
background,
font,
}: {
background: string;
font: Buffer;
},
): Promise<Buffer> => {
const words = parser.parse(title).map((word) => {
// 分かち書きされない文字を分割する
return word.split(/(?<=(?:・|,|スマートフォンで|】|::DateManip))/g);
});
const svg = await satori(
<div
style={{
display: "flex",
justifyContent: "center",
alignItems: "center",
textAlign: "center",
width: 1200,
height: 630,
backgroundImage: `url(${background})`,
backgroundSize: "1200px 630px",
}}
>
<div
style={{
paddingBottom: "80px",
paddingLeft: "30px",
paddingRight: "30px",
display: "flex",
justifyContent: "center",
width: 1040,
height: 390,
fontSize: "50px",
color: "#fff",
flexWrap: "wrap",
textOverflow: "ellipsis",
alignContent: "center",
alignItems: "center",
}}
>
{words.flat().map((word) => {
return <span style={{ display: "block" }}>{word}</span>;
})}
</div>
</div>,
{
width: 1200,
height: 630,
fonts: [
{
name: "NotoSansJP",
data: font,
weight: 900,
style: "normal",
},
],
},
);
const png = await sharp(Buffer.from(svg)).png().toBuffer();
return png;
};
export default (): AstroIntegration => ({
name: "build-ogimages",
hooks: {
"astro:build:done": async ({ dir }) => {
const outputdir = fileURLToPath(dir) + "og-images/";
const filenames = globSync([
"./src/content/posts/**/*.md*",
"./src/content/pages/*.md*",
]);
const data: any = filenames
.map((filename) => {
try {
const markdownWithMeta = readFileSync(filename);
const { data: frontmatter } = grayMatter(markdownWithMeta);
const postid =
frontmatter.postid ?? filename.split(/[.\/]/).slice(-2)[0];
const imgpath = outputdir + postid + ".png";
if (existsSync(imgpath)) {
console.log("\x1b[90m", imgpath + " :skip");
return;
}
return {
postid: postid,
title: frontmatter.title,
};
} catch (e: any) {
console.log(e.message);
}
})
.filter((v) => v);
const background = readFileSync(
path.resolve("./public/assets/images/og-image.png"),
"base64",
);
const font = readFileSync(
path.resolve("./public/assets/fonts/NotoSansJP-Bold.ttf"),
);
if (!existsSync(outputdir)) {
mkdir(outputdir, (e) => {
if (e) console.log(e);
});
}
for (const attr of data) {
const buffer = await generate(attr.title, {
background: `data:image/png;base64,${background}`,
font,
});
const filename = outputdir + attr.postid + ".png";
writeFileSync(filename, buffer);
}
},
},
});