Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use defsubset if there is no comment in css #133

Merged
merged 3 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>",
"main": "./dist/index.js",
"module": "./dist/index.mjs",
Expand Down
3 changes: 2 additions & 1 deletion src/api-gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ interface GotResponse {

const fetchURL = async (url: string): Promise<void> => {
// 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);

Expand Down
42 changes: 26 additions & 16 deletions src/api-parser-v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]: {
Expand All @@ -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,
Expand All @@ -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) {
Expand Down Expand Up @@ -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')) {
Expand All @@ -211,13 +198,36 @@ 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;
}
}
}
}
}
}
}

// 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;
};

Expand Down
3 changes: 2 additions & 1 deletion src/variable-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,14 +198,15 @@ 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<string[][]>); // Additional type assertion needed for pkgroll dts plugin

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);
Expand Down