Skip to content

Commit 51b2753

Browse files
committed
jupyter - JSON-encode compound metadata keys
1 parent 0a38e70 commit 51b2753

File tree

1 file changed

+34
-4
lines changed

1 file changed

+34
-4
lines changed

src/core/jupyter/jupyter.ts

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ import {
174174
jupyterCellSrcAsLines,
175175
jupyterCellSrcAsStr,
176176
} from "./jupyter-shared.ts";
177+
import { error } from "../../deno_ral/log.ts";
177178

178179
export const kQuartoMimeType = "quarto_mimetype";
179180
export const kQuartoOutputOrder = "quarto_order";
@@ -921,8 +922,31 @@ export function jupyterCellWithOptions(
921922
}
922923
};
923924

925+
const validMetadata: Record<string, string | number | boolean> = {};
926+
for (const key of Object.keys(cell.metadata)) {
927+
const value = cell.metadata[key];
928+
if (value !== undefined) {
929+
if (value && typeof value === "object") {
930+
// we need to json-encode this and signal the encoding in the key
931+
validMetadata[
932+
`quarto-json-encoded-${key.replaceAll(/[^A-Za-z]/g, "_")}`
933+
] = JSON.stringify({ key, value });
934+
} else if (
935+
typeof value === "string" || typeof value === "number" ||
936+
typeof value === "boolean"
937+
) {
938+
validMetadata[key] = value;
939+
} else {
940+
error(
941+
`Invalid metadata type for key ${key}: ${typeof value}. Entry will not be serialized.`,
942+
);
943+
}
944+
}
945+
}
946+
924947
return {
925948
...cell,
949+
metadata: validMetadata,
926950
id: cellId(cell),
927951
source,
928952
optionsSource,
@@ -1766,7 +1790,10 @@ function isMarkdown(output: JupyterOutput, options: JupyterToMarkdownOptions) {
17661790
return isDisplayDataType(output, options, displayDataIsMarkdown);
17671791
}
17681792

1769-
async function mdOutputStream(output: JupyterOutputStream, options: JupyterToMarkdownOptions) {
1793+
async function mdOutputStream(
1794+
output: JupyterOutputStream,
1795+
options: JupyterToMarkdownOptions,
1796+
) {
17701797
let text: string[] = [];
17711798
if (typeof output.text === "string") {
17721799
text = [output.text];
@@ -1873,8 +1900,11 @@ async function mdOutputDisplayData(
18731900
// if output is invalid, warn and emit empty
18741901
const data = output.data[mimeType] as unknown;
18751902
if (!Array.isArray(data) || data.some((s) => typeof s !== "string")) {
1876-
return await mdWarningOutput(`Unable to process text plain output data
1877-
which does not appear to be plain text: ${JSON.stringify(data)}`, options);
1903+
return await mdWarningOutput(
1904+
`Unable to process text plain output data
1905+
which does not appear to be plain text: ${JSON.stringify(data)}`,
1906+
options,
1907+
);
18781908
}
18791909
const lines = data as string[];
18801910
// pandas inexplicably outputs html tables as text/plain with an enclosing single-quote
@@ -1911,7 +1941,7 @@ which does not appear to be plain text: ${JSON.stringify(data)}`, options);
19111941
// no type match found
19121942
return await mdWarningOutput(
19131943
"Unable to display output for mime type(s): " +
1914-
Object.keys(output.data).join(", "),
1944+
Object.keys(output.data).join(", "),
19151945
options,
19161946
);
19171947
}

0 commit comments

Comments
 (0)