-
Notifications
You must be signed in to change notification settings - Fork 433
Expand file tree
/
Copy pathbrand.ts
More file actions
650 lines (621 loc) · 19 KB
/
brand.ts
File metadata and controls
650 lines (621 loc) · 19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
/*
* brand.ts
*
* Class that implements support for `_brand.yml` data in Quarto
*
* Copyright (C) 2024 Posit Software, PBC
*/
import {
BrandColorLightDark,
BrandFont,
BrandLogoExplicitResource,
BrandLogoResource,
BrandLogoSingle,
BrandLogoUnified,
BrandNamedLogo,
BrandNamedThemeColor,
BrandSingle,
BrandStringLightDark,
BrandTypographyOptionsBase,
BrandTypographyOptionsHeadingsSingle,
BrandTypographySingle,
BrandTypographyUnified,
BrandUnified,
LogoLightDarkSpecifier,
LogoOptions,
NormalizedLogoLightDarkSpecifier,
Zod,
} from "../../resources/types/zod/schema-types.ts";
import { InternalError } from "../lib/error.ts";
import { join, relative } from "../../deno_ral/path.ts";
import { warnOnce } from "../log.ts";
import { isCssColorName } from "../css/color-names.ts";
import { assert } from "testing/asserts";
type ProcessedBrandData = {
color: Record<string, string>;
typography: BrandTypographySingle;
logo: {
small?: BrandLogoExplicitResource;
medium?: BrandLogoExplicitResource;
large?: BrandLogoExplicitResource;
images: Record<string, BrandLogoExplicitResource>;
};
};
export class Brand {
data: BrandSingle;
brandDir: string;
projectDir: string;
processedData: ProcessedBrandData;
constructor(
readonly brand: unknown,
brandDir: string,
projectDir: string,
) {
this.data = Zod.BrandSingle.parse(brand);
this.brandDir = brandDir;
this.projectDir = projectDir;
this.processedData = this.processData(this.data);
}
processData(data: BrandSingle): ProcessedBrandData {
const color: Record<string, string> = {};
for (const colorName of Object.keys(data.color?.palette ?? {})) {
color[colorName] = this.getColor(colorName);
}
for (const colorName of Object.keys(data.color ?? {})) {
if (colorName === "palette") {
continue;
}
color[colorName] = this.getColor(colorName);
}
const typography: BrandTypographySingle = {};
const base = this.getFont("base");
if (base) {
typography.base = base;
}
const headings = this.getFont("headings");
if (headings) {
typography.headings = headings;
}
const link = data.typography?.link;
if (link) {
typography.link = link;
}
let monospace = this.getFont("monospace");
let monospaceInline = this.getFont("monospace-inline");
let monospaceBlock = this.getFont("monospace-block");
if (monospace) {
if (typeof monospace === "string") {
monospace = { family: monospace };
}
typography.monospace = monospace;
}
if (monospaceInline && typeof monospaceInline === "string") {
monospaceInline = { family: monospaceInline };
}
if (monospaceBlock && typeof monospaceBlock === "string") {
monospaceBlock = { family: monospaceBlock };
}
// cut off control flow here so the type checker knows these
// are not strings
if (typeof monospace === "string") {
throw new InternalError("should never happen");
}
if (typeof monospaceInline === "string") {
throw new InternalError("should never happen");
}
if (typeof monospaceBlock === "string") {
throw new InternalError("should never happen");
}
if (monospace || monospaceInline) {
typography["monospace-inline"] = {
...(monospace ?? {}),
...(monospaceInline ?? {}),
};
}
if (monospaceBlock) {
if (typeof monospaceBlock === "string") {
monospaceBlock = { family: monospaceBlock };
}
}
if (monospace || monospaceBlock) {
typography["monospace-block"] = {
...(monospace ?? {}),
...(monospaceBlock ?? {}),
};
}
const logo: ProcessedBrandData["logo"] = { images: {} };
for (
const size of Zod.BrandNamedLogo.options
) {
const v = this.getLogo(size);
if (v) {
logo[size] = v;
}
}
for (const [key, value] of Object.entries(data.logo?.images ?? {})) {
logo.images[key] = this.resolvePath(value);
}
return {
color,
typography,
logo,
};
}
// semantics of name resolution for colors:
// - if the name is in the "palette" key, use that value as they key for a recursive call (so color names can be aliased or redefined away from scss defaults)
// - if the name is a default color name, call getColor recursively (so defaults can use named values)
// - otherwise, assume it's a color value and return it
getColor(name: string, quiet = false): string {
const seenValues = new Set<string>();
do {
if (seenValues.has(name)) {
throw new Error(
`Circular reference in _brand.yml color definitions: ${
Array.from(seenValues).join(
" -> ",
)
}`,
);
}
seenValues.add(name);
if (this.data.color?.palette?.[name]) {
name = this.data.color.palette[name] as string;
} else if (
Zod.BrandNamedThemeColor.options.includes(
name as BrandNamedThemeColor,
) &&
this.data.color?.[name as BrandNamedThemeColor]
) {
name = this.data.color[name as BrandNamedThemeColor]!;
} else {
// if the name is not a default color name, assume it's a color value
if (!isCssColorName(name) && !quiet) {
warnOnce(
`"${name}" is not a valid CSS color name.\nThis might cause SCSS compilation to fail, or the color to have no effect.`,
);
}
return name;
}
} while (seenValues.size < 100); // 100 ought to be enough for anyone, with apologies to Bill Gates
throw new Error(
"Recursion depth exceeded 100 in _brand.yml color definitions",
);
}
getFont(
name: string,
):
| BrandTypographyOptionsBase
| BrandTypographyOptionsHeadingsSingle
| undefined {
if (!this.data.typography) {
return undefined;
}
const typography = this.data.typography;
switch (name) {
case "base":
return typography.base;
case "headings":
return typography.headings;
case "link":
return typography.link;
case "monospace":
return typography.monospace;
case "monospace-inline":
return typography["monospace-inline"];
case "monospace-block":
return typography["monospace-block"];
}
return undefined;
}
getFontResources(name: string): BrandFont[] {
if (name === "fonts") {
throw new Error(
"'fonts' is a reserved name in _brand.yml typography definitions",
);
}
if (!this.data.typography) {
return [];
}
const typography = this.data.typography;
const fonts = typography.fonts;
return fonts ?? [];
}
resolvePath(entry: BrandLogoResource) {
const pathPrefix = relative(this.projectDir, this.brandDir);
if (typeof entry === "string") {
return { path: join(pathPrefix, entry) };
}
return {
...entry,
path: join(pathPrefix, entry.path),
};
}
getLogoResource(name: string): BrandLogoExplicitResource {
const entry = this.data.logo?.images?.[name];
if (!entry) {
return { path: name };
}
return this.resolvePath(entry);
}
getLogo(name: BrandNamedLogo): BrandLogoExplicitResource | undefined {
const entry = this.data.logo?.[name];
if (!entry) {
return undefined;
}
return this.getLogoResource(entry);
}
}
export type LightDarkBrand = {
light?: Brand;
dark?: Brand;
};
export type LightDarkBrandDarkFlag = {
light?: Brand;
dark?: Brand;
enablesDarkMode: boolean;
};
export type LightDarkColor = {
light?: string;
dark?: string;
};
export const getFavicon = (brand: Brand): string | undefined => {
const logoInfo = brand.getLogo("small");
if (!logoInfo) {
return undefined;
}
return logoInfo.path;
};
export function resolveLogo(
brand: LightDarkBrand | undefined,
spec: LogoLightDarkSpecifier | undefined,
order: BrandNamedLogo[],
): NormalizedLogoLightDarkSpecifier | undefined {
const resolveBrandLogo = (
mode: "light" | "dark",
name: string,
): LogoOptions => {
const logo = brand?.[mode]?.processedData?.logo;
return logo &&
((Zod.BrandNamedLogo.options.includes(name as BrandNamedLogo) &&
logo[name as BrandNamedLogo]) || logo.images[name]) ||
{ path: name };
};
function findLogo(
mode: "light" | "dark",
order: BrandNamedLogo[],
): LogoOptions | undefined {
if (brand?.[mode]) {
for (const size of order) {
const logo = brand[mode].processedData.logo[size];
if (logo) {
return logo;
}
}
}
return undefined;
}
const resolveLogoOptions = (
mode: "light" | "dark",
logo: LogoOptions,
): LogoOptions => {
const logo2 = resolveBrandLogo(mode, logo.path);
if (logo2) {
const { path: _, ...rest } = logo;
return {
...logo2,
...rest,
};
}
return logo;
};
if (!spec) {
return {
light: findLogo("light", order) || findLogo("dark", order),
dark: findLogo("dark", order) || findLogo("light", order),
};
}
if (typeof spec === "string") {
return {
light: resolveBrandLogo("light", spec),
dark: resolveBrandLogo("light", spec),
};
}
if ("path" in spec) {
return {
light: resolveLogoOptions("light", spec),
dark: resolveLogoOptions("dark", spec),
};
}
let light, dark;
if (!spec.light) {
light = findLogo("light", order);
} else if (typeof spec.light === "string") {
light = resolveBrandLogo("light", spec.light);
} else {
light = resolveLogoOptions("light", spec.light);
}
if (!spec.dark) {
dark = findLogo("dark", order);
} else if (typeof spec.dark === "string") {
dark = resolveBrandLogo("dark", spec.dark);
} else {
dark = resolveLogoOptions("dark", spec.dark);
}
// light logo default to dark logo if no light logo specified
if (!light && dark) {
light = { ...dark };
}
// dark logo default to light logo if no dark logo specified
// and dark mode is enabled
if (!dark && light && brand && brand.dark) {
dark = { ...light };
}
return {
light,
dark,
};
}
function splitColorLightDark(
bcld: BrandColorLightDark,
): LightDarkColor {
if (typeof bcld === "string") {
return { light: bcld, dark: bcld };
}
return bcld;
}
const enablesDarkMode = (x: BrandColorLightDark | BrandStringLightDark) =>
typeof x === "object" && x?.dark;
export function brandHasDarkMode(brand: BrandUnified): boolean {
if (brand.color) {
for (const colorName of Zod.BrandNamedThemeColor.options) {
if (!brand.color[colorName]) {
continue;
}
if (enablesDarkMode(brand.color![colorName])) {
return true;
}
}
}
if (brand.typography) {
for (const elementName of Zod.BrandNamedTypographyElements.options) {
const element = brand.typography[elementName];
if (!element || typeof element === "string") {
continue;
}
if (
"background-color" in element && element["background-color"] &&
enablesDarkMode(element["background-color"])
) {
return true;
}
if (
"color" in element && element["color"] &&
enablesDarkMode(element["color"])
) {
return true;
}
}
}
if (brand.logo) {
for (const logoName of Zod.BrandNamedLogo.options) {
const logo = brand.logo[logoName];
if (!logo || typeof logo === "string") {
continue;
}
if (enablesDarkMode(logo)) {
return true;
}
}
}
return false;
}
function sharedTypography(
unified: BrandTypographyUnified,
): BrandTypographySingle {
const ret: BrandTypographySingle = {
fonts: unified.fonts,
};
for (const elementName of Zod.BrandNamedTypographyElements.options) {
if (!unified[elementName]) {
continue;
}
if (typeof unified[elementName] === "string") {
ret[elementName] = unified[elementName];
continue;
}
ret[elementName] = Object.fromEntries(
Object.entries(unified[elementName]).filter(
([key, _]) => !["color", "background-color"].includes(key),
),
);
}
return ret;
}
function splitLogo(
unifiedLogo: BrandLogoUnified,
): { light: BrandLogoSingle; dark: BrandLogoSingle } {
const light: BrandLogoSingle = { images: unifiedLogo.images },
dark: BrandLogoSingle = { images: unifiedLogo.images };
for (const logoName of Zod.BrandNamedLogo.options) {
if (unifiedLogo[logoName]) {
if (typeof unifiedLogo[logoName] === "string") {
light[logoName] = dark[logoName] = unifiedLogo[logoName];
continue;
}
({ light: light[logoName], dark: dark[logoName] } =
unifiedLogo[logoName]);
}
}
return { light, dark };
}
export function splitUnifiedBrand(
unified: unknown,
brandDir: string,
projectDir: string,
): LightDarkBrandDarkFlag {
const unifiedBrand: BrandUnified = Zod.BrandUnified.parse(unified);
let typography: BrandTypographySingle | undefined = undefined;
let headingsColor: LightDarkColor | undefined = undefined;
let monospaceColor: LightDarkColor | undefined = undefined;
let monospaceBackgroundColor: LightDarkColor | undefined = undefined;
let monospaceInlineColor: LightDarkColor | undefined = undefined;
let monospaceInlineBackgroundColor: LightDarkColor | undefined = undefined;
let monospaceBlockColor: LightDarkColor | undefined = undefined;
let monospaceBlockBackgroundColor: LightDarkColor | undefined = undefined;
let linkColor: LightDarkColor | undefined = undefined;
let linkBackgroundColor: LightDarkColor | undefined = undefined;
if (unifiedBrand.typography) {
typography = sharedTypography(unifiedBrand.typography);
if (
unifiedBrand.typography.headings &&
typeof unifiedBrand.typography.headings !== "string" &&
unifiedBrand.typography.headings.color
) {
headingsColor = splitColorLightDark(
unifiedBrand.typography.headings.color,
);
}
if (
unifiedBrand.typography.monospace &&
typeof unifiedBrand.typography.monospace !== "string"
) {
if (unifiedBrand.typography.monospace.color) {
monospaceColor = splitColorLightDark(
unifiedBrand.typography.monospace.color,
);
}
if (unifiedBrand.typography.monospace["background-color"]) {
monospaceBackgroundColor = splitColorLightDark(
unifiedBrand.typography.monospace["background-color"],
);
}
}
if (
unifiedBrand.typography["monospace-inline"] &&
typeof unifiedBrand.typography["monospace-inline"] !== "string"
) {
if (unifiedBrand.typography["monospace-inline"].color) {
monospaceInlineColor = splitColorLightDark(
unifiedBrand.typography["monospace-inline"].color,
);
}
if (unifiedBrand.typography["monospace-inline"]["background-color"]) {
monospaceInlineBackgroundColor = splitColorLightDark(
unifiedBrand.typography["monospace-inline"]["background-color"],
);
}
}
if (
unifiedBrand.typography["monospace-block"] &&
typeof unifiedBrand.typography["monospace-block"] !== "string"
) {
if (unifiedBrand.typography["monospace-block"].color) {
monospaceBlockColor = splitColorLightDark(
unifiedBrand.typography["monospace-block"].color,
);
}
if (unifiedBrand.typography["monospace-block"]["background-color"]) {
monospaceBlockBackgroundColor = splitColorLightDark(
unifiedBrand.typography["monospace-block"]["background-color"],
);
}
}
if (
unifiedBrand.typography.link &&
typeof unifiedBrand.typography.link !== "string"
) {
if (unifiedBrand.typography.link.color) {
linkColor = splitColorLightDark(
unifiedBrand.typography.link.color,
);
}
if (unifiedBrand.typography.link["background-color"]) {
linkBackgroundColor = splitColorLightDark(
unifiedBrand.typography.link["background-color"],
);
}
}
}
const specializeTypography = (
typography: BrandTypographySingle,
mode: "light" | "dark",
) =>
typography && {
fonts: typography.fonts && [...typography.fonts],
base: !typography.base || typeof typography.base === "string"
? typography.base
: { ...typography.base },
headings: !typography.headings || typeof typography.headings === "string"
? typography.headings
: {
...typography.headings,
color: headingsColor && headingsColor[mode],
},
monospace:
!typography.monospace || typeof typography.monospace === "string"
? typography.monospace
: {
...typography.monospace,
color: monospaceColor && monospaceColor[mode],
"background-color": monospaceBackgroundColor &&
monospaceBackgroundColor[mode],
},
"monospace-inline": !typography["monospace-inline"] ||
typeof typography["monospace-inline"] === "string"
? typography["monospace-inline"]
: {
...typography["monospace-inline"],
color: monospaceInlineColor && monospaceInlineColor[mode],
"background-color": monospaceInlineBackgroundColor &&
monospaceInlineBackgroundColor[mode],
},
"monospace-block": !typography["monospace-block"] ||
typeof typography["monospace-block"] === "string"
? typography["monospace-block"]
: {
...typography["monospace-block"],
color: monospaceBlockColor && monospaceBlockColor[mode],
"background-color": monospaceBlockBackgroundColor &&
monospaceBlockBackgroundColor[mode],
},
link: !typography.link || typeof typography.link === "string"
? typography.link
: {
...typography.link,
color: linkColor && linkColor[mode],
"background-color": linkBackgroundColor &&
linkBackgroundColor[mode],
},
};
const logos = unifiedBrand.logo && splitLogo(unifiedBrand.logo);
const lightBrand: BrandSingle = {
meta: unifiedBrand.meta,
color: { palette: unifiedBrand.color && { ...unifiedBrand.color.palette } },
typography: typography && specializeTypography(typography, "light"),
logo: logos && logos.light,
defaults: unifiedBrand.defaults,
};
const darkBrand: BrandSingle = {
meta: unifiedBrand.meta,
color: { palette: unifiedBrand.color && { ...unifiedBrand.color.palette } },
typography: typography && specializeTypography(typography, "dark"),
logo: logos && logos.dark,
defaults: unifiedBrand.defaults,
};
if (unifiedBrand.color) {
for (const colorName of Zod.BrandNamedThemeColor.options) {
if (!unifiedBrand.color[colorName]) {
continue;
}
({
light: lightBrand.color![colorName],
dark: darkBrand.color![colorName],
} = splitColorLightDark(unifiedBrand.color![colorName]));
}
}
return {
light: new Brand(lightBrand, brandDir, projectDir),
dark: new Brand(darkBrand, brandDir, projectDir),
enablesDarkMode: brandHasDarkMode(unifiedBrand),
};
}