From 75c5fc0841db71192392358da933c5109d4c341d Mon Sep 17 00:00:00 2001 From: m5r Date: Wed, 15 Feb 2023 19:40:00 +0100 Subject: [PATCH] reuse variable names instead of the names I had guessed --- src/index.ts | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/src/index.ts b/src/index.ts index bd18c60..19fc900 100644 --- a/src/index.ts +++ b/src/index.ts @@ -29,24 +29,28 @@ const languageFontMap = { math: "Noto+Sans+Math", unknown: "Noto+Sans", }; -async function loadGoogleFont(fontFamily: string | string[], segment: string) { - if (!fontFamily || !segment) { +async function loadGoogleFont(fontFamily: string | string[], text: string) { + if (!fontFamily || !text) { return; } - let o = `https://fonts.googleapis.com/css2?family=${fontFamily}&text=${encodeURIComponent(segment)}`, - s = ( - await ( - await fetch(o, { - 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() - ).match(/src: url\((.+)\) format\('(opentype|truetype)'\)/); - if (!s) throw new Error("Failed to load font"); - return fetch(s[1]).then((a) => a.arrayBuffer()); + const API = `https://fonts.googleapis.com/css2?family=${fontFamily}&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 load font"); + } + + return fetch(resource[1]).then((res) => res.arrayBuffer()); } const assetCache = new Map(); const loadDynamicAsset = (emojiType?: EmojiType) => {