Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
quarto-dev
GitHub Repository: quarto-dev/quarto-cli
Path: blob/main/src/config/format.ts
12924 views
1
/*
2
* format.ts
3
*
4
* Copyright (C) 2020-2022 Posit Software, PBC
5
*/
6
7
import { kBaseFormat, kPreferHtml } from "../config/constants.ts";
8
import { Format, FormatPandoc } from "./types.ts";
9
10
export function isPdfOutput(format: string): boolean;
11
export function isPdfOutput(format: FormatPandoc): boolean;
12
export function isPdfOutput(format: string | FormatPandoc): boolean {
13
return isFormatTo(format, "pdf") || isFormatTo(format, "beamer");
14
}
15
16
export function isLatexOutput(format: FormatPandoc) {
17
return ["pdf", "latex", "beamer"].some((fmt) => isFormatTo(format, fmt));
18
}
19
20
export function isTypstOutput(format: string): boolean;
21
export function isTypstOutput(format: FormatPandoc): boolean;
22
export function isTypstOutput(format: string | FormatPandoc) {
23
return isFormatTo(format, "typst");
24
}
25
26
export function isBeamerOutput(format: FormatPandoc) {
27
return isFormatTo(format, "beamer");
28
}
29
30
export function isEpubOutput(format: string): boolean;
31
export function isEpubOutput(format: FormatPandoc): boolean;
32
export function isEpubOutput(format: string | FormatPandoc): boolean {
33
if (typeof format !== "string") {
34
format = format?.to || "html";
35
}
36
return ["epub", "epub2", "epub3"].some((fmt) => isFormatTo(format, fmt));
37
}
38
39
export function isDocxOutput(format: string): boolean;
40
export function isDocxOutput(format: FormatPandoc): boolean;
41
export function isDocxOutput(format: string | FormatPandoc): boolean {
42
return isFormatTo(format, "docx");
43
}
44
45
export function isHtmlFileOutput(format: string): boolean;
46
export function isHtmlFileOutput(format: FormatPandoc): boolean;
47
export function isHtmlFileOutput(format?: string | FormatPandoc): boolean {
48
if (typeof format !== "string") {
49
format = format?.to || "html";
50
}
51
return isHtmlDocOutput(format) || isHtmlSlideOutput(format);
52
}
53
54
export function isHtmlOutput(format: string, strict?: boolean): boolean;
55
export function isHtmlOutput(format: FormatPandoc, strict?: boolean): boolean;
56
export function isHtmlOutput(
57
format?: string | FormatPandoc,
58
strict?: boolean,
59
): boolean {
60
if (typeof format !== "string") {
61
format = format?.to;
62
}
63
format = format || "html";
64
if (
65
isHtmlDocOutput(format) || isHtmlDashboardOutput(format)
66
) {
67
return true;
68
} else if (!strict) {
69
return isHtmlSlideOutput(format) || isEpubOutput(format);
70
} else {
71
return false;
72
}
73
}
74
75
export function isHtmlDocOutput(format: string | FormatPandoc) {
76
return ["html", "html4", "html5"].some((fmt) => isFormatTo(format, fmt));
77
}
78
79
export function isHtmlSlideOutput(format: string | FormatPandoc) {
80
return [
81
"s5",
82
"dzslides",
83
"slidy",
84
"slideous",
85
"revealjs",
86
].some((fmt) => isFormatTo(format, fmt));
87
}
88
89
// Dashboard uses pandoc.to="html", so pass format.identifier["base-format"]
90
// (not format.pandoc.to) when checking from a Format object.
91
export function isHtmlDashboardOutput(format?: string) {
92
return format === "dashboard" || format?.endsWith("-dashboard");
93
}
94
95
export function isJatsOutput(format?: string | FormatPandoc) {
96
if (typeof format !== "string") {
97
format = format?.to || "html";
98
}
99
100
return [
101
"jats",
102
"jats_archiving",
103
"jats_articleauthoring",
104
"jats_publishing",
105
].find((formatStr) => {
106
return (format as string).startsWith(formatStr);
107
}) !== undefined;
108
}
109
110
export function isPresentationOutput(format: FormatPandoc) {
111
if (format.to) {
112
return ["s5", "dzslides", "slidy", "slideous", "revealjs", "beamer", "pptx"]
113
.some((to) => format.to?.startsWith(to));
114
} else {
115
return false;
116
}
117
}
118
119
export function isRevealjsOutput(format: string): boolean;
120
export function isRevealjsOutput(format: FormatPandoc): boolean;
121
export function isRevealjsOutput(format?: string | FormatPandoc) {
122
if (typeof format !== "string") {
123
format = format?.to;
124
}
125
format = format || "html";
126
return format.startsWith("revealjs");
127
}
128
129
export function isNativeOutput(format: FormatPandoc) {
130
return isFormatTo(format, "native");
131
}
132
133
export function isJsonOutput(format: FormatPandoc) {
134
return isFormatTo(format, "json");
135
}
136
137
export function isAstOutput(format: FormatPandoc) {
138
return isNativeOutput(format) || isJsonOutput(format);
139
}
140
141
export function isIpynbOutput(format: FormatPandoc) {
142
return isFormatTo(format, "ipynb");
143
}
144
145
function isFormatTo(format: string | FormatPandoc, to: string) {
146
const formatStr = typeof format === "string"
147
? format
148
: (format?.to || "html");
149
return formatStr.startsWith(to);
150
}
151
152
export function isMarkdownOutput(
153
format: Format,
154
flavors = [
155
"markdown",
156
"markdown_github",
157
"markdown_mmd",
158
"markdown_phpextra",
159
"markdown_strict",
160
"gfm",
161
"commonmark",
162
"commonmark_x",
163
"markua",
164
],
165
) {
166
const to = format.identifier[kBaseFormat] || "html";
167
return flavors.includes(to) || isIpynbOutput(format.pandoc);
168
}
169
170
export function isHtmlCompatible(format: Format) {
171
return isHtmlOutput(format.pandoc) ||
172
(isMarkdownOutput(format) && format.render[kPreferHtml]) ||
173
isIpynbOutput(format.pandoc);
174
}
175
176
export function isJavascriptCompatible(format: Format) {
177
return isHtmlCompatible(format) && !isEpubOutput(format.pandoc);
178
}
179
180