import type { Plugin } from 'vite' import { cleanupOldOGImages } from '../../../../scripts/cleanup-og-images.js' import { generateFavicons } from '../../../../scripts/generate-favicon.js' import { generateOGImage } from '../../../../scripts/generate-og-image.js' interface OGImagePluginOptions { title?: string description?: string siteName?: string siteUrl?: string } export function ogImagePlugin(options: OGImagePluginOptions = {}): Plugin { const { title = 'Afilmory', description = 'Capturing beautiful moments in life, documenting daily warmth and emotions through my lens.', siteName = 'Afilmory', siteUrl, } = options let ogImagePath = '' return { name: 'og-image-plugin', async buildStart() { // 在构建开始时生成 OG 图片 const timestamp = Date.now() const fileName = `og-image-${timestamp}.png` try { // 生成 favicon await generateFavicons() // 生成 OG 图片 await generateOGImage({ title, description, outputPath: fileName, includePhotos: true, photoCount: 4, }) ogImagePath = `/${fileName}` console.info(`🖼️ OG image generated: ${ogImagePath}`) // 清理旧的 OG 图片 await cleanupOldOGImages(3) } catch (error) { console.error('Failed to generate OG image:', error) } }, transformIndexHtml: { order: 'pre', handler(html) { if (!ogImagePath) { console.warn('⚠️ No OG image path available') return html } // 生成 meta 标签 const metaTags = ` ` // 在 标签前插入 meta 标签 return html.replace('', `${metaTags}\n `) }, }, } }