diff --git a/extensions/ipynb/src/common.ts b/extensions/ipynb/src/common.ts index feb77ba9df3..d361d554877 100644 --- a/extensions/ipynb/src/common.ts +++ b/extensions/ipynb/src/common.ts @@ -40,3 +40,23 @@ export interface CellOutputMetadata { */ __isJson?: boolean; } + + +/** + * Metadata we store in VS Code cells. + * This contains the original metadata from the Jupyuter cells. + */ +export interface CellMetadata { + /** + * Cell id for notebooks created with the new 4.5 version of nbformat. + */ + id?: string; + /** + * Stores attachments for cells. + */ + attachments?: nbformat.IAttachments; + /** + * Stores cell metadata. + */ + metadata?: Partial; +} diff --git a/extensions/ipynb/src/deserializers.ts b/extensions/ipynb/src/deserializers.ts index 4ed27d23e7b..e98bf286efc 100644 --- a/extensions/ipynb/src/deserializers.ts +++ b/extensions/ipynb/src/deserializers.ts @@ -5,7 +5,7 @@ import { nbformat } from '@jupyterlab/coreutils'; import { extensions, NotebookCellData, NotebookCellExecutionSummary, NotebookCellKind, NotebookCellOutput, NotebookCellOutputItem, NotebookData } from 'vscode'; -import { CellOutputMetadata } from './common'; +import { CellMetadata, CellOutputMetadata } from './common'; const jupyterLanguageToMonacoLanguageMapping = new Map([ ['c#', 'csharp'], @@ -146,21 +146,6 @@ function convertJupyterOutputToBuffer(mime: string, value: unknown): NotebookCel } } -/** - * Metadata we store in VS Code cells. - * This contains the original metadata from the Jupyuter cells. - */ -interface CellMetadata { - /** - * Stores attachments for cells. - */ - attachments?: nbformat.IAttachments; - /** - * Stores cell metadata. - */ - metadata?: Partial; -} - function getNotebookCellMetadata(cell: nbformat.IBaseCell): CellMetadata { // We put this only for VSC to display in diff view. // Else we don't use this. @@ -171,6 +156,9 @@ function getNotebookCellMetadata(cell: nbformat.IBaseCell): CellMetadata { custom[propertyToClone] = JSON.parse(JSON.stringify(cell[propertyToClone])); } }); + if ('id' in cell && typeof cell.id === 'string') { + custom.id = cell.id; + } return custom; } function getOutputMetadata(output: nbformat.IOutput): CellOutputMetadata { diff --git a/extensions/ipynb/src/serializers.ts b/extensions/ipynb/src/serializers.ts index 11587bb4296..b7e3f574f63 100644 --- a/extensions/ipynb/src/serializers.ts +++ b/extensions/ipynb/src/serializers.ts @@ -5,7 +5,7 @@ import { nbformat } from '@jupyterlab/coreutils'; import { NotebookCellData, NotebookCellKind, NotebookCellOutput } from 'vscode'; -import { CellOutputMetadata } from './common'; +import { CellMetadata, CellOutputMetadata } from './common'; import { textMimeTypes } from './deserializers'; const textDecoder = new TextDecoder(); @@ -62,6 +62,9 @@ function createCodeCellFromNotebookCell(cell: NotebookCellData): nbformat.ICodeC outputs: (cell.outputs || []).map(translateCellDisplayOutput), metadata: cellMetadata?.metadata || {} // This cannot be empty. }; + if (cellMetadata?.id) { + codeCell.id = cellMetadata.id; + } return codeCell; } @@ -75,6 +78,9 @@ function createRawCellFromNotebookCell(cell: NotebookCellData): nbformat.IRawCel if (cellMetadata?.attachments) { rawCell.attachments = cellMetadata.attachments; } + if (cellMetadata?.id) { + rawCell.id = cellMetadata.id; + } return rawCell; } @@ -322,24 +328,12 @@ function createMarkdownCellFromNotebookCell(cell: NotebookCellData): nbformat.IM if (cellMetadata?.attachments) { markdownCell.attachments = cellMetadata.attachments; } + if (cellMetadata?.id) { + markdownCell.id = cellMetadata.id; + } return markdownCell; } -/** - * Metadata we store in VS Code cells. - * This contains the original metadata from the Jupyuter cells. - */ -interface CellMetadata { - /** - * Stores attachments for cells. - */ - attachments?: nbformat.IAttachments; - /** - * Stores cell metadata. - */ - metadata?: Partial; -} - export function pruneCell(cell: nbformat.ICell): nbformat.ICell { // Source is usually a single string on input. Convert back to an array const result = {