-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate-og.js
More file actions
80 lines (60 loc) · 2.51 KB
/
generate-og.js
File metadata and controls
80 lines (60 loc) · 2.51 KB
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
#!/usr/bin/env node
// OG image generator with Puppeteer for PNG conversion
import { existsSync, mkdirSync, readdirSync, statSync } from 'fs'
import { join, dirname } from 'path'
import { fileURLToPath } from 'url'
const __dirname = dirname(fileURLToPath(import.meta.url))
async function generateOGImages() {
console.log('🎨 Starting OG image generation...')
const ogDir = join(__dirname, '.vitepress/dist/og')
// Create og directory if it doesn't exist
if (!existsSync(ogDir)) {
mkdirSync(ogDir, { recursive: true })
}
try {
// First generate HTML templates
const { generateOGImages: generateTemplates } = await import('./.vitepress/og-generator.mjs')
await generateTemplates()
// Try to convert HTML templates to PNG using Puppeteer
try {
const puppeteer = await import('puppeteer')
console.log('🔄 Converting HTML templates to PNG images...')
const browser = await puppeteer.default.launch({
headless: true,
executablePath: process.env.PUPPETEER_EXECUTABLE_PATH || undefined,
args: [
'--no-sandbox',
'--disable-setuid-sandbox',
'--disable-dev-shm-usage',
'--disable-gpu',
'--disable-web-security',
'--disable-features=VizDisplayCompositor'
]
})
const page = await browser.newPage()
await page.setViewport({ width: 1200, height: 630 })
// Find all HTML files in og directory
const ogHtmlFiles = readdirSync(ogDir).filter(file => file.endsWith('.html'))
for (const htmlFile of ogHtmlFiles) {
const htmlPath = join(ogDir, htmlFile)
const pngFile = htmlFile.replace('.html', '.png')
const pngPath = join(ogDir, pngFile)
await page.goto(`file://${htmlPath}`, { waitUntil: 'networkidle0' })
await page.screenshot({ path: pngPath, type: 'png' })
console.log(`Generated: ${pngFile}`)
}
await browser.close()
console.log(`✅ Generated ${ogHtmlFiles.length} PNG images`)
} catch (puppeteerError) {
console.log('📝 Puppeteer not available or failed:', puppeteerError.message)
console.log('📝 HTML templates generated. Install Puppeteer for PNG conversion:')
console.log(' bun add puppeteer')
}
console.log('✅ OG image generation completed!')
} catch (error) {
console.error('❌ Failed to generate OG images:', error.message)
process.exit(1)
}
}
// Run the generator
generateOGImages()