@@ -29,13 +29,15 @@ WrappedLine.propTypes = {
2929// Final Styled Output
3030// Includes Highlighted | Overlayed | null styled lines
3131const StructuredOutput = ( { gutteredInput, highlights = [ ] } ) =>
32- gutteredInput . map ( ( ac , idx ) => (
33- < WrappedLine
34- key = { `cm-wrapped-${ idx } ` }
35- child = { ac }
36- className = { [ 'cm-linerow' , highlights . length ? highlights [ idx ] : '' ] . join ( ' ' ) }
37- />
38- ) ) ;
32+ gutteredInput . map ( ( ac , idx ) => {
33+ return (
34+ < WrappedLine
35+ key = { `cm-wrapped-${ idx } ` }
36+ child = { ac }
37+ className = { [ 'cm-linerow' , highlights . length ? highlights [ idx ] : '' ] . join ( ' ' ) }
38+ />
39+ ) ;
40+ } ) ;
3941
4042StructuredOutput . propTypes = {
4143 gutteredInput : PropTypes . any ,
@@ -68,23 +70,51 @@ const highlightedLines = (ranges, totalLength) => {
6870} ;
6971
7072const StyledSyntaxHighlighter = ( { output, ranges } ) => {
73+ const lineBreakRegex = / \r ? \n / ;
7174 const gutteredOutput = [ ] ;
7275 let bucket = [ ] ;
7376 let lineNumber = 1 ;
7477
78+ const incrementLine = newLine => {
79+ // If new line, we'll manually add it to a bucket
80+ if ( newLine ) bucket . push ( '\n' ) ;
81+ // Regardless, we'll add the bucket to our larger output and start a new numbered div
82+ gutteredOutput . push ( bucket ) ;
83+ lineNumber += 1 ;
84+ bucket = [ defaultLineJsx ( lineNumber ) ] ;
85+ } ;
86+
87+ const enumerateMatches = ( o , final ) => {
88+ // Case where a single line break
89+ if ( o . length === 1 ) {
90+ incrementLine ( true ) ;
91+ // Case with multiple consecutive line breaks
92+ } else {
93+ const matches = o . split ( lineBreakRegex ) ;
94+ matches . forEach ( m => {
95+ if ( m . length ) {
96+ bucket . push ( m ) ;
97+ // If we've found a match right before the end of our output, we pad it
98+ if ( final ) incrementLine ( ) ;
99+ } else {
100+ incrementLine ( true ) ;
101+ }
102+ } ) ;
103+ }
104+ } ;
105+
75106 output . unshift ( defaultLineJsx ( 1 ) ) ;
76107 output . forEach ( ( o , idx ) => {
77- const lineBreakRegex = / \n / g;
78-
79108 if ( idx === output . length - 1 ) {
80- bucket . push ( o ) ;
81- gutteredOutput . push ( bucket ) ;
109+ if ( typeof o === 'string' ) enumerateMatches ( o , true ) ;
110+ else {
111+ bucket . push ( o ) ;
112+ gutteredOutput . push ( bucket ) ;
113+ }
82114 } else if ( ! lineBreakRegex . test ( o ) ) {
83115 bucket . push ( o ) ;
84- } else {
85- gutteredOutput . push ( bucket ) ;
86- lineNumber += 1 ;
87- bucket = [ defaultLineJsx ( lineNumber ) ] ;
116+ } else if ( typeof o === 'string' ) {
117+ enumerateMatches ( o ) ;
88118 }
89119 } ) ;
90120
0 commit comments