@@ -24,12 +24,13 @@ export function concatMultilineString(str: nbformat.MultilineString): string {
2424}
2525
2626// 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 ;
3334}
3435
3536export function formatStreamText ( str : string ) : string {
@@ -77,6 +78,7 @@ export function generateMarkdownFromCodeLines(lines: string[]) {
7778 return appendLineFeed ( extractComments ( lines . slice ( 1 ) ) ) ;
7879}
7980
81+ // tslint:disable-next-line: cyclomatic-complexity
8082export function parseForComments (
8183 lines : string [ ] ,
8284 foundCommentLine : ( s : string , i : number ) => void ,
@@ -109,15 +111,20 @@ export function parseForComments(
109111 }
110112 // Not inside either, see if starting a quote
111113 } 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 ;
113118 foundNonCommentLine ( l , pos ) ;
114119 // Not starting a quote, might be starting a comment
115120 } 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 ;
117124
118125 // Might end with text too
119126 if ( trim . length > 3 ) {
120- foundCommentLine ( trim . slice ( 3 ) , pos ) ;
127+ foundCommentLine ( trim . slice ( 3 , endIndex >= 0 ? endIndex : undefined ) , pos ) ;
121128 }
122129 } else {
123130 // Normal line
@@ -136,9 +143,3 @@ function extractComments(lines: string[]): string[] {
136143 parseForComments ( lines , ( s ) => result . push ( s ) , ( _s ) => noop ( ) ) ;
137144 return result ;
138145}
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