Skip to content

Commit edad1e9

Browse files
committed
Properly resuscitate the mimetype
When restoring display data, pandoc is always using the `application/json` mime type. If we have additional mime information, we stick that in the json payload and later use that to correct the mimetype.
1 parent d68cd85 commit edad1e9

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

src/core/jupyter/jupyter.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,8 @@ import { ProjectContext } from "../../project/types.ts";
151151
import { mergeConfigs } from "../config.ts";
152152
import { encode as encodeBase64 } from "encoding/base64.ts";
153153

154+
export const kQuartoMimeType = "quarto_mimetype";
155+
154156
export const kJupyterNotebookExtensions = [
155157
".ipynb",
156158
];
@@ -1511,9 +1513,12 @@ async function mdOutputDisplayData(
15111513
} else if (displayDataIsHtml(mimeType)) {
15121514
return mdHtmlOutput(output.data[mimeType] as string[]);
15131515
} else if (displayDataIsJson(mimeType)) {
1516+
// Add the literal mimetype information to the payload, for later use
1517+
const json = output.data[mimeType] as Record<string, unknown>;
1518+
json[kQuartoMimeType] = mimeType;
15141519
return mdJsonOutput(
15151520
mimeType,
1516-
output.data[mimeType] as Record<string, unknown>,
1521+
json,
15171522
options,
15181523
);
15191524
} else if (displayDataIsJavascript(mimeType)) {

src/format/ipynb/format-ipynb.ts

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ import {
1414
kIPynbTitleBlockTemplate,
1515
} from "../../config/constants.ts";
1616
import { Format, PandocFlags } from "../../config/types.ts";
17-
import { jupyterFromFile } from "../../core/jupyter/jupyter.ts";
17+
import {
18+
jupyterFromFile,
19+
kQuartoMimeType,
20+
} from "../../core/jupyter/jupyter.ts";
1821
import {
1922
kApplicationRtf,
2023
kRestructuredText,
@@ -25,6 +28,11 @@ import { formatResourcePath } from "../../core/resources.ts";
2528
import { createFormat } from "../formats-shared.ts";
2629

2730
import { decode as base64decode } from "encoding/base64.ts";
31+
import {
32+
JupyterOutput,
33+
JupyterOutputDisplayData,
34+
} from "../../core/jupyter/types.ts";
35+
2836
export function ipynbFormat(): Format {
2937
return createFormat("ipynb", {
3038
pandoc: {
@@ -100,6 +108,30 @@ export function ipynbFormat(): Format {
100108
}
101109
}
102110
}
111+
112+
// Fix up mime types that Quarto has emplaced
113+
cell.outputs?.forEach((output) => {
114+
const cellOutput = output as JupyterOutput;
115+
if (cellOutput.output_type === "display_data") {
116+
const cellDisplayOutput =
117+
cellOutput as JupyterOutputDisplayData;
118+
if (cellDisplayOutput.data["application/json"]) {
119+
const jsonData =
120+
(cellDisplayOutput.data["application/json"] as Record<
121+
string,
122+
unknown
123+
>);
124+
if (jsonData[kQuartoMimeType]) {
125+
const realMimetype = jsonData[kQuartoMimeType] as string;
126+
delete jsonData[kQuartoMimeType];
127+
128+
cellDisplayOutput.data[realMimetype] = jsonData;
129+
delete cellDisplayOutput.data["application/json"];
130+
}
131+
}
132+
}
133+
});
134+
103135
return cell;
104136
});
105137
Deno.writeTextFileSync(output, JSON.stringify(nb, null, 2));

0 commit comments

Comments
 (0)