From 7ee9dc8a2160f6257d6f2c3c538cdac6b1bd87ff Mon Sep 17 00:00:00 2001 From: Ben Schlegel Date: Sun, 10 Nov 2024 15:09:39 +0100 Subject: [PATCH] refactor(open-graph): dont use async promise inside `fetchTtf()` --- quartz/util/imageHelper.ts | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/quartz/util/imageHelper.ts b/quartz/util/imageHelper.ts index 5d587fd9b..06e12cb7a 100644 --- a/quartz/util/imageHelper.ts +++ b/quartz/util/imageHelper.ts @@ -31,24 +31,30 @@ export async function getSatoriFont(headerFontName: string, bodyFontName: string * @param weight what font weight to fetch font * @returns `.ttf` file of google font */ -function fetchTtf(fontName: string, weight: FontWeight): Promise { - return new Promise(async (resolve, reject) => { +async function fetchTtf(fontName: string, weight: FontWeight): Promise { + try { // Get css file from google fonts - const css = await ( - await fetch(`https://fonts.googleapis.com/css?family=${fontName}:${weight}`) - ).text() + const cssResponse = await fetch(`https://fonts.googleapis.com/css?family=${fontName}:${weight}`) + const css = await cssResponse.text() // Extract .ttf url from css file const urlRegex = /url\((https:\/\/fonts.gstatic.com\/s\/.*?.ttf)\)/g const match = urlRegex.exec(css) - if (match) { - // fontData is an ArrayBuffer containing the .ttf file data (get match[1] due to google fonts response format, always contains link twice, but second entry is the "raw" link) - const fontData = await (await fetch(match[1])).arrayBuffer() - resolve(fontData) - } else { - reject("Could not fetch font") + + if (!match) { + throw new Error("Could not fetch font") } - }) + + // Retrieve font data as ArrayBuffer + const fontResponse = await fetch(match[1]) + + // fontData is an ArrayBuffer containing the .ttf file data (get match[1] due to google fonts response format, always contains link twice, but second entry is the "raw" link) + const fontData = await fontResponse.arrayBuffer() + + return fontData + } catch (error) { + throw new Error(`Error fetching font: ${error}`) + } } export type SocialImageOptions = {