mirror of
https://github.com/ChenQihan666/MyBlog-Next.git
synced 2026-08-14 11:29:33 +08:00
feat: generate og image using templates
This commit is contained in:
@@ -1,8 +1,8 @@
|
|||||||
import satori, { type SatoriOptions } from "satori";
|
import satori, { type SatoriOptions } from "satori";
|
||||||
import { writeFile } from "node:fs/promises";
|
|
||||||
import { Resvg } from "@resvg/resvg-js";
|
import { Resvg } from "@resvg/resvg-js";
|
||||||
import { getCollection } from "astro:content";
|
import { type CollectionEntry } from "astro:content";
|
||||||
import postOgImage from "./og-templates/post";
|
import postOgImage from "./og-templates/post";
|
||||||
|
import siteOgImage from "./og-templates/site";
|
||||||
|
|
||||||
const fetchFonts = async () => {
|
const fetchFonts = async () => {
|
||||||
// Regular Font
|
// Regular Font
|
||||||
@@ -42,42 +42,18 @@ const options: SatoriOptions = {
|
|||||||
],
|
],
|
||||||
};
|
};
|
||||||
|
|
||||||
const generateOgImages = async () => {
|
function svgBufferToPngBuffer(svg: string) {
|
||||||
const postImportResult = await getCollection(
|
const resvg = new Resvg(svg);
|
||||||
"blog",
|
const pngData = resvg.render();
|
||||||
({ data }) => !data.draft
|
return pngData.asPng();
|
||||||
);
|
}
|
||||||
const posts = Object.values(postImportResult).filter(
|
|
||||||
({ data }) => !data.ogImage
|
|
||||||
);
|
|
||||||
|
|
||||||
const imageGenerationPromises = posts.map(async post => {
|
export async function generateOgImageForPost(post: CollectionEntry<"blog">) {
|
||||||
const svg = await satori(postOgImage(post.data.title), options);
|
const svg = await satori(postOgImage(post), options);
|
||||||
|
return svgBufferToPngBuffer(svg);
|
||||||
|
}
|
||||||
|
|
||||||
// render png in production mode
|
export async function generateOgImageForSite() {
|
||||||
if (import.meta.env.MODE === "production") {
|
const svg = await satori(siteOgImage(), options);
|
||||||
const resvg = new Resvg(svg);
|
return svgBufferToPngBuffer(svg);
|
||||||
const pngData = resvg.render();
|
}
|
||||||
const pngBuffer = pngData.asPng();
|
|
||||||
|
|
||||||
const outputImage = `${post.slug}.png`;
|
|
||||||
|
|
||||||
const LIGHT_BLUE = "\x1b[94m";
|
|
||||||
const DARK_GRAY = "\x1b[30m";
|
|
||||||
const RESET = "\x1b[0m";
|
|
||||||
console.info(
|
|
||||||
` ${LIGHT_BLUE}└─${RESET} output png image:`,
|
|
||||||
`${DARK_GRAY}/${outputImage}${RESET}`
|
|
||||||
);
|
|
||||||
|
|
||||||
await writeFile(`./dist/${outputImage}`, pngBuffer);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// Wait for all image generation operations to complete
|
|
||||||
await Promise.all(imageGenerationPromises);
|
|
||||||
|
|
||||||
return null;
|
|
||||||
};
|
|
||||||
|
|
||||||
export default generateOgImages;
|
|
||||||
|
|||||||
@@ -0,0 +1,87 @@
|
|||||||
|
import { SITE } from "@config";
|
||||||
|
|
||||||
|
export default () => {
|
||||||
|
return (
|
||||||
|
<div
|
||||||
|
style={{
|
||||||
|
background: "#fefbfb",
|
||||||
|
width: "100%",
|
||||||
|
height: "100%",
|
||||||
|
display: "flex",
|
||||||
|
alignItems: "center",
|
||||||
|
justifyContent: "center",
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<div
|
||||||
|
style={{
|
||||||
|
position: "absolute",
|
||||||
|
top: "-1px",
|
||||||
|
right: "-1px",
|
||||||
|
border: "4px solid #000",
|
||||||
|
background: "#ecebeb",
|
||||||
|
opacity: "0.9",
|
||||||
|
borderRadius: "4px",
|
||||||
|
display: "flex",
|
||||||
|
justifyContent: "center",
|
||||||
|
margin: "2.5rem",
|
||||||
|
width: "88%",
|
||||||
|
height: "80%",
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
|
||||||
|
<div
|
||||||
|
style={{
|
||||||
|
border: "4px solid #000",
|
||||||
|
background: "#fefbfb",
|
||||||
|
borderRadius: "4px",
|
||||||
|
display: "flex",
|
||||||
|
justifyContent: "center",
|
||||||
|
margin: "2rem",
|
||||||
|
width: "88%",
|
||||||
|
height: "80%",
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<div
|
||||||
|
style={{
|
||||||
|
display: "flex",
|
||||||
|
flexDirection: "column",
|
||||||
|
justifyContent: "space-between",
|
||||||
|
margin: "20px",
|
||||||
|
width: "90%",
|
||||||
|
height: "90%",
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<div
|
||||||
|
style={{
|
||||||
|
display: "flex",
|
||||||
|
flexDirection: "column",
|
||||||
|
justifyContent: "center",
|
||||||
|
alignItems: "center",
|
||||||
|
height: "90%",
|
||||||
|
maxHeight: "90%",
|
||||||
|
overflow: "hidden",
|
||||||
|
textAlign: "center",
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<p style={{ fontSize: 72, fontWeight: "bold" }}>{SITE.title}</p>
|
||||||
|
<p style={{ fontSize: 28 }}>{SITE.desc}</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div
|
||||||
|
style={{
|
||||||
|
display: "flex",
|
||||||
|
justifyContent: "flex-end",
|
||||||
|
width: "100%",
|
||||||
|
marginBottom: "8px",
|
||||||
|
fontSize: 28,
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<span style={{ overflow: "hidden", fontWeight: "bold" }}>
|
||||||
|
{new URL(SITE.website).hostname}
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user