|
| 1 | +import type { LoadContext, Plugin } from '@docusaurus/types'; |
| 2 | +import { Resvg } from '@resvg/resvg-js'; |
| 3 | +import fs from 'node:fs'; |
| 4 | +import path from 'node:path'; |
| 5 | +import util from 'node:util'; |
| 6 | +import * as React from 'react'; |
| 7 | +import satori from 'satori'; |
| 8 | + |
| 9 | +import OgImage from '../components/OgImage'; |
| 10 | + |
| 11 | +const WIDTH = 1200; |
| 12 | +const HEIGHT = 630; |
| 13 | +const OG_IMAGE_PATH = 'img/og-image.png'; |
| 14 | + |
| 15 | +function findHtmlFiles(dir: string): string[] { |
| 16 | + const results: string[] = []; |
| 17 | + |
| 18 | + for (const entry of fs.readdirSync(dir, { withFileTypes: true })) { |
| 19 | + const fullPath = path.join(dir, entry.name); |
| 20 | + |
| 21 | + if (entry.isDirectory()) { |
| 22 | + results.push(...findHtmlFiles(fullPath)); |
| 23 | + } else if (entry.name === 'index.html') { |
| 24 | + results.push(fullPath); |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + return results; |
| 29 | +} |
| 30 | + |
| 31 | +export default function ogImagePlugin(context: LoadContext): Plugin { |
| 32 | + return { |
| 33 | + name: 'og-image', |
| 34 | + |
| 35 | + async postBuild({ outDir }) { |
| 36 | + const logoSvg = await fs.promises.readFile( |
| 37 | + path.join(context.siteDir, 'static/img/spiro_white.svg'), |
| 38 | + 'utf-8' |
| 39 | + ); |
| 40 | + |
| 41 | + const logoBase64 = `data:image/svg+xml;base64,${Buffer.from(logoSvg).toString('base64')}`; |
| 42 | + |
| 43 | + const svg = await satori(React.createElement(OgImage, { logoBase64 }), { |
| 44 | + width: WIDTH, |
| 45 | + height: HEIGHT, |
| 46 | + fonts: [], |
| 47 | + }); |
| 48 | + |
| 49 | + const resvg = new Resvg(svg, { |
| 50 | + fitTo: { mode: 'width', value: WIDTH }, |
| 51 | + }); |
| 52 | + |
| 53 | + const png = new Uint8Array(resvg.render().asPng()); |
| 54 | + const imgPath = path.join(outDir, OG_IMAGE_PATH); |
| 55 | + await fs.promises.writeFile(imgPath, png); |
| 56 | + |
| 57 | + const ogUrl = `${context.siteConfig.url}/${OG_IMAGE_PATH}`; |
| 58 | + const htmlFiles = findHtmlFiles(outDir); |
| 59 | + |
| 60 | + let injected = 0; |
| 61 | + |
| 62 | + await Promise.all( |
| 63 | + htmlFiles.map(async (htmlFile) => { |
| 64 | + const html = await fs.promises.readFile(htmlFile, 'utf-8'); |
| 65 | + |
| 66 | + if (html.includes('og:image')) return; |
| 67 | + |
| 68 | + const metaTags = [ |
| 69 | + `<meta property="og:image" content="${ogUrl}">`, |
| 70 | + ...(!html.includes('twitter:card') |
| 71 | + ? [`<meta name="twitter:card" content="summary_large_image">`] |
| 72 | + : []), |
| 73 | + `<meta name="twitter:image" content="${ogUrl}">`, |
| 74 | + ].join('\n '); |
| 75 | + |
| 76 | + const updatedHtml = html.replace( |
| 77 | + '</head>', |
| 78 | + ` ${metaTags}\n </head>` |
| 79 | + ); |
| 80 | + await fs.promises.writeFile(htmlFile, updatedHtml); |
| 81 | + |
| 82 | + injected++; |
| 83 | + }) |
| 84 | + ); |
| 85 | + |
| 86 | + console.log( |
| 87 | + `${util.styleText(['magenta', 'bold'], '[og-image]')} Generated image for ${injected} pages` |
| 88 | + ); |
| 89 | + }, |
| 90 | + }; |
| 91 | +} |
0 commit comments