-
Notifications
You must be signed in to change notification settings - Fork 432
Expand file tree
/
Copy pathdisplay-data.ts
More file actions
202 lines (183 loc) · 5.09 KB
/
display-data.ts
File metadata and controls
202 lines (183 loc) · 5.09 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
/*
* display-data.ts
*
* Copyright (C) 2020-2022 Posit Software, PBC
*/
import * as ld from "../../core/lodash.ts";
import {
kApplicationJavascript,
kApplicationJupyterWidgetState,
kApplicationJupyterWidgetView,
kApplicationPdf,
kImageJpeg,
kImagePng,
kImageSvg,
kTextHtml,
kTextLatex,
kTextMarkdown,
kTextPlain,
} from "../mime.ts";
import {
JupyterCell,
JupyterCellOutputData,
JupyterOutput,
JupyterOutputDisplayData,
JupyterToMarkdownOptions,
} from "./types.ts";
export function isDisplayData(
output: JupyterOutput,
): output is JupyterOutputDisplayData {
return ["display_data", "execute_result"].includes(output.output_type);
}
export function isCaptionableData(output: JupyterOutput) {
if (isDisplayData(output)) {
const displayData = output as JupyterOutputDisplayData;
return !displayData.noCaption;
} else {
return false;
}
}
export function displayDataMimeType(
output: JupyterCellOutputData,
options: JupyterToMarkdownOptions,
) {
const displayPriority = [
kTextMarkdown,
kImageSvg,
kImagePng,
kImageJpeg,
];
if (options.toHtml) {
const htmlFormats = [
kApplicationJupyterWidgetState,
kApplicationJupyterWidgetView,
kApplicationJavascript,
kTextHtml,
];
// if we are targeting markdown w/ html then prioritize the html formats
// (this is b/c jupyter widgets also provide a text/markdown representation
// that we don't want to have "win" over the widget)
if (options.toMarkdown) {
displayPriority.unshift(...htmlFormats);
// otherwise put them after markdown
} else {
displayPriority.push(...htmlFormats);
}
displayPriority.unshift(
kApplicationJupyterWidgetState,
kApplicationJupyterWidgetView,
kApplicationJavascript,
kTextHtml,
);
} else if (options.toLatex) {
// latex and pdf should be preferred over the other mime types
displayPriority.unshift(
kTextLatex,
kApplicationPdf,
);
} else if (options.toMarkdown) {
displayPriority.push(
kTextHtml,
);
}
// if there is an html table then add html (as we can read this directly
// into the pandoc AST in our lua filters)
if (displayDataHasHtmlTable(output) && !displayPriority.includes(kTextHtml)) {
displayPriority.push(kTextHtml);
}
// always add text/plain
displayPriority.push(
kTextPlain,
);
const availDisplay = Object.keys(output.data);
for (const display of displayPriority) {
if (availDisplay.includes(display)) {
return display;
}
}
return null;
}
export function displayDataLatexIsMath(latex: string[]) {
if (latex.length > 0) {
const first = latex[0];
const last = latex[latex.length - 1];
return (
// Inline or display math
(first.startsWith("$") && last.endsWith("$")) ||
// Latex environment
(first.startsWith("\\begin{") && last.includes("\\end{"))
);
}
return false;
}
export function displayDataWithMarkdownMath(output: JupyterOutputDisplayData) {
if (Array.isArray(output.data[kTextLatex]) && !output.data[kTextMarkdown]) {
const latex = output.data[kTextLatex] as string[];
if (displayDataLatexIsMath(latex)) {
output = {
...output,
data: {
...output.data,
[kTextMarkdown]: latex,
},
};
return output;
}
}
return output;
}
export function displayDataHasHtmlTable(
output: JupyterCellOutputData,
) {
const html = Array.isArray(output.data[kTextHtml])
? output.data[kTextHtml] as string[]
: typeof (output.data[kTextHtml]) === "string"
? [output.data[kTextHtml]] as string[]
: undefined;
if (html) {
const htmlLower = html.map((line) => line.toLowerCase());
return htmlLower.some((line) => !!line.match(/<[Tt][Aa][Bb][Ll][Ee]/)) &&
htmlLower.some((line) => !!line.match(/<\/[Tt][Aa][Bb][Ll][Ee]/));
} else {
return false;
}
}
export function displayDataIsImage(mimeType: string) {
return [kImagePng, kImageJpeg, kImageSvg, kApplicationPdf].includes(mimeType);
}
export function displayDataIsTextPlain(mimeType: string) {
return [kTextPlain].includes(mimeType);
}
export function displayDataIsMarkdown(mimeType: string) {
return [kTextMarkdown].includes(mimeType);
}
export function displayDataIsLatex(mimeType: string) {
return [kTextLatex].includes(mimeType);
}
export function displayDataIsHtml(mimeType: string) {
return [kTextHtml].includes(mimeType);
}
export function displayDataIsJson(mimeType: string) {
return [kApplicationJupyterWidgetState, kApplicationJupyterWidgetView]
.includes(mimeType);
}
export function displayDataIsJavascript(mimeType: string) {
return [kApplicationJavascript].includes(mimeType);
}
export function cellHasOnlyMarkdownDisplayData(
cell: JupyterCell,
options: JupyterToMarkdownOptions,
) {
if (cell.outputs) {
return cell.outputs.every((output) => {
if (isDisplayData(output)) {
const mimeType = displayDataMimeType(output, options);
return mimeType && displayDataIsMarkdown(mimeType);
} else {
return false;
}
});
} else {
return false;
}
}