Skip to content

Commit

Permalink
brand,typography - new schema with fonts again
Browse files Browse the repository at this point in the history
  • Loading branch information
cscheid committed Sep 10, 2024
1 parent 598e1f7 commit 15fd0ef
Show file tree
Hide file tree
Showing 8 changed files with 1,046 additions and 648 deletions.
71 changes: 40 additions & 31 deletions src/core/brand/brand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ const defaultFontNames: string[] = [
"headings",
// "link",
"monospace",
"monospace-inline",
"monospace-block",
];

const defaultLogoNames: string[] = [
Expand Down Expand Up @@ -86,10 +88,6 @@ export class Brand {
if (base) {
typography.base = base;
}
const emphasis = data.typography?.emphasis;
if (emphasis) {
typography.emphasis = emphasis;
}
const headings = this.getFont("headings");
if (headings) {
typography.headings = headings;
Expand All @@ -102,6 +100,20 @@ export class Brand {
if (monospace) {
typography.monospace = monospace;
}
const monospaceInline = this.getFont("monospace-inline");
if (monospaceInline) {
typography["monospace-inline"] = {
...(monospace ?? {}),
...monospaceInline,
};
}
const monospaceBlock = this.getFont("monospace-block");
if (monospaceBlock) {
typography["monospace-block"] = {
...(monospace ?? {}),
...monospaceBlock,
};
}

const logo: Record<string, BrandStringLightDark> = {};
for (const logoName of Object.keys(data.logo?.with ?? {})) {
Expand Down Expand Up @@ -156,13 +168,22 @@ export class Brand {
}

getFont(name: string): BrandTypographyOptions | null {
if (name === "fonts") {
throw new Error(
"'fonts' is a reserved name in _brand.yml typography definitions",
);
}
const seenValues = new Set<string>();
const defs = new Array<BrandTypographyOptions>();

if (!this.data.typography) {
// alternatively we could provide defaults here
return null;
}
const typography = this.data.typography as Record<
string,
BrandTypographyOptions
>;

// the family field is the key to recurse, finding objects to mergeConfig
// eventually it resolves to an actual font family name which is kept
Expand All @@ -177,9 +198,19 @@ export class Brand {
);
}
seenValues.add(name);
const withFonts = this.data.typography.with as Record<string, BrandFont>;
if (withFonts[name]) {
const value = withFonts[name];
let value: BrandFont | undefined = undefined;
if (!typography[name]) {
return null;
}
const family = typography[name].family;
if (!family) {
console.warn(`font needs family as key: ${name}`);
return null;
}
value = (this.data.typography.fonts ?? []).find(
(x) => typeof x === "string" ? x === family : x.family == family,
);
if (value) {
if (typeof value == "string") {
name = value;
} else if ("files" in value) {
Expand All @@ -204,34 +235,12 @@ export class Brand {
defs.push(value2);
} else {
console.log(
"warning: google font forge not supported for non-html formats",
"warning: online font forge not supported for non-html formats",
);
console.assert("google" in value);
console.assert(value.source === "google" || value.source === "bunny");
const ret = mergeConfigs({ family: name }, ...defs.reverse());
return ret;
// download to (temporary?) directory and populate .files
}
} else if (defaultFontNames.includes(name)) {
const value = this.data.typography[name as BrandNamedFont];
if (!value) {
// alternatively we could provide defaults here
return null;
}
if (typeof value == "string") {
name = value;
} else {
if (!value.family) {
console.warn("font needs family as key", value);
return null;
}
name = value.family;
const value2 = { ...value };
delete value2.family;
defs.push(value2);
}
} else {
const ret = mergeConfigs({ family: name }, ...defs.reverse());
return ret;
}
} while (seenValues.size < 100);
throw new Error(
Expand Down
Loading

0 comments on commit 15fd0ef

Please sign in to comment.