Skip to content

Commit 3e42395

Browse files
authored
Fix markdown blocks being saved as type=code in .deepnote files (#176)
- 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
1 parent 2910974 commit 3e42395

File tree

3 files changed

+47
-2
lines changed

3 files changed

+47
-2
lines changed

src/notebooks/deepnote/deepnoteDataConverter.unit.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,21 @@ suite('DeepnoteDataConverter', () => {
176176
assert.strictEqual(blocks[0].content, '## Heading');
177177
});
178178

179+
test('converts markdown cell without pocket to markdown block (not code)', () => {
180+
const cells: NotebookCellData[] = [
181+
{
182+
kind: NotebookCellKind.Markup,
183+
value: '# Title\n\nSome content',
184+
languageId: 'markdown'
185+
}
186+
];
187+
188+
const blocks = converter.convertCellsToBlocks(cells);
189+
190+
assert.strictEqual(blocks[0].type, 'markdown');
191+
assert.strictEqual(blocks[0].content, '# Title\n\nSome content');
192+
});
193+
179194
test('generates new IDs and sorting keys for cells without metadata', () => {
180195
const cells: NotebookCellData[] = [
181196
{

src/platform/deepnote/pocket.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { NotebookCellData } from 'vscode';
1+
import { NotebookCellKind, type NotebookCellData } from 'vscode';
22

33
import type { DeepnoteBlock } from './deepnoteTypes';
44
import { generateBlockId, generateSortingKey } from '../../notebooks/deepnote/dataConversionUtils';
@@ -64,13 +64,18 @@ export function createBlockFromPocket(cell: NotebookCellData, index: number): De
6464
}
6565
}
6666

67+
// Determine the block type:
68+
// 1. Use the type from the pocket if available
69+
// 2. Otherwise, infer from the cell kind (Code -> 'code', Markup -> 'markdown')
70+
const defaultType = cell.kind === NotebookCellKind.Code ? 'code' : 'markdown';
71+
6772
const block: DeepnoteBlock = {
6873
blockGroup: pocket?.blockGroup || generateUuid(),
6974
content: cell.value,
7075
id: cellId || generateBlockId(),
7176
metadata,
7277
sortingKey: pocket?.sortingKey || generateSortingKey(index),
73-
type: pocket?.type || 'code'
78+
type: pocket?.type || defaultType
7479
};
7580

7681
if (pocket?.executionCount !== undefined) {

src/platform/deepnote/pocket.unit.test.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,31 @@ suite('Pocket', () => {
190190
assert.strictEqual(block.type, 'code');
191191
});
192192

193+
test('uses markdown type for markup cells when no pocket exists', () => {
194+
const cell = new NotebookCellData(NotebookCellKind.Markup, '# Heading', 'markdown');
195+
196+
const block = createBlockFromPocket(cell, 0);
197+
198+
assert.strictEqual(block.type, 'markdown');
199+
});
200+
201+
test('uses markdown type for markup cells when pocket has no type', () => {
202+
const cell = new NotebookCellData(NotebookCellKind.Markup, '# Heading', 'markdown');
203+
204+
cell.metadata = {
205+
__deepnotePocket: {
206+
sortingKey: 'a5'
207+
},
208+
id: 'markdown-block-123'
209+
};
210+
211+
const block = createBlockFromPocket(cell, 0);
212+
213+
assert.strictEqual(block.type, 'markdown');
214+
assert.strictEqual(block.id, 'markdown-block-123');
215+
assert.strictEqual(block.sortingKey, 'a5');
216+
});
217+
193218
test('handles partial pocket data', () => {
194219
const cell = new NotebookCellData(NotebookCellKind.Code, 'print("hello")', 'python');
195220

0 commit comments

Comments
 (0)