diff --git a/package.json b/package.json index 9359893..3fd9fd6 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "google-font-metadata", "description": "A metadata generator for Google Fonts.", - "version": "5.2.0", + "version": "5.2.1", "author": "Ayuhito ", "main": "./dist/index.js", "module": "./dist/index.mjs", diff --git a/src/api-gen.ts b/src/api-gen.ts index 8dbff23..d801287 100644 --- a/src/api-gen.ts +++ b/src/api-gen.ts @@ -15,7 +15,8 @@ interface GotResponse { const fetchURL = async (url: string): Promise => { // Have to double assert to please esbuild - const response = (await got(url).json()) as unknown as GotResponse; + // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion + const response = (await got(url).json()) as GotResponse; // Google ships icons into the API, so we have to separate them const stripped = await stripIconsApiGen(response.items); diff --git a/src/api-parser-v2.ts b/src/api-parser-v2.ts index a6423c1..a018d8c 100644 --- a/src/api-parser-v2.ts +++ b/src/api-parser-v2.ts @@ -82,6 +82,7 @@ export const processCSS = ( font: APIResponse, ) => { const id = font.family.replaceAll(/\s/g, '-').toLowerCase(); + const defSubset = font.subsets.includes('latin') ? 'latin' : font.subsets[0]; const fontObject: FontObjectV2 = { [id]: { @@ -92,7 +93,7 @@ export const processCSS = ( styles: [], unicodeRange: {}, variants: {}, - defSubset: font.subsets.includes('latin') ? 'latin' : font.subsets[0], + defSubset, lastModified: font.lastModified, version: font.version, category: font.category, @@ -102,7 +103,7 @@ export const processCSS = ( for (const extension of css) { const rules = compile(extension); - let subset = ''; + let subset = defSubset ?? 'latin'; let fontStyle = ''; let fontWeight = ''; for (const rule of rules) { @@ -186,20 +187,6 @@ export const processCSS = ( fontObject[id].variants[fontWeight] = fontObject[id].variants[fontWeight] || {}; - if (fontStyle && subset && type === 'url') { - fontObject[id].variants[fontWeight][fontStyle] = - fontObject[id].variants[fontWeight][fontStyle] || {}; - - fontObject[id].variants[fontWeight][fontStyle][subset] = - fontObject[id].variants[fontWeight][fontStyle][subset] || { - url: {}, - }; - - fontObject[id].variants[fontWeight][fontStyle][subset].url[ - format - ] = path; - } - // APIv2 splits woff/woff2 files by subset, but uses one combined file for other formats // These don't have a subset if (fontStyle && type === 'url' && !format.startsWith('woff')) { @@ -211,6 +198,19 @@ export const processCSS = ( format ] = path; } + // We do not want to include local fonts + } else if (type === 'url') { + fontObject[id].variants[fontWeight][fontStyle] = + fontObject[id].variants[fontWeight][fontStyle] || {}; + + fontObject[id].variants[fontWeight][fontStyle][subset] = + fontObject[id].variants[fontWeight][fontStyle][subset] || { + url: {}, + }; + + fontObject[id].variants[fontWeight][fontStyle][subset].url[ + format + ] = path; } } } @@ -218,6 +218,16 @@ export const processCSS = ( } } } + + // If unicode-range is empty, but the font has a subset, add a fallback range that covers all characters + if ( + Object.keys(fontObject[id].unicodeRange).length === 0 && + fontObject[id].defSubset + ) { + fontObject[id].unicodeRange[fontObject[id].defSubset] = + 'U+0000-00FF,U+0131,U+0152-0153,U+02BB-02BC,U+02C6,U+02DA,U+02DC,U+0304,U+0308,U+0329,U+2000-206F,U+2074,U+20AC,U+2122,U+2191,U+2193,U+2212,U+2215,U+FEFF,U+FFFD'; + } + return fontObject; }; diff --git a/src/variable-parser.ts b/src/variable-parser.ts index 26c72ca..c2fba2d 100644 --- a/src/variable-parser.ts +++ b/src/variable-parser.ts @@ -198,6 +198,7 @@ export const fetchCSS = async (url: string) => { // [key, css] export const fetchAllCSS = async (links: Links) => + // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion await (Promise.all( Object.keys(links).map(async (key) => [key, await fetchCSS(links[key])]), ) as Promise); // Additional type assertion needed for pkgroll dts plugin @@ -205,7 +206,7 @@ export const fetchAllCSS = async (links: Links) => export const parseCSS = (cssTuple: string[][], defSubset?: string) => { const fontVariants: FontVariantsVariable = {}; - let subset = ''; + let subset = defSubset ?? 'latin'; for (const [key, cssVariant] of cssTuple) { const [fontType, fontStyle] = key.split('.'); const rules = compile(cssVariant);