import { render } from "https://deno.land/x/resvg_wasm/mod.ts"
import satori from "npm:satori"
export async function ogImage(body) {
const svg = await satori(
body,
{
width: 1200,
height: 630,
fonts: await loadFonts(),
loadAdditionalAsset: async (code, segment) => {
console.log("loadAdditionalAsset", code, segment)
if (code === "emoji") {
const unicode = segment.codePointAt(0).toString(16).toUpperCase()
return `data:image/svg+xml;base64,` + btoa(await loadEmoji(unicode))
}
return ""
},
},
)
return await render(svg)
}
export const loadFonts = async () => {
const fontsConfig = [
{ name: "Inter", weight: 400, fontFile: "Inter-Regular.otf" },
{ name: "Inter", weight: 600, fontFile: "Inter-SemiBold.otf" },
]
const fontPromises = fontsConfig.map(async (font) => {
const fontUrl = "https://cdn.jsdelivr.net/npm/@tamagui/font-inter@1.108.3/otf/" + font.fontFile
const fontArrayBuf = await fetch(fontUrl).then((res) => res.arrayBuffer())
return { name: font.name, data: fontArrayBuf, weight: font.weight }
})
const fonts: any = await Promise.all(fontPromises)
return fonts
}
const loadEmoji = (code) => {
const api = `https://cdn.jsdelivr.net/gh/shuding/fluentui-emoji-unicode/assets/${code.toLowerCase()}_color.svg`
return fetch(api).then((r) => r.text())
}