@@ -7,27 +7,44 @@ import { ErrorOutputHandler } from './outputHandlers/ErrorOutputHandler';
77import { RichOutputHandler } from './outputHandlers/RichOutputHandler' ;
88import { mergeMetadata , hasMetadataContent , generateBlockId , generateSortingKey } from './dataConversionUtils' ;
99
10+ /**
11+ * Utility class for converting between Deepnote block structures and VS Code notebook cells.
12+ * Handles bidirectional conversion while preserving metadata and execution state.
13+ */
1014export class DeepnoteDataConverter {
1115 private readonly outputDetector = new OutputTypeDetector ( ) ;
1216 private readonly streamHandler = new StreamOutputHandler ( ) ;
1317 private readonly errorHandler = new ErrorOutputHandler ( ) ;
1418 private readonly richHandler = new RichOutputHandler ( ) ;
19+
20+ /**
21+ * Converts Deepnote blocks to VS Code notebook cells.
22+ * Sorts blocks by sortingKey before conversion to maintain proper order.
23+ * @param blocks Array of Deepnote blocks to convert
24+ * @returns Array of VS Code notebook cell data
25+ */
1526 convertBlocksToCells ( blocks : DeepnoteBlock [ ] ) : NotebookCellData [ ] {
1627 return blocks
1728 . sort ( ( a , b ) => a . sortingKey . localeCompare ( b . sortingKey ) )
1829 . map ( ( block ) => this . convertBlockToCell ( block ) ) ;
1930 }
2031
32+ /**
33+ * Converts VS Code notebook cells back to Deepnote blocks.
34+ * Generates missing IDs and sorting keys as needed.
35+ * @param cells Array of VS Code notebook cells to convert
36+ * @returns Array of Deepnote blocks
37+ */
2138 convertCellsToBlocks ( cells : NotebookCellData [ ] ) : DeepnoteBlock [ ] {
2239 return cells . map ( ( cell , index ) => this . convertCellToBlock ( cell , index ) ) ;
2340 }
2441
2542 private convertBlockToCell ( block : DeepnoteBlock ) : NotebookCellData {
2643 const cellKind = block . type === 'code' ? NotebookCellKind . Code : NotebookCellKind . Markup ;
2744 const languageId = block . type === 'code' ? 'python' : 'markdown' ;
28-
45+
2946 const cell = new NotebookCellData ( cellKind , block . content , languageId ) ;
30-
47+
3148 cell . metadata = {
3249 deepnoteBlockId : block . id ,
3350 deepnoteBlockType : block . type ,
@@ -36,9 +53,9 @@ export class DeepnoteDataConverter {
3653 ...( typeof block . executionCount === 'number' && { executionCount : block . executionCount } ) ,
3754 ...( block . outputReference && { deepnoteOutputReference : block . outputReference } )
3855 } ;
39-
56+
4057 cell . outputs = this . convertDeepnoteOutputsToVSCodeOutputs ( block . outputs || [ ] ) ;
41-
58+
4259 return cell ;
4360 }
4461
@@ -79,7 +96,7 @@ export class DeepnoteDataConverter {
7996
8097 private convertSingleOutput ( output : DeepnoteOutput ) : NotebookCellOutput {
8198 const outputItems = this . createOutputItems ( output ) ;
82-
99+
83100 const metadata = mergeMetadata (
84101 output . metadata ,
85102 output . execution_count !== undefined ? { executionCount : output . execution_count } : undefined
@@ -101,7 +118,7 @@ export class DeepnoteDataConverter {
101118
102119 switch ( detection . type ) {
103120 case 'error' :
104- deepnoteOutput = this . errorHandler . convertToDeepnote ( output , detection . errorItem ! ) ;
121+ deepnoteOutput = this . errorHandler . convertToDeepnote ( detection . errorItem ! ) ;
105122 break ;
106123 case 'stream' :
107124 deepnoteOutput = this . streamHandler . convertToDeepnote ( output ) ;
@@ -115,7 +132,7 @@ export class DeepnoteDataConverter {
115132 // Preserve metadata from VS Code output
116133 if ( output . metadata ) {
117134 deepnoteOutput . metadata = mergeMetadata ( deepnoteOutput . metadata , output . metadata ) ;
118-
135+
119136 // Extract execution count from metadata
120137 if ( output . metadata . executionCount !== undefined ) {
121138 deepnoteOutput . execution_count = output . metadata . executionCount as number ;
@@ -125,7 +142,6 @@ export class DeepnoteDataConverter {
125142 return deepnoteOutput ;
126143 }
127144
128-
129145 private createOutputItems ( output : DeepnoteOutput ) : NotebookCellOutputItem [ ] {
130146 switch ( output . output_type ) {
131147 case 'stream' :
@@ -143,5 +159,4 @@ export class DeepnoteDataConverter {
143159 return [ ] ;
144160 }
145161 }
146-
147162}
0 commit comments