-
-
Notifications
You must be signed in to change notification settings - Fork 101
/
og-image.tsx
128 lines (110 loc) · 2.84 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
import * as path from 'path'
import OgImage from './src/components/OgImage'
import * as React from 'react'
import { join } from 'path'
import * as fs from 'fs'
const matter = require('gray-matter')
const sharp = require('sharp')
import satori from 'satori'
const getOutputType = (banner: string) => {
if (banner.endsWith('.png')) {
return 'png'
}
if (banner.endsWith('.jpg') || banner.endsWith('.jpeg')) {
return 'jpeg'
}
}
const baseDirectory = join(__dirname, 'content', 'posts')
const files = fs.readdirSync(baseDirectory)
async function run() {
for (const file of files) {
const filePath = path.join(baseDirectory, file)
const isDirectory = fs.statSync(filePath).isDirectory()
if (isDirectory) {
await convert(file)
}
}
}
async function convert(slug: string) {
// Read the content of the MDX file
const mdxContent = fs.readFileSync(
join(__dirname, 'content', 'posts', slug, 'index.mdx'),
'utf8'
)
// Use gray-matter to parse frontmatter
const { data } = matter(mdxContent)
// Print the frontmatter
console.log('Frontmatter:', data)
const banner = fs.readFileSync(
join(__dirname, 'content', 'posts', slug, data.banner)
)
const metadata = await sharp(Buffer.from(banner)).metadata()
const resizeProps =
metadata.width > metadata.height
? {
width: 700,
}
: { height: 500 }
const resizedBanner = await sharp(banner)
.resize({
...resizeProps,
withoutEnlargement: true,
})
.toBuffer()
const base64Banner = Buffer.from(resizedBanner).toString('base64')
const outputType = getOutputType(data.banner)
// console.log(`data:image/${outputType};base64,${base64Banner}`)
const resizedMetaData = await sharp(
Buffer.from(resizedBanner)
).metadata()
const svg = await satori(
<OgImage
title={data.title}
img={{
src: `data:image/${outputType};base64,${base64Banner}`,
width: resizedMetaData.width,
height: resizedMetaData.height,
}}
/>,
{
width: 1200,
height: 630,
fonts: [
{
name: 'Inter',
// Use `fs` (Node.js only) or `fetch` to read the font as Buffer/ArrayBuffer and provide `data` here.
data: fs.readFileSync(
join(__dirname, 'static', 'fonts', 'Inter-Regular.woff')
),
weight: 400,
style: 'normal',
},
],
}
)
const outputPath = join(
__dirname,
'static',
'og-images',
`${slug}.png`
)
// Convert SVG to PNG
sharp(Buffer.from(svg))
.png({
compressionLevel: 9,
adaptiveFiltering: true,
force: true,
})
.toFile(outputPath, (err, info) => {
if (err) {
console.error(err)
} else {
console.log('Image saved successfully:', info)
}
})
}
try {
run()
} catch (e) {
console.error(e)
}