Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
quarto-dev
GitHub Repository: quarto-dev/quarto-cli
Path: blob/main/src/format/html/format-html-info.ts
6450 views
1
/*
2
* format-html-info.ts
3
*
4
* functions to obtain information about qmd files in HTML format
5
*
6
* Copyright (C) 2020-2022 Posit Software, PBC
7
*/
8
9
import { kBrand, kTheme } from "../../config/constants.ts";
10
import { isHtmlDashboardOutput, isHtmlOutput } from "../../config/format.ts";
11
import { Format, Metadata } from "../../config/types.ts";
12
13
export function formatHasBootstrap(format: Format) {
14
if (
15
format &&
16
(isHtmlOutput(format.pandoc, true) ||
17
isHtmlDashboardOutput(format.identifier["base-format"]))
18
) {
19
return hasBootstrapTheme(format.metadata);
20
} else {
21
return false;
22
}
23
}
24
25
export function hasBootstrapTheme(metadata: Metadata) {
26
const theme = metadata[kTheme];
27
return theme !== "none" && theme !== "pandoc";
28
}
29
30
// Returns a boolean indicating whether dark mode is requested
31
// (true or false) or undefined if the dark mode support isn't present
32
// Key order determines whether dark mode is true or false
33
export function formatDarkMode(format: Format): boolean | undefined {
34
const isBootstrap = formatHasBootstrap(format);
35
if (isBootstrap) {
36
return darkModeDefault(format);
37
}
38
return undefined;
39
}
40
41
// there are two stages of knowing whether dark mode is enabled:
42
// 1. from the start we can look at theme and brand metadata to see if there is
43
// a dark theme or brand
44
// 2. later, after brand is loaded, we'll know whether unified brand enabled dark mode
45
// in practice, we only know
46
export function darkModeDefaultMetadata(
47
metadata: Metadata,
48
): boolean | undefined {
49
if (metadata[kTheme] && typeof metadata[kTheme] === "object") {
50
const keys = Object.keys(metadata[kTheme]);
51
if (keys.includes("dark")) {
52
if (keys[0] === "dark") {
53
return true;
54
} else {
55
return false;
56
}
57
}
58
}
59
if (metadata[kBrand] && typeof metadata[kBrand] === "object") {
60
const keys = Object.keys(metadata[kBrand]);
61
if (keys.includes("dark")) {
62
if (keys[0] === "dark") {
63
return true;
64
} else {
65
return false;
66
}
67
}
68
}
69
return undefined;
70
}
71
export function darkModeDefault(format: Format): boolean | undefined {
72
const metadata = format.metadata;
73
if (metadata !== undefined) {
74
const dmdm = darkModeDefaultMetadata(metadata);
75
if (dmdm !== undefined) {
76
return dmdm;
77
}
78
}
79
const brand = format.render.brand;
80
if (brand && brand.dark) {
81
// unified brand has no author preference but it can have dark mode
82
return false;
83
}
84
return undefined;
85
}
86
87