Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
quarto-dev
GitHub Repository: quarto-dev/quarto-cli
Path: blob/main/src/config/format.ts
6449 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
export function isHtmlDashboardOutput(format?: string) {
90
return format === "dashboard" || format?.endsWith("-dashboard");
91
}
92
93
export function isJatsOutput(format?: string | FormatPandoc) {
94
if (typeof format !== "string") {
95
format = format?.to || "html";
96
}
97
98
return [
99
"jats",
100
"jats_archiving",
101
"jats_articleauthoring",
102
"jats_publishing",
103
].find((formatStr) => {
104
return (format as string).startsWith(formatStr);
105
}) !== undefined;
106
}
107
108
export function isPresentationOutput(format: FormatPandoc) {
109
if (format.to) {
110
return ["s5", "dzslides", "slidy", "slideous", "revealjs", "beamer", "pptx"]
111
.some((to) => format.to?.startsWith(to));
112
} else {
113
return false;
114
}
115
}
116
117
export function isRevealjsOutput(format: string): boolean;
118
export function isRevealjsOutput(format: FormatPandoc): boolean;
119
export function isRevealjsOutput(format?: string | FormatPandoc) {
120
if (typeof format !== "string") {
121
format = format?.to;
122
}
123
format = format || "html";
124
return format.startsWith("revealjs");
125
}
126
127
export function isNativeOutput(format: FormatPandoc) {
128
return isFormatTo(format, "native");
129
}
130
131
export function isJsonOutput(format: FormatPandoc) {
132
return isFormatTo(format, "json");
133
}
134
135
export function isAstOutput(format: FormatPandoc) {
136
return isNativeOutput(format) || isJsonOutput(format);
137
}
138
139
export function isIpynbOutput(format: FormatPandoc) {
140
return isFormatTo(format, "ipynb");
141
}
142
143
function isFormatTo(format: string | FormatPandoc, to: string) {
144
const formatStr = typeof format === "string"
145
? format
146
: (format?.to || "html");
147
return formatStr.startsWith(to);
148
}
149
150
export function isMarkdownOutput(
151
format: Format,
152
flavors = [
153
"markdown",
154
"markdown_github",
155
"markdown_mmd",
156
"markdown_phpextra",
157
"markdown_strict",
158
"gfm",
159
"commonmark",
160
"commonmark_x",
161
"markua",
162
],
163
) {
164
const to = format.identifier[kBaseFormat] || "html";
165
return flavors.includes(to) || isIpynbOutput(format.pandoc);
166
}
167
168
export function isHtmlCompatible(format: Format) {
169
return isHtmlOutput(format.pandoc) ||
170
(isMarkdownOutput(format) && format.render[kPreferHtml]) ||
171
isIpynbOutput(format.pandoc);
172
}
173
174
export function isJavascriptCompatible(format: Format) {
175
return isHtmlCompatible(format) && !isEpubOutput(format.pandoc);
176
}
177
178