-
Notifications
You must be signed in to change notification settings - Fork 331
/
format-reveal-theme.ts
273 lines (242 loc) · 7.42 KB
/
format-reveal-theme.ts
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
/*
* format-reveal-theme.ts
*
* Copyright (C) 2021-2022 Posit Software, PBC
*/
import { dirname, join, relative } from "../../deno_ral/path.ts";
import { existsSync } from "fs/mod.ts";
import { kTheme } from "../../config/constants.ts";
import {
Format,
kTextHighlightingMode,
Metadata,
SassBundleLayers,
SassLayer,
} from "../../config/types.ts";
import { isFileRef } from "../../core/http.ts";
import { pathWithForwardSlashes } from "../../core/path.ts";
import { formatResourcePath } from "../../core/resources.ts";
import {
compileSass,
mergeLayers,
outputVariable,
sassLayerFile,
SassVariable,
sassVariable,
} from "../../core/sass.ts";
import { kCodeBlockHeight, kRevealJsUrl } from "./constants.ts";
import { resolveTextHighlightingLayer } from "../html/format-html-scss.ts";
import { quartoBaseLayer } from "../html/format-html-shared.ts";
import { TempContext } from "../../core/temp.ts";
import { hasAdaptiveTheme } from "../../quarto-core/text-highlighting.ts";
import { copyMinimal, copyTo } from "../../core/copy.ts";
import { titleSlideScss } from "./format-reveal-title.ts";
import { asCssFont, asCssNumber } from "../../core/css.ts";
import { cssHasDarkModeSentinel } from "../../core/pandoc/css.ts";
import { pandocNativeStr } from "../../core/pandoc/codegen.ts";
import { ProjectContext } from "../../project/types.ts";
import { brandRevealSassBundleLayers } from "../../core/sass/brand.ts";
export const kRevealLightThemes = [
"white",
"beige",
"sky",
"serif",
"simple",
"solarized",
];
export const kRevealDarkThemes = [
"black",
"league",
"night",
"blood",
"moon",
];
export const kRevealThemes = [...kRevealLightThemes, ...kRevealDarkThemes];
export async function revealTheme(
format: Format,
input: string,
libDir: string,
temp: TempContext,
project: ProjectContext,
) {
// metadata override to return
const metadata: Metadata = {};
// target revealDir
const revealDir = join(libDir, "revealjs");
// revealurl (optional)
const revealJsUrl = format.metadata[kRevealJsUrl] as string | undefined;
// we don't support remote versions b/c we need to compile the scss
if (revealJsUrl && !isFileRef(revealJsUrl)) {
throw new Error(
"Invalid revealjs-url: " + revealJsUrl +
" (remote urls are not supported)",
);
}
// compute reveal url
const revealUrl = pathWithForwardSlashes(revealDir);
// escape to avoid pandoc markdown parsing from YAML default file
// https://github.com/quarto-dev/quarto-cli/issues/9117
metadata[kRevealJsUrl] = pandocNativeStr(revealUrl).mappedString().value;
// copy reveal dir
const revealSrcDir = revealJsUrl ||
formatResourcePath("revealjs", "reveal");
const revealDestDir = join(dirname(input), libDir, "revealjs");
["dist", "plugin"].forEach((dir) => {
copyMinimal(join(revealSrcDir, dir), join(revealDestDir, dir));
});
// Resolve load paths
const cssThemeDir = join(revealSrcDir, "css", "theme");
const loadPaths = [
join(cssThemeDir, "source"),
join(cssThemeDir, "template"),
];
// theme is either user provided scss or something in our 'themes' dir
// (note that standard reveal scss themes must be converted to quarto
// theme format so they can participate in the pipeline)
const themeConfig =
(format.metadata?.[kTheme] as string | string[] | undefined) || "default";
const themeLayers = (Array.isArray(themeConfig) ? themeConfig : [themeConfig])
.map(
(theme) => {
const themePath = join(relative(Deno.cwd(), dirname(input)), theme);
if (existsSync(themePath)) {
loadPaths.unshift(join(dirname(input), dirname(theme)));
return themeLayer(themePath);
} else {
// alias revealjs theme names
if (theme === "white") {
theme = "default";
} else if (theme === "black") {
theme = "dark";
}
// read theme
theme = formatResourcePath(
"revealjs",
join("themes", `${theme}.scss`),
);
return themeLayer(theme);
}
},
);
// get any variables defined in yaml
const yamlLayer: SassLayer = {
uses: "",
defaults: pandocVariablesToThemeScss(format.metadata, true),
functions: "",
mixins: "",
rules: "",
};
// Inject the highlighting theme, if not adaptive
const highlightingLayer = !hasAdaptiveTheme(format.pandoc)
? resolveTextHighlightingLayer(
input,
format,
"light",
)
: undefined;
const userLayers = [yamlLayer];
if (highlightingLayer) {
userLayers.push(highlightingLayer);
}
userLayers.push(...themeLayers);
// Quarto layers
const quartoLayers = [
quartoBaseLayer(format, true, true, false, true),
quartoLayer(),
];
const titleSlideLayer = titleSlideScss(format);
if (titleSlideLayer) {
userLayers.unshift(titleSlideLayer);
}
// create sass bundle layers
const bundleLayers: SassBundleLayers = {
key: "reveal-theme",
user: mergeLayers(...userLayers),
quarto: mergeLayers(
...quartoLayers,
),
framework: revealFrameworkLayer(revealSrcDir),
loadPaths,
};
const brandLayers: SassBundleLayers[] = await brandRevealSassBundleLayers(
format,
project,
);
// compile sass
const css = await compileSass([bundleLayers, ...brandLayers], temp);
copyTo(
css,
join(revealDestDir, "dist", "theme", "quarto.css"),
);
metadata[kTheme] = "quarto";
const highlightingMode: "light" | "dark" =
cssHasDarkModeSentinel(Deno.readTextFileSync(css)) ? "dark" : "light";
// return
return {
revealUrl,
revealDestDir,
metadata,
[kTextHighlightingMode]: highlightingMode,
};
}
function revealFrameworkLayer(revealDir: string): SassLayer {
const readTemplate = (template: string) => {
return Deno.readTextFileSync(
join(revealDir, "css", "theme", "template", template),
);
};
return {
uses: "",
defaults: "",
functions: "",
mixins: readTemplate("mixins.scss"),
rules: readTemplate("theme.scss"),
};
}
export function pandocVariablesToThemeScss(
metadata: Metadata,
asDefaults = false,
) {
return pandocVariablesToRevealDefaults(metadata).map(
(variable) => {
return outputVariable(variable, asDefaults);
},
).join("\n");
}
function pandocVariablesToRevealDefaults(
metadata: Metadata,
): SassVariable[] {
const explicitVars: SassVariable[] = [];
// Helper for adding explicitly set variables
const add = (
defaults: SassVariable[],
name: string,
value?: unknown,
formatter?: (val: unknown) => unknown,
) => {
if (value) {
const sassVar = sassVariable(name, value, formatter);
defaults.push(sassVar);
}
};
// Pass through to some theme variables variables
add(explicitVars, "font-family-sans-serif", metadata["mainfont"], asCssFont);
add(explicitVars, "font-family-monospace", metadata["monofont"], asCssFont);
add(explicitVars, "presentation-font-size-root", metadata["fontsize"]);
add(
explicitVars,
"presentation-line-height",
metadata["linestretch"],
asCssNumber,
);
add(explicitVars, "code-block-bg", metadata["monobackgroundcolor"]);
// Non-pandoc options from front matter
add(explicitVars, "code-block-height", metadata[kCodeBlockHeight]);
return explicitVars;
}
function quartoLayer(): SassLayer {
return sassLayerFile(formatResourcePath("revealjs", "quarto.scss"));
}
function themeLayer(theme: string): SassLayer {
return sassLayerFile(theme);
}