This commit is contained in:
rebornix 2022-03-15 17:00:43 -07:00
parent 1bcad24aba
commit 157cba093c
No known key found for this signature in database
GPG key ID: 181FC90D15393C20
3 changed files with 24 additions and 6 deletions

View file

@ -308,7 +308,9 @@ function createNotebookCellDataFromCodeCell(cell: nbformat.ICodeCell, cellLangua
? { executionOrder: cell.execution_count as number }
: {};
const cellData = new NotebookCellData(NotebookCellKind.Code, source, cellLanguage);
const vscodeCustomMetadata = cell.metadata['vscode'] as { [key: string]: any } | undefined;
const cellLanguageId = vscodeCustomMetadata && vscodeCustomMetadata.languageId && typeof vscodeCustomMetadata.languageId === 'string' ? vscodeCustomMetadata.languageId : cellLanguage;
const cellData = new NotebookCellData(NotebookCellKind.Code, source, cellLanguageId);
cellData.outputs = outputs;
cellData.metadata = { custom: getNotebookCellMetadata(cell) };

View file

@ -83,9 +83,11 @@ export class NotebookSerializer implements vscode.NotebookSerializer {
public serializeNotebookToString(data: vscode.NotebookData): string {
const notebookContent = getNotebookMetadata(data);
// use the preferred language from document metadata or the first cell language as the notebook preferred cell language
const preferredCellLanguage = notebookContent.metadata?.language_info?.name ?? data.cells[0].languageId;
notebookContent.cells = data.cells
.map(cell => createJupyterCellFromNotebookCell(cell))
.map(cell => createJupyterCellFromNotebookCell(cell, preferredCellLanguage))
.map(pruneCell);
const indentAmount = data.metadata && 'indentAmount' in data.metadata && typeof data.metadata.indentAmount === 'string' ?

View file

@ -17,7 +17,8 @@ enum CellOutputMimeTypes {
}
export function createJupyterCellFromNotebookCell(
vscCell: NotebookCellData
vscCell: NotebookCellData,
preferredLanguage: string | undefined
): nbformat.IRawCell | nbformat.IMarkdownCell | nbformat.ICodeCell {
let cell: nbformat.IRawCell | nbformat.IMarkdownCell | nbformat.ICodeCell;
if (vscCell.kind === NotebookCellKind.Markup) {
@ -25,7 +26,7 @@ export function createJupyterCellFromNotebookCell(
} else if (vscCell.languageId === 'raw') {
cell = createRawCellFromNotebookCell(vscCell);
} else {
cell = createCodeCellFromNotebookCell(vscCell);
cell = createCodeCellFromNotebookCell(vscCell, preferredLanguage);
}
return cell;
}
@ -56,14 +57,27 @@ export function sortObjectPropertiesRecursively(obj: any): any {
export function getCellMetadata(cell: NotebookCell | NotebookCellData) {
return cell.metadata?.custom as CellMetadata | undefined;
}
function createCodeCellFromNotebookCell(cell: NotebookCellData): nbformat.ICodeCell {
function createCodeCellFromNotebookCell(cell: NotebookCellData, preferredLanguage: string | undefined): nbformat.ICodeCell {
const cellMetadata = getCellMetadata(cell);
let metadata = cellMetadata?.metadata || {}; // This cannot be empty.
if (cell.languageId !== preferredLanguage) {
metadata = {
...metadata,
vscode: {
languageId: cell.languageId
}
};
} else {
// cell current language is the same as the preferred cell language in the document, flush the vscode custom language id metadata
metadata.vscode = undefined;
}
const codeCell: any = {
cell_type: 'code',
execution_count: cell.executionSummary?.executionOrder ?? null,
source: splitMultilineString(cell.value.replace(/\r\n/g, '\n')),
outputs: (cell.outputs || []).map(translateCellDisplayOutput),
metadata: cellMetadata?.metadata || {} // This cannot be empty.
metadata: metadata
};
if (cellMetadata?.id) {
codeCell.id = cellMetadata.id;