-
Notifications
You must be signed in to change notification settings - Fork 433
Expand file tree
/
Copy pathformat-reveal-theme.ts
More file actions
299 lines (268 loc) · 8.33 KB
/
format-reveal-theme.ts
File metadata and controls
299 lines (268 loc) · 8.33 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
/*
* format-reveal-theme.ts
*
* Copyright (C) 2021-2022 Posit Software, PBC
*/
import { dirname, join, relative } from "../../deno_ral/path.ts";
import { existsSync } from "../../deno_ral/fs.ts";
import { kTheme } from "../../config/constants.ts";
import {
Format,
kTextHighlightingMode,
Metadata,
SassBundleLayers,
SassBundleLayersWithBrand,
SassLayer,
} from "../../config/types.ts";
import { isFileRef } from "../../core/http.ts";
import { pathWithForwardSlashes } from "../../core/path.ts";
import { formatResourcePath, resourcePath } from "../../core/resources.ts";
import {
cleanSourceMappingUrl,
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 { brandRevealSassLayers } from "../../core/sass/brand.ts";
import { md5HashBytes } from "../../core/hash.ts";
export const kRevealLightThemes = [
"white",
"beige",
"sky",
"serif",
"simple",
"solarized",
];
export const kRevealDarkThemes = [
"black",
"league",
"night",
"blood",
"moon",
"dracula",
];
export const kRevealThemes = [...kRevealLightThemes, ...kRevealDarkThemes];
export async function revealTheme(
format: Format,
input: string,
libDir: string,
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"),
];
const brandLayers: SassLayer[] = await brandRevealSassLayers(
input,
format,
project,
);
// 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";
let usedBrandLayers = false;
const themeLayers = (Array.isArray(themeConfig) ? themeConfig : [themeConfig])
.map(
(theme) => {
const themePath = join(relative(Deno.cwd(), dirname(input)), theme);
if (themePath === "brand") {
usedBrandLayers = true;
return brandLayers;
} else 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)];
}
},
).flat();
if (!usedBrandLayers) {
themeLayers.unshift(...brandLayers);
}
// 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(),
quartoRevealBrandLayer(),
];
const titleSlideLayer = titleSlideScss(format);
if (titleSlideLayer) {
userLayers.unshift(titleSlideLayer);
}
// create sass bundle layers
const bundleLayers: SassBundleLayers = {
key: "reveal-theme",
user: userLayers,
quarto: mergeLayers(
...quartoLayers,
),
framework: revealFrameworkLayer(revealSrcDir),
loadPaths,
};
// compile sass
const css = await compileSass([bundleLayers], project);
// Remove sourcemap information
cleanSourceMappingUrl(css);
// convert from string to bytes
const hash = await md5HashBytes(Deno.readFileSync(css));
const fileName = `quarto-${hash}`;
copyTo(
css,
join(revealDestDir, "dist", "theme", `${fileName}.css`),
);
metadata[kTheme] = fileName;
const highlightingMode: "light" | "dark" =
cssHasDarkModeSentinel(Deno.readTextFileSync(css)) ? "dark" : "light";
// return
return {
revealUrl,
revealDestDir,
metadata,
[kTextHighlightingMode]: highlightingMode,
};
}
// Revealjs framework layer is supposed to be more files but:
// - Only mixins.scss and theme.scss are needed here
// - settings.scss is manually included in the quarto.scss file
// - exposer.scss is loaded in theme.scss and found through the loadPaths
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);
}
function quartoRevealBrandLayer(): SassLayer {
return sassLayerFile(
resourcePath(join("formats", "revealjs", "brand", "brand.scss")),
);
}