@@ -48,7 +48,7 @@ import { DirectiveCell } from "../core/lib/break-quarto-md-types.ts";
4848import { QuartoJSONSchema , readYamlFromMarkdown } from "../core/yaml.ts" ;
4949import { refSchema } from "../core/lib/yaml-schema/common.ts" ;
5050import { Brand as BrandJson } from "../resources/types/schema-types.ts" ;
51- import { Brand } from "../core/brand/brand.ts" ;
51+ import { Brand , LightDarkBrand } from "../core/brand/brand.ts" ;
5252import { warnOnce } from "../core/log.ts" ;
5353import { assert } from "testing/asserts" ;
5454
@@ -512,7 +512,24 @@ export const ensureFileInformationCache = (
512512export async function projectResolveBrand (
513513 project : ProjectContext ,
514514 fileName ?: string ,
515- ) {
515+ ) : Promise < { light ?: Brand , dark ?: Brand } | undefined > {
516+ async function loadBrand ( brandPath : string ) : Promise < Brand > {
517+ const brand = await readAndValidateYamlFromFile (
518+ brandPath ,
519+ refSchema ( "brand" , "Format-independent brand configuration." ) ,
520+ "Brand validation failed for " + brandPath + "." ,
521+ ) as BrandJson ;
522+ return new Brand ( brand , dirname ( brandPath ) , project . dir ) ;
523+ }
524+ async function loadLocalBrand ( brandPath : string ) : Promise < Brand > {
525+ let resolved : string = "" ;
526+ if ( brandPath . startsWith ( "/" ) ) {
527+ resolved = join ( project . dir , brandPath ) ;
528+ } else {
529+ resolved = join ( dirname ( fileName ! ) , brandPath ) ;
530+ }
531+ return await loadBrand ( resolved ) ;
532+ }
516533 if ( fileName === undefined ) {
517534 if ( project . brandCache ) {
518535 return project . brandCache . brand ;
@@ -538,46 +555,60 @@ export async function projectResolveBrand(
538555 refSchema ( "brand" , "Format-independent brand configuration." ) ,
539556 "Brand validation failed for " + brandPath + "." ,
540557 ) as BrandJson ;
541- project . brandCache . brand = new Brand (
558+ project . brandCache . brand = { light : new Brand (
542559 brand ,
543560 dirname ( brandPath ) ,
544561 project . dir ,
545- ) ;
562+ ) } ;
546563 }
547564 return project . brandCache . brand ;
548565 } else {
549566 const metadata = await project . fileMetadata ( fileName ) ;
550567 if ( metadata . brand === false ) {
551568 return undefined ;
552569 }
553- if ( metadata . brand === true || metadata . brand === undefined ) {
570+ if ( metadata . brand === true || metadata . brand === undefined || metadata . brand === null ) {
554571 return project . resolveBrand ( ) ;
555572 }
556573 const fileInformation = ensureFileInformationCache ( project , fileName ) ;
557574 if ( fileInformation . brand ) {
558575 return fileInformation . brand ;
559576 }
560577 if ( typeof metadata . brand === "string" ) {
561- let brandPath : string = "" ;
562- if ( brandPath . startsWith ( "/" ) ) {
563- brandPath = join ( project . dir , metadata . brand ) ;
564- } else {
565- brandPath = join ( dirname ( fileName ) , metadata . brand ) ;
566- }
567- const brand = await readAndValidateYamlFromFile (
568- brandPath ,
569- refSchema ( "brand" , "Format-independent brand configuration." ) ,
570- "Brand validation failed for " + brandPath + "." ,
571- ) as BrandJson ;
572- fileInformation . brand = new Brand ( brand , dirname ( brandPath ) , project . dir ) ;
578+ fileInformation . brand = { light : await loadLocalBrand ( metadata . brand ) } ;
573579 return fileInformation . brand ;
574580 } else {
575581 assert ( typeof metadata . brand === "object" ) ;
576- fileInformation . brand = new Brand (
577- metadata . brand as BrandJson ,
578- dirname ( fileName ) ,
579- project . dir ,
580- ) ;
582+ if ( ( metadata . brand as LightDarkBrand ) . light || ( metadata . brand as LightDarkBrand ) . dark ) {
583+ let light ;
584+ let ldb = metadata . brand as LightDarkBrand ;
585+ if ( typeof ldb . light === "string" ) {
586+ light = await loadLocalBrand ( ldb . light )
587+ } else {
588+ light = new Brand (
589+ ldb . light as BrandJson ,
590+ dirname ( fileName ) ,
591+ project . dir
592+ ) ;
593+ }
594+ let dark ;
595+ if ( typeof ldb . dark === "string" ) {
596+ dark = await loadLocalBrand ( ldb . dark )
597+ } else {
598+ dark = new Brand (
599+ ldb . dark as BrandJson ,
600+ dirname ( fileName ) ,
601+ project . dir
602+ ) ;
603+ }
604+ fileInformation . brand = { light, dark} ;
605+ } else {
606+ fileInformation . brand = { light : new Brand (
607+ metadata . brand as BrandJson ,
608+ dirname ( fileName ) ,
609+ project . dir ,
610+ ) } ;
611+ }
581612 return fileInformation . brand ;
582613 }
583614 }
0 commit comments