mirror of
https://github.com/ChenQihan666/MyBlog-Next.git
synced 2026-08-14 11:29:33 +08:00
fix: update auto-gen OG images to allow special char usage in title
Update the implementation of og image generation code to allow special character usage in title. fix #103, fix #88
This commit is contained in:
@@ -0,0 +1,177 @@
|
||||
import satori, { type SatoriOptions } from "satori";
|
||||
import { SITE } from "@config";
|
||||
import { writeFile } from "node:fs/promises";
|
||||
import { Resvg } from "@resvg/resvg-js";
|
||||
import { getCollection } from "astro:content";
|
||||
|
||||
const fetchFonts = async () => {
|
||||
// Regular Font
|
||||
const fontFileRegular = await fetch(
|
||||
"https://www.1001fonts.com/download/font/ibm-plex-mono.regular.ttf"
|
||||
);
|
||||
const fontRegular: ArrayBuffer = await fontFileRegular.arrayBuffer();
|
||||
|
||||
// Bold Font
|
||||
const fontFileBold = await fetch(
|
||||
"https://www.1001fonts.com/download/font/ibm-plex-mono.bold.ttf"
|
||||
);
|
||||
const fontBold: ArrayBuffer = await fontFileBold.arrayBuffer();
|
||||
|
||||
return { fontRegular, fontBold };
|
||||
};
|
||||
|
||||
const { fontRegular, fontBold } = await fetchFonts();
|
||||
|
||||
const ogImage = (text: string) => {
|
||||
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%",
|
||||
}}
|
||||
>
|
||||
<p
|
||||
style={{
|
||||
fontSize: 72,
|
||||
fontWeight: "bold",
|
||||
maxHeight: "84%",
|
||||
overflow: "hidden",
|
||||
}}
|
||||
>
|
||||
{text}
|
||||
</p>
|
||||
<div
|
||||
style={{
|
||||
display: "flex",
|
||||
justifyContent: "space-between",
|
||||
width: "100%",
|
||||
marginBottom: "8px",
|
||||
fontSize: 28,
|
||||
}}
|
||||
>
|
||||
<span>
|
||||
by{" "}
|
||||
<span
|
||||
style={{
|
||||
color: "transparent",
|
||||
}}
|
||||
>
|
||||
"
|
||||
</span>
|
||||
<span style={{ overflow: "hidden", fontWeight: "bold" }}>
|
||||
{SITE.author}
|
||||
</span>
|
||||
</span>
|
||||
|
||||
<span style={{ overflow: "hidden", fontWeight: "bold" }}>
|
||||
{SITE.title}
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
const options: SatoriOptions = {
|
||||
width: 1200,
|
||||
height: 630,
|
||||
embedFont: true,
|
||||
fonts: [
|
||||
{
|
||||
name: "IBM Plex Mono",
|
||||
data: fontRegular,
|
||||
weight: 400,
|
||||
style: "normal",
|
||||
},
|
||||
{
|
||||
name: "IBM Plex Mono",
|
||||
data: fontBold,
|
||||
weight: 600,
|
||||
style: "normal",
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
const generateOgImages = async () => {
|
||||
const postImportResult = await getCollection(
|
||||
"blog",
|
||||
({ data }) => !data.draft
|
||||
);
|
||||
const posts = Object.values(postImportResult).filter(
|
||||
({ data }) => !data.ogImage
|
||||
);
|
||||
|
||||
const imageGenerationPromises = posts.map(async post => {
|
||||
const svg = await satori(ogImage(post.data.title), options);
|
||||
|
||||
// render png in production mode
|
||||
if (import.meta.env.MODE === "production") {
|
||||
const resvg = new Resvg(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;
|
||||
Reference in New Issue
Block a user