-
-
Notifications
You must be signed in to change notification settings - Fork 541
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: resolve non-latin char issue in generated OG images (#318)
Changes made - update font source with Google font - add error handling in font fetching logic - update docs in dynamic-og-images blog post --------- Co-authored-by: satnaing <[email protected]>
- Loading branch information
1 parent
0440d2a
commit 2f82feb
Showing
6 changed files
with
140 additions
and
52 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,20 @@ | ||
import satori, { type SatoriOptions } from "satori"; | ||
import { Resvg } from "@resvg/resvg-js"; | ||
import { type CollectionEntry } from "astro:content"; | ||
import postOgImage from "./og-templates/post"; | ||
import siteOgImage from "./og-templates/site"; | ||
|
||
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 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", | ||
}, | ||
], | ||
}; | ||
|
||
function svgBufferToPngBuffer(svg: string) { | ||
const resvg = new Resvg(svg); | ||
const pngData = resvg.render(); | ||
return pngData.asPng(); | ||
} | ||
|
||
export async function generateOgImageForPost(post: CollectionEntry<"blog">) { | ||
const svg = await satori(postOgImage(post), options); | ||
const svg = await postOgImage(post); | ||
return svgBufferToPngBuffer(svg); | ||
} | ||
|
||
export async function generateOgImageForSite() { | ||
const svg = await satori(siteOgImage(), options); | ||
const svg = await siteOgImage(); | ||
return svgBufferToPngBuffer(svg); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import type { FontStyle, FontWeight } from "satori"; | ||
|
||
export type FontOptions = { | ||
name: string; | ||
data: ArrayBuffer; | ||
weight: FontWeight | undefined; | ||
style: FontStyle | undefined; | ||
}; | ||
|
||
async function loadGoogleFont( | ||
font: string, | ||
text: string | ||
): Promise<ArrayBuffer> { | ||
const API = `https://fonts.googleapis.com/css2?family=${font}&text=${encodeURIComponent(text)}`; | ||
|
||
const css = await ( | ||
await fetch(API, { | ||
headers: { | ||
"User-Agent": | ||
"Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_8; de-at) AppleWebKit/533.21.1 (KHTML, like Gecko) Version/5.0.5 Safari/533.21.1", | ||
}, | ||
}) | ||
).text(); | ||
|
||
const resource = css.match( | ||
/src: url\((.+)\) format\('(opentype|truetype)'\)/ | ||
); | ||
|
||
if (!resource) throw new Error("Failed to download dynamic font"); | ||
|
||
const res = await fetch(resource[1]); | ||
|
||
if (!res.ok) { | ||
throw new Error("Failed to download dynamic font. Status: " + res.status); | ||
} | ||
|
||
const fonts: ArrayBuffer = await res.arrayBuffer(); | ||
return fonts; | ||
} | ||
|
||
async function loadGoogleFonts( | ||
text: string | ||
): Promise< | ||
Array<{ name: string; data: ArrayBuffer; weight: number; style: string }> | ||
> { | ||
const fontsConfig = [ | ||
{ | ||
name: "IBM Plex Mono", | ||
font: "IBM+Plex+Mono", | ||
weight: 400, | ||
style: "normal", | ||
}, | ||
{ | ||
name: "IBM Plex Mono", | ||
font: "IBM+Plex+Mono:wght@700", | ||
weight: 700, | ||
style: "bold", | ||
}, | ||
]; | ||
|
||
const fonts = await Promise.all( | ||
fontsConfig.map(async ({ name, font, weight, style }) => { | ||
const data = await loadGoogleFont(font, text); | ||
return { name, data, weight, style }; | ||
}) | ||
); | ||
|
||
return fonts; | ||
} | ||
|
||
export default loadGoogleFonts; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters