import type { AstroIntegration } from "astro"; import fs from "node:fs"; import path from "node:path"; import { fileURLToPath } from "node:url"; export default function ogImageIntegration(): AstroIntegration { return { name: "og-image-per-page", hooks: { "astro:build:done": async ({ dir, pages }) => { const distDir = typeof dir === "string" ? dir : fileURLToPath(dir); for (const page of pages) { const htmlPath = path.join(distDir, page.pathname, "index.html"); if (!fs.existsSync(htmlPath)) continue; let html = fs.readFileSync(htmlPath, "utf-8"); let slug = page.pathname.replace(/^\/|\/$/g, "") || "index"; // URL-safe: replace / with - for nested paths const imageSlug = slug.replace(/\//g, "-"); const ogImageUrl = `https://yasutakeyohei.com/og/${imageSlug}.png?v=2`; html = html.replace( /about-dyslexiaディスレクシアについて<`); fs.writeFileSync(htmlPath, html, "utf-8"); } }, }, }; }