Path: blob/main/src/format/html/format-html-info.ts
6450 views
/*1* format-html-info.ts2*3* functions to obtain information about qmd files in HTML format4*5* Copyright (C) 2020-2022 Posit Software, PBC6*/78import { kBrand, kTheme } from "../../config/constants.ts";9import { isHtmlDashboardOutput, isHtmlOutput } from "../../config/format.ts";10import { Format, Metadata } from "../../config/types.ts";1112export function formatHasBootstrap(format: Format) {13if (14format &&15(isHtmlOutput(format.pandoc, true) ||16isHtmlDashboardOutput(format.identifier["base-format"]))17) {18return hasBootstrapTheme(format.metadata);19} else {20return false;21}22}2324export function hasBootstrapTheme(metadata: Metadata) {25const theme = metadata[kTheme];26return theme !== "none" && theme !== "pandoc";27}2829// Returns a boolean indicating whether dark mode is requested30// (true or false) or undefined if the dark mode support isn't present31// Key order determines whether dark mode is true or false32export function formatDarkMode(format: Format): boolean | undefined {33const isBootstrap = formatHasBootstrap(format);34if (isBootstrap) {35return darkModeDefault(format);36}37return undefined;38}3940// there are two stages of knowing whether dark mode is enabled:41// 1. from the start we can look at theme and brand metadata to see if there is42// a dark theme or brand43// 2. later, after brand is loaded, we'll know whether unified brand enabled dark mode44// in practice, we only know45export function darkModeDefaultMetadata(46metadata: Metadata,47): boolean | undefined {48if (metadata[kTheme] && typeof metadata[kTheme] === "object") {49const keys = Object.keys(metadata[kTheme]);50if (keys.includes("dark")) {51if (keys[0] === "dark") {52return true;53} else {54return false;55}56}57}58if (metadata[kBrand] && typeof metadata[kBrand] === "object") {59const keys = Object.keys(metadata[kBrand]);60if (keys.includes("dark")) {61if (keys[0] === "dark") {62return true;63} else {64return false;65}66}67}68return undefined;69}70export function darkModeDefault(format: Format): boolean | undefined {71const metadata = format.metadata;72if (metadata !== undefined) {73const dmdm = darkModeDefaultMetadata(metadata);74if (dmdm !== undefined) {75return dmdm;76}77}78const brand = format.render.brand;79if (brand && brand.dark) {80// unified brand has no author preference but it can have dark mode81return false;82}83return undefined;84}858687