Skip to content

Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.

only replace parts of family that are separate words during normalization

Oleg Isonen74236cb77c08
diff --git a/packages/fonts/src/font-data.test.ts b/packages/fonts/src/font-data.test.ts
index fd0d357b7d..462f0f6d04 100644
--- a/packages/fonts/src/font-data.test.ts
+++ b/packages/fonts/src/font-data.test.ts
@@ -56,6 +56,7 @@ describe("font-data", () => {
test("basic", () => {
expect(normalizeFamily("Roboto Black", "Black")).toBe("Roboto");
expect(normalizeFamily("Roboto Light", "Light Italic")).toBe("Roboto");
+ expect(normalizeFamily("Robolder Bold", "Bold")).toBe("Robolder");
});
});
});
diff --git a/packages/fonts/src/font-data.ts b/packages/fonts/src/font-data.ts
index 6c2c2d2d5e..94fe4fd200 100644
--- a/packages/fonts/src/font-data.ts
+++ b/packages/fonts/src/font-data.ts
@@ -34,22 +34,21 @@ export const parseSubfamily = (subfamily: string) => {
return { style, weight: Number(weight) };
};
-// Family name can contain additional information like "Roboto Black" or "Roboto Bold", though we need pure family name, because the rest is already encoded in weight and style.
+const splitAndTrim = (string: string) =>
+ string
+ .split(" ")
+ .map((part) => part.trim())
+ .filter(Boolean);
+
+// Family name can contain additional information like "Roboto Black" or "Roboto Bold", though we need pure family name "Roboto", because the rest is already encoded in weight and style.
// We need a name we can reference in CSS font-family property, while CSS matches it with the right font-face considering the weight and style.
export const normalizeFamily = (family: string, subfamily: string) => {
- let simplifiedFamily = family;
- subfamily
- .trim()
- .split(" ")
- .forEach((word: string) => {
- // We need to remove 'Black' or 'black' from 'Roboto Black'
- const index = simplifiedFamily.toLowerCase().indexOf(word.toLowerCase());
- if (index !== -1) {
- const found = simplifiedFamily.slice(index, index + word.length);
- simplifiedFamily = simplifiedFamily.replace(found, "");
- }
- });
- return simplifiedFamily.trim();
+ const familyParts = splitAndTrim(family);
+ const subfamilyParts = splitAndTrim(subfamily.toLowerCase());
+ const familyPartsNormalized = familyParts.filter(
+ (familyPart) => subfamilyParts.includes(familyPart.toLowerCase()) === false
+ );
+ return familyPartsNormalized.join(" ");
};
type FontData = {