mirror of
https://github.com/jackyzha0/quartz.git
synced 2025-12-27 14:54:05 -06:00
refactor(open-graph): dont use async promise inside fetchTtf()
This commit is contained in:
parent
9cc29fccd5
commit
7ee9dc8a21
@ -31,24 +31,30 @@ export async function getSatoriFont(headerFontName: string, bodyFontName: string
|
|||||||
* @param weight what font weight to fetch font
|
* @param weight what font weight to fetch font
|
||||||
* @returns `.ttf` file of google font
|
* @returns `.ttf` file of google font
|
||||||
*/
|
*/
|
||||||
function fetchTtf(fontName: string, weight: FontWeight): Promise<ArrayBuffer> {
|
async function fetchTtf(fontName: string, weight: FontWeight): Promise<ArrayBuffer> {
|
||||||
return new Promise(async (resolve, reject) => {
|
try {
|
||||||
// Get css file from google fonts
|
// Get css file from google fonts
|
||||||
const css = await (
|
const cssResponse = await fetch(`https://fonts.googleapis.com/css?family=${fontName}:${weight}`)
|
||||||
await fetch(`https://fonts.googleapis.com/css?family=${fontName}:${weight}`)
|
const css = await cssResponse.text()
|
||||||
).text()
|
|
||||||
|
|
||||||
// Extract .ttf url from css file
|
// Extract .ttf url from css file
|
||||||
const urlRegex = /url\((https:\/\/fonts.gstatic.com\/s\/.*?.ttf)\)/g
|
const urlRegex = /url\((https:\/\/fonts.gstatic.com\/s\/.*?.ttf)\)/g
|
||||||
const match = urlRegex.exec(css)
|
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)
|
if (!match) {
|
||||||
const fontData = await (await fetch(match[1])).arrayBuffer()
|
throw new Error("Could not fetch font")
|
||||||
resolve(fontData)
|
|
||||||
} else {
|
|
||||||
reject("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 = {
|
export type SocialImageOptions = {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user