Skip to content

Commit

Permalink
style: handle font family extraction in extractFontFaceData
Browse files Browse the repository at this point in the history
  • Loading branch information
qwerzl committed Mar 11, 2024
1 parent cf23e20 commit cdce965
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
7 changes: 6 additions & 1 deletion src/css/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,14 @@ const extractableKeyMap: Record<string, keyof NormalizedFontFaceData> = {
'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 }

Expand Down
5 changes: 2 additions & 3 deletions src/providers/adobe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}

0 comments on commit cdce965

Please sign in to comment.