From cdce9657b1677ebaf05a19a798bf50a976623463 Mon Sep 17 00:00:00 2001 From: qwerzl Date: Tue, 12 Mar 2024 07:40:18 +0800 Subject: [PATCH] style: handle font family extraction in `extractFontFaceData` --- src/css/parse.ts | 7 ++++++- src/providers/adobe.ts | 5 ++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/css/parse.ts b/src/css/parse.ts index e627acc..9dcb13d 100644 --- a/src/css/parse.ts +++ b/src/css/parse.ts @@ -13,9 +13,14 @@ const extractableKeyMap: Record = { 'unicode-range': 'unicodeRange', } -export function extractFontFaceData (css: string): NormalizedFontFaceData[] { +export function extractFontFaceData (css: string, family?: string): NormalizedFontFaceData[] { const fontFaces: NormalizedFontFaceData[] = [] + if (family) { + const fontFaceRegex = new RegExp(`@font-face\\s*{[^}]*font-family:\\s*["']${family.toLowerCase()}["'][^}]*}`, 'g'); + css = css.match(fontFaceRegex)!.join('\n') + } + for (const node of findAll(parse(css), node => node.type === 'Atrule' && node.name === 'font-face')) { if (node.type !== 'Atrule' || node.name !== 'font-face') { continue } diff --git a/src/providers/adobe.ts b/src/providers/adobe.ts index 402ec42..3b69334 100644 --- a/src/providers/adobe.ts +++ b/src/providers/adobe.ts @@ -84,8 +84,7 @@ async function getFontDetails (family: string, variants: ResolveFontFacesOptions } if (styles.length === 0) return [] - const fontFaceRegex = new RegExp(`@font-face\\s*{[^}]*font-family:\\s*["']${family.toLowerCase()}["'][^}]*}`, 'g'); - const css = (await fontCSSAPI(`${fonts.kit.id}.css`)).match(fontFaceRegex).join('\n') + const css = await fontCSSAPI(`${fonts.kit.id}.css`) - return addLocalFallbacks(family, extractFontFaceData(css)) + return addLocalFallbacks(family, extractFontFaceData(css, family)) }