@@ -24,12 +24,13 @@ export function concatMultilineString(str: nbformat.MultilineString): string {
24
24
}
25
25
26
26
// Strip out comment lines from code
27
- export function stripComments ( str : nbformat . MultilineString ) : nbformat . MultilineString {
28
- if ( Array . isArray ( str ) ) {
29
- return extractNonComments ( str ) ;
30
- } else {
31
- return extractNonComments ( [ str ] ) ;
32
- }
27
+ export function stripComments ( str : string ) : string {
28
+ let result : string = '' ;
29
+ parseForComments (
30
+ str . splitLines ( { trim : false , removeEmptyEntries : false } ) ,
31
+ ( _s ) => noop ,
32
+ ( s ) => result = result . concat ( `${ s } \n` ) ) ;
33
+ return result ;
33
34
}
34
35
35
36
export function formatStreamText ( str : string ) : string {
@@ -77,6 +78,7 @@ export function generateMarkdownFromCodeLines(lines: string[]) {
77
78
return appendLineFeed ( extractComments ( lines . slice ( 1 ) ) ) ;
78
79
}
79
80
81
+ // tslint:disable-next-line: cyclomatic-complexity
80
82
export function parseForComments (
81
83
lines : string [ ] ,
82
84
foundCommentLine : ( s : string , i : number ) => void ,
@@ -109,15 +111,20 @@ export function parseForComments(
109
111
}
110
112
// Not inside either, see if starting a quote
111
113
} else if ( isMultilineQuote && ! isMultilineComment ) {
112
- insideMultilineQuote = isMultilineQuote ;
114
+ // Make sure doesn't begin and end on the same line.
115
+ const beginQuote = trim . indexOf ( isMultilineQuote ) ;
116
+ const endQuote = trim . lastIndexOf ( isMultilineQuote ) ;
117
+ insideMultilineQuote = endQuote !== beginQuote ? undefined : isMultilineQuote ;
113
118
foundNonCommentLine ( l , pos ) ;
114
119
// Not starting a quote, might be starting a comment
115
120
} else if ( isMultilineComment ) {
116
- insideMultilineComment = isMultilineComment ;
121
+ // See if this line ends the comment too or not
122
+ const endIndex = trim . indexOf ( isMultilineComment , 3 ) ;
123
+ insideMultilineComment = endIndex >= 0 ? undefined : isMultilineComment ;
117
124
118
125
// Might end with text too
119
126
if ( trim . length > 3 ) {
120
- foundCommentLine ( trim . slice ( 3 ) , pos ) ;
127
+ foundCommentLine ( trim . slice ( 3 , endIndex >= 0 ? endIndex : undefined ) , pos ) ;
121
128
}
122
129
} else {
123
130
// Normal line
@@ -136,9 +143,3 @@ function extractComments(lines: string[]): string[] {
136
143
parseForComments ( lines , ( s ) => result . push ( s ) , ( _s ) => noop ( ) ) ;
137
144
return result ;
138
145
}
139
-
140
- function extractNonComments ( lines : string [ ] ) : string [ ] {
141
- const result : string [ ] = [ ] ;
142
- parseForComments ( lines , ( _s ) => noop , ( s ) => result . push ( s ) ) ;
143
- return result ;
144
- }
0 commit comments