@@ -174,6 +174,7 @@ import {
174
174
jupyterCellSrcAsLines ,
175
175
jupyterCellSrcAsStr ,
176
176
} from "./jupyter-shared.ts" ;
177
+ import { error } from "../../deno_ral/log.ts" ;
177
178
178
179
export const kQuartoMimeType = "quarto_mimetype" ;
179
180
export const kQuartoOutputOrder = "quarto_order" ;
@@ -921,8 +922,31 @@ export function jupyterCellWithOptions(
921
922
}
922
923
} ;
923
924
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 - Z a - 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
+
924
947
return {
925
948
...cell ,
949
+ metadata : validMetadata ,
926
950
id : cellId ( cell ) ,
927
951
source,
928
952
optionsSource,
@@ -1766,7 +1790,10 @@ function isMarkdown(output: JupyterOutput, options: JupyterToMarkdownOptions) {
1766
1790
return isDisplayDataType ( output , options , displayDataIsMarkdown ) ;
1767
1791
}
1768
1792
1769
- async function mdOutputStream ( output : JupyterOutputStream , options : JupyterToMarkdownOptions ) {
1793
+ async function mdOutputStream (
1794
+ output : JupyterOutputStream ,
1795
+ options : JupyterToMarkdownOptions ,
1796
+ ) {
1770
1797
let text : string [ ] = [ ] ;
1771
1798
if ( typeof output . text === "string" ) {
1772
1799
text = [ output . text ] ;
@@ -1873,8 +1900,11 @@ async function mdOutputDisplayData(
1873
1900
// if output is invalid, warn and emit empty
1874
1901
const data = output . data [ mimeType ] as unknown ;
1875
1902
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
+ ) ;
1878
1908
}
1879
1909
const lines = data as string [ ] ;
1880
1910
// 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);
1911
1941
// no type match found
1912
1942
return await mdWarningOutput (
1913
1943
"Unable to display output for mime type(s): " +
1914
- Object . keys ( output . data ) . join ( ", " ) ,
1944
+ Object . keys ( output . data ) . join ( ", " ) ,
1915
1945
options ,
1916
1946
) ;
1917
1947
}
0 commit comments