-
Notifications
You must be signed in to change notification settings - Fork 431
Expand file tree
/
Copy pathformat-html-shared.ts
More file actions
553 lines (486 loc) · 13.5 KB
/
format-html-shared.ts
File metadata and controls
553 lines (486 loc) · 13.5 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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
/*
* format-html-shared.ts
*
* Copyright (C) 2020-2022 Posit Software, PBC
*/
import { dirname, join, relative } from "../../deno_ral/path.ts";
import { outputVariable, sassLayer, sassVariable } from "../../core/sass.ts";
import {
kCapLoc,
kCapTop,
kCitationLocation,
kCodeOverflow,
kCopyButtonTooltip,
kFigCapLoc,
kLinkExternalIcon,
kReferenceLocation,
kSectionTitleFootnotes,
kSectionTitleReferences,
kTblCapLoc,
} from "../../config/constants.ts";
import {
Format,
FormatDependency,
FormatLanguage,
PandocFlags,
} from "../../config/types.ts";
import { formatResourcePath } from "../../core/resources.ts";
import { Document, Element } from "../../core/deno-dom.ts";
import { normalizePath } from "../../core/path.ts";
// features that are enabled by default for 'html'. setting
// all of these to false will yield the minimal html output
// that quarto can produce (there is still some CSS we generate
// to provide figure layout, etc.). you can also set the
// 'minimal' option to do this in one shot
export const kTabsets = "tabsets";
export const kCodeCopy = "code-copy";
export const kAnchorSections = "anchor-sections";
export const kCitationsHover = "citations-hover";
export const kFootnotesHover = "footnotes-hover";
export const kXrefsHover = "crossrefs-hover";
export const kSmoothScroll = "smooth-scroll";
// Code Annotation
export const kCodeAnnotations = "code-annotations";
// turn off optional html features as well as all themes
export const kMinimal = "minimal";
export const kPageLayout = "page-layout";
export const kPageLayoutArticle = "article";
export const kPageLayoutCustom = "custom";
export const kPageLayoutFull = "full";
export const kComments = "comments";
export const kHypothesis = "hypothesis";
export const kUtterances = "utterances";
export const kGiscus = "giscus";
export const kAxe = "axe";
export const kSectionCollapse = "section-collapse";
export const kGiscusRepoId = "repo-id";
export const kGiscusCategoryId = "category-id";
export const kDraft = "draft";
export const kAppendixStyle = "appendix-style";
export const kAppendixCiteAs = "appendix-cite-as";
export const kLicense = "license";
export const kCopyright = "copyright";
export const kCitation = "citation";
export const kDocumentCss = "document-css";
export const kBootstrapDependencyName = "bootstrap";
export const clipboardDependency = () => {
const dependency: FormatDependency = { name: "clipboard" };
dependency.scripts = [];
dependency.scripts.push({
name: "clipboard.min.js",
path: formatResourcePath("html", join("clipboard", "clipboard.min.js")),
});
return dependency;
};
export const bootstrapFunctions = () => {
return Deno.readTextFileSync(
join(bootstrapResourceDir(), "_functions.scss"),
);
};
export const bootstrapMixins = () => {
return Deno.readTextFileSync(
join(bootstrapResourceDir(), "_mixins.scss"),
);
};
export const bootstrapVariables = () => {
return Deno.readTextFileSync(
join(bootstrapResourceDir(), "_variables.scss"),
);
};
export const bootstrapRules = () => {
return Deno.readTextFileSync(
join(bootstrapResourceDir(), "bootstrap.scss"),
);
};
export const bslibComponentMixins = () => {
const bootstrapDistDir = formatResourcePath(
"html",
"bslib",
);
const mixinsDir = join(bootstrapDistDir, "components", "scss", "mixins");
return Deno.readTextFileSync(join(mixinsDir, "_mixins.scss"));
};
export const bslibComponentRules = () => {
const bslibDistDir = formatResourcePath(
"html",
join("bslib"),
);
const bslibDirs = [
join(bslibDistDir, "bslib-scss"),
join(bslibDistDir, "components", "scss"),
];
const scss = [];
for (const bsLibDir of bslibDirs) {
for (
const walk of Deno.readDirSync(bsLibDir)
) {
if (walk.isFile) {
const contents = Deno.readTextFileSync(join(bsLibDir, walk.name));
scss.push(contents);
}
}
}
return scss.join("\n");
};
export const htmlToolsRules = () => {
const htmlToolsDir = formatResourcePath(
"html",
join("htmltools"),
);
const fillCss = Deno.readTextFileSync(join(htmlToolsDir, "fill.css"));
return fillCss;
};
export const bootstrapResourceDir = () => {
return formatResourcePath(
"html",
join("bootstrap", "dist", "scss"),
);
};
export const bslibResourceDir = () => {
return formatResourcePath(
"html",
join("bslib", "bslib-scss"),
);
};
export const sassUtilFunctions = (name: string) => {
const bootstrapDistDir = formatResourcePath(
"html",
join("bootstrap", "dist"),
);
const path = join(bootstrapDistDir, "sass-utils", name);
return Deno.readTextFileSync(path);
};
export const quartoRules = () =>
Deno.readTextFileSync(formatResourcePath(
"html",
"_quarto-rules.scss",
));
export const quartoCopyCodeDefaults = () =>
Deno.readTextFileSync(formatResourcePath(
"html",
"_quarto-variables-copy-code.scss",
));
export const quartoCopyCodeRules = () =>
Deno.readTextFileSync(formatResourcePath(
"html",
"_quarto-rules-copy-code.scss",
));
export const quartoLinkExternalRules = () =>
Deno.readTextFileSync(formatResourcePath(
"html",
"_quarto-rules-link-external.scss",
));
export const quartoCodeFilenameRules = () =>
Deno.readTextFileSync(formatResourcePath(
"html",
"_quarto-rules-code-filename.scss",
));
export const quartoTabbyRules = () =>
Deno.readTextFileSync(formatResourcePath(
"html",
"_quarto-rules-tabby.scss",
));
export const quartoFigResponsiveRules = () => {
return [
".img-fluid {",
" max-width: 100%;",
" height: auto;",
"}",
].join("\n");
};
export const quartoGlobalCssVariableRules = () => {
return `
$font-family-monospace: SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace !default;
/*! quarto-variables-start */
:root {
--quarto-font-monospace: #{inspect($font-family-monospace)};
}
/*! quarto-variables-end */
`;
};
export const quartoBootstrapCustomizationLayer = () => {
const path = formatResourcePath(
"html",
join("bootstrap", "_bootstrap-customize.scss"),
);
return sassLayer(path);
};
export const quartoBootstrapRules = () =>
Deno.readTextFileSync(formatResourcePath(
"html",
join("bootstrap", "_bootstrap-rules.scss"),
));
export const quartoBootstrapMixins = () =>
Deno.readTextFileSync(formatResourcePath(
"html",
join("bootstrap", "_bootstrap-mixins.scss"),
));
export const quartoBootstrapFunctions = () =>
Deno.readTextFileSync(formatResourcePath(
"html",
join("bootstrap", "_bootstrap-functions.scss"),
));
export const quartoBaseLayer = (
format: Format,
codeCopy = false,
tabby = false,
figResponsive = false,
codeFilename = false,
) => {
const rules: string[] = [quartoRules()];
const defaults: string[] = [quartoDefaults(format), quartoVariables()];
if (codeCopy) {
rules.push(quartoCopyCodeRules());
defaults.push(quartoCopyCodeDefaults());
}
if (tabby) {
rules.push(quartoTabbyRules());
}
if (figResponsive) {
rules.push(quartoFigResponsiveRules());
}
if (codeFilename) {
rules.push(quartoCodeFilenameRules());
}
if (format.render[kLinkExternalIcon]) {
rules.push(quartoLinkExternalRules());
}
return {
uses: quartoUses(),
defaults: defaults.join("\n"),
functions: quartoFunctions(),
mixins: "",
rules: rules.join("\n"),
};
};
export const quartoVariables = () =>
Deno.readTextFileSync(formatResourcePath(
"html",
"_quarto-variables.scss",
));
export const quartoUses = () =>
Deno.readTextFileSync(formatResourcePath(
"html",
"_quarto-uses.scss",
));
export const quartoFunctions = () =>
Deno.readTextFileSync(formatResourcePath(
"html",
"_quarto-functions.scss",
));
export const quartoDefaults = (format: Format) => {
const defaults: string[] = [];
defaults.push(
outputVariable(
sassVariable(
"code-copy-selector",
format.metadata[kCodeCopy] === undefined ||
format.metadata[kCodeCopy] === "hover"
// ? '"div.sourceCode:hover > "'
? '"div.code-copy-outer-scaffold:hover > "'
: '""',
),
),
);
defaults.push(
outputVariable(
sassVariable(
"code-white-space",
format.render[kCodeOverflow] === "wrap" ? "pre-wrap" : "pre",
),
),
);
defaults.push(
outputVariable(
sassVariable(
kTblCapLoc,
format.metadata[kTblCapLoc] ||
format.metadata[kCapLoc] || kCapTop,
),
),
);
return defaults.join("\n");
};
export function insertFootnotesTitle(
doc: Document,
footnotesEl: Element,
language: FormatLanguage,
level = 2,
classes: string[] = [],
) {
prependHeading(
doc,
footnotesEl,
language[kSectionTitleFootnotes],
level,
classes,
);
}
export function insertReferencesTitle(
doc: Document,
refsEl: Element,
language: FormatLanguage,
level = 2,
classes: string[] = [],
) {
prependHeading(
doc,
refsEl,
language[kSectionTitleReferences],
level,
classes,
);
}
export function insertTitle(
doc: Document,
el: Element,
title: string,
level = 2,
headingClasses: string[] = [],
) {
prependHeading(doc, el, title, level, headingClasses);
}
function prependHeading(
doc: Document,
el: Element,
title: string | undefined,
level: number,
classes: string[],
) {
const heading = doc.createElement("h" + level);
if (typeof title == "string" && title !== "none") {
heading.innerHTML = title;
}
if (classes) {
classes.forEach((clz) => {
heading.classList.add(clz);
});
}
el.insertBefore(heading, el.firstChild);
const hr = el.querySelector("hr");
if (hr) {
hr.remove();
}
}
export function removeFootnoteBacklinks(footnotesEl: Element) {
const backlinks = footnotesEl.querySelectorAll(".footnote-back");
for (const backlink of backlinks) {
(backlink as Element).remove();
}
}
export function setMainColumn(doc: Document, column: string) {
const selectors = [
"main.content",
".page-navigation",
".quarto-title-banner .quarto-title",
".quarto-title-block .quarto-title-meta-author",
".quarto-title-block .quarto-title-meta",
"div[class^='quarto-about-']",
"div[class*=' quarto-about-']",
];
selectors.forEach((selector) => {
const el = doc.querySelector(selector);
if (el) {
// Clear existing column
for (const clz of el.classList) {
if (clz.startsWith("column-")) {
el.classList.remove(clz);
}
}
// Set the new column
el.classList.add(column);
}
});
}
export function hasMarginRefs(format: Format, flags: PandocFlags) {
// If margin footnotes are enabled move them
return format.pandoc[kReferenceLocation] === "margin" ||
flags[kReferenceLocation] === "margin";
}
export function hasMarginCites(format: Format) {
// If margin cites are enabled, move them
return format.metadata[kCitationLocation] === "margin";
}
export function hasMarginFigCaps(format: Format) {
return format.metadata[kFigCapLoc] === "margin";
}
export function computeUrl(
input: string,
baseUrl: string,
offset: string,
outputFileName: string,
) {
const rootDir = normalizePath(join(dirname(input), offset));
if (outputFileName === "index.html") {
return `${baseUrl}/${relative(rootDir, dirname(input))}`;
} else {
return `${baseUrl}/${
relative(rootDir, join(dirname(input), outputFileName))
}`;
}
}
export function createCodeCopyButton(doc: Document, format: Format) {
const copyButton = doc.createElement("button");
const title = format.language[kCopyButtonTooltip]!;
copyButton.setAttribute("title", title);
copyButton.classList
.add("code-copy-button");
const copyIcon = doc.createElement("i");
copyIcon.classList.add("bi");
copyButton.appendChild(copyIcon);
return copyButton;
}
export function createCodeBlock(
doc: Document,
htmlContents: string,
language?: string,
) {
const preEl = doc.createElement("PRE");
preEl.classList.add("sourceCode");
preEl.classList.add("code-with-copy");
const codeEl = doc.createElement("CODE");
codeEl.classList.add("sourceCode");
if (language) {
codeEl.classList.add(language);
}
codeEl.innerHTML = htmlContents;
preEl.appendChild(codeEl);
return preEl;
}
export function writeMetaTag(name: string, content: string, doc: Document) {
// Meta tag
const m = doc.createElement("META");
if (name.startsWith("og:")) {
m.setAttribute("property", name);
} else {
m.setAttribute("name", name);
}
m.setAttribute("content", content);
// New Line
const nl = doc.createTextNode("\n");
// Insert the nodes
doc.querySelector("head")?.appendChild(m);
doc.querySelector("head")?.appendChild(nl);
}
export function writeLinkTag(rel: string, href: string, doc: Document) {
// Meta tag
const l = doc.createElement("LINK");
l.setAttribute("rel", rel);
l.setAttribute("href", href);
// New Line
const nl = doc.createTextNode("\n");
// Insert the nodes
doc.querySelector("head")?.appendChild(l);
doc.querySelector("head")?.appendChild(nl);
}
export function formatPageLayout(format: Format) {
return format.metadata[kPageLayout] as string || kPageLayoutArticle;
}
export function formatHasFullLayout(format: Format) {
return format.metadata[kPageLayout] === kPageLayoutFull;
}
export function formatHasArticleLayout(format: Format) {
return format.metadata[kPageLayout] === undefined ||
format.metadata[kPageLayout] === kPageLayoutArticle ||
format.metadata[kPageLayout] === kPageLayoutFull;
}