@@ -90,8 +90,16 @@ function sortOutputItemsBasedOnDisplayOrder(outputItems: NotebookCellOutputItem[
90
90
. sort ( ( outputItemA , outputItemB ) => outputItemA . index - outputItemB . index ) . map ( item => item . item ) ;
91
91
}
92
92
93
- function concatMultilineString ( str : string | string [ ] , trim ?: boolean ) : string {
94
- const nonLineFeedWhiteSpaceTrim = / ( ^ [ \t \f \v \r ] + | [ \t \f \v \r ] + $ ) / g;
93
+ /**
94
+ * Concatenates a multiline string or an array of strings into a single string.
95
+ * Also normalizes line endings to use LF (`\n`) instead of CRLF (`\r\n`).
96
+ * Same is done in serializer as well.
97
+ */
98
+ function concatMultilineCellSource ( source : string | string [ ] ) : string {
99
+ return concatMultilineString ( source ) . replace ( / \r \n / g, '\n' ) ;
100
+ }
101
+
102
+ function concatMultilineString ( str : string | string [ ] ) : string {
95
103
if ( Array . isArray ( str ) ) {
96
104
let result = '' ;
97
105
for ( let i = 0 ; i < str . length ; i += 1 ) {
@@ -103,10 +111,9 @@ function concatMultilineString(str: string | string[], trim?: boolean): string {
103
111
}
104
112
}
105
113
106
- // Just trim whitespace. Leave \n in place
107
- return trim ? result . replace ( nonLineFeedWhiteSpaceTrim , '' ) : result ;
114
+ return result ;
108
115
}
109
- return trim ? str . toString ( ) . replace ( nonLineFeedWhiteSpaceTrim , '' ) : str . toString ( ) ;
116
+ return str . toString ( ) ;
110
117
}
111
118
112
119
function convertJupyterOutputToBuffer ( mime : string , value : unknown ) : NotebookCellOutputItem {
@@ -289,15 +296,15 @@ export function jupyterCellOutputToCellOutput(output: nbformat.IOutput): Noteboo
289
296
}
290
297
291
298
function createNotebookCellDataFromRawCell ( cell : nbformat . IRawCell ) : NotebookCellData {
292
- const cellData = new NotebookCellData ( NotebookCellKind . Code , concatMultilineString ( cell . source ) , 'raw' ) ;
299
+ const cellData = new NotebookCellData ( NotebookCellKind . Code , concatMultilineCellSource ( cell . source ) , 'raw' ) ;
293
300
cellData . outputs = [ ] ;
294
301
cellData . metadata = getNotebookCellMetadata ( cell ) ;
295
302
return cellData ;
296
303
}
297
304
function createNotebookCellDataFromMarkdownCell ( cell : nbformat . IMarkdownCell ) : NotebookCellData {
298
305
const cellData = new NotebookCellData (
299
306
NotebookCellKind . Markup ,
300
- concatMultilineString ( cell . source ) ,
307
+ concatMultilineCellSource ( cell . source ) ,
301
308
'markdown'
302
309
) ;
303
310
cellData . outputs = [ ] ;
@@ -309,7 +316,7 @@ function createNotebookCellDataFromCodeCell(cell: nbformat.ICodeCell, cellLangua
309
316
const outputs = cellOutputs . map ( jupyterCellOutputToCellOutput ) ;
310
317
const hasExecutionCount = typeof cell . execution_count === 'number' && cell . execution_count > 0 ;
311
318
312
- const source = concatMultilineString ( cell . source ) ;
319
+ const source = concatMultilineCellSource ( cell . source ) ;
313
320
314
321
const executionSummary : NotebookCellExecutionSummary = hasExecutionCount
315
322
? { executionOrder : cell . execution_count as number }
0 commit comments