From 126d30a392450f784169e5c4b579b5b92dcade2a Mon Sep 17 00:00:00 2001 From: jankuca Date: Tue, 4 Nov 2025 15:17:47 +0100 Subject: [PATCH] Fix markdown blocks being saved as type=code in .deepnote files - Modified createBlockFromPocket to infer block type from cell kind when pocket metadata is missing - Markdown cells (NotebookCellKind.Markup) now correctly default to type='markdown' instead of type='code' - Added unit tests to verify markdown type preservation - Ensures round-trip fidelity for markdown blocks even when __deepnotePocket metadata is not preserved --- .../deepnoteDataConverter.unit.test.ts | 15 +++++++++++ src/platform/deepnote/pocket.ts | 9 +++++-- src/platform/deepnote/pocket.unit.test.ts | 25 +++++++++++++++++++ 3 files changed, 47 insertions(+), 2 deletions(-) diff --git a/src/notebooks/deepnote/deepnoteDataConverter.unit.test.ts b/src/notebooks/deepnote/deepnoteDataConverter.unit.test.ts index 10931ca36f..fdf81e29d7 100644 --- a/src/notebooks/deepnote/deepnoteDataConverter.unit.test.ts +++ b/src/notebooks/deepnote/deepnoteDataConverter.unit.test.ts @@ -176,6 +176,21 @@ suite('DeepnoteDataConverter', () => { assert.strictEqual(blocks[0].content, '## Heading'); }); + test('converts markdown cell without pocket to markdown block (not code)', () => { + const cells: NotebookCellData[] = [ + { + kind: NotebookCellKind.Markup, + value: '# Title\n\nSome content', + languageId: 'markdown' + } + ]; + + const blocks = converter.convertCellsToBlocks(cells); + + assert.strictEqual(blocks[0].type, 'markdown'); + assert.strictEqual(blocks[0].content, '# Title\n\nSome content'); + }); + test('generates new IDs and sorting keys for cells without metadata', () => { const cells: NotebookCellData[] = [ { diff --git a/src/platform/deepnote/pocket.ts b/src/platform/deepnote/pocket.ts index 7848d8bc3d..f921485f57 100644 --- a/src/platform/deepnote/pocket.ts +++ b/src/platform/deepnote/pocket.ts @@ -1,4 +1,4 @@ -import type { NotebookCellData } from 'vscode'; +import { NotebookCellKind, type NotebookCellData } from 'vscode'; import type { DeepnoteBlock } from './deepnoteTypes'; import { generateBlockId, generateSortingKey } from '../../notebooks/deepnote/dataConversionUtils'; @@ -64,13 +64,18 @@ export function createBlockFromPocket(cell: NotebookCellData, index: number): De } } + // Determine the block type: + // 1. Use the type from the pocket if available + // 2. Otherwise, infer from the cell kind (Code -> 'code', Markup -> 'markdown') + const defaultType = cell.kind === NotebookCellKind.Code ? 'code' : 'markdown'; + const block: DeepnoteBlock = { blockGroup: pocket?.blockGroup || generateUuid(), content: cell.value, id: cellId || generateBlockId(), metadata, sortingKey: pocket?.sortingKey || generateSortingKey(index), - type: pocket?.type || 'code' + type: pocket?.type || defaultType }; if (pocket?.executionCount !== undefined) { diff --git a/src/platform/deepnote/pocket.unit.test.ts b/src/platform/deepnote/pocket.unit.test.ts index 8ff4f51a79..36643160b9 100644 --- a/src/platform/deepnote/pocket.unit.test.ts +++ b/src/platform/deepnote/pocket.unit.test.ts @@ -190,6 +190,31 @@ suite('Pocket', () => { assert.strictEqual(block.type, 'code'); }); + test('uses markdown type for markup cells when no pocket exists', () => { + const cell = new NotebookCellData(NotebookCellKind.Markup, '# Heading', 'markdown'); + + const block = createBlockFromPocket(cell, 0); + + assert.strictEqual(block.type, 'markdown'); + }); + + test('uses markdown type for markup cells when pocket has no type', () => { + const cell = new NotebookCellData(NotebookCellKind.Markup, '# Heading', 'markdown'); + + cell.metadata = { + __deepnotePocket: { + sortingKey: 'a5' + }, + id: 'markdown-block-123' + }; + + const block = createBlockFromPocket(cell, 0); + + assert.strictEqual(block.type, 'markdown'); + assert.strictEqual(block.id, 'markdown-block-123'); + assert.strictEqual(block.sortingKey, 'a5'); + }); + test('handles partial pocket data', () => { const cell = new NotebookCellData(NotebookCellKind.Code, 'print("hello")', 'python');