11type ParsedSection = {
22 content : string ;
33 style : string ;
4+ line : number ;
45} ;
56
67function parseStyles (
@@ -12,7 +13,7 @@ function parseStyles(
1213
1314 /** Creates a function that turns lines into a style tree based on a given regex */
1415 function createLineParser ( matcher : RegExp , stylename : string ) {
15- return ( line : string | ParsedSection | ParsedSection [ ] ) => {
16+ return ( line : string | ParsedSection | ParsedSection [ ] , index : number ) => {
1617 // Section has already been parsed
1718 if ( typeof line !== 'string' ) return line ;
1819 const captureGroups = matcher . exec ( line ) ;
@@ -31,10 +32,10 @@ function parseStyles(
3132 if ( content === '' ) return null ;
3233 // The content is what matched the regex, it's the styled content
3334 if ( content === captureGroups [ 1 ] ) {
34- return { content, style : stylename } ;
35+ return { line : index , content, style : stylename } ;
3536 }
3637 // It's unstyled
37- return { content, style : 'regular' } ;
38+ return { line : index , content, style : 'regular' } ;
3839 } )
3940 . filter ( Boolean ) as ParsedSection [ ]
4041 ) ;
@@ -47,15 +48,19 @@ function parseStyles(
4748 ] ;
4849
4950 // Parse all the styles
50- let styles : ( string | ParsedSection | ParsedSection [ ] ) [ ] = inputLines ;
51+ let styles = inputLines ;
5152 for ( const { matcher, stylename } of parsers ) {
5253 const parser = createLineParser ( matcher , stylename ) ;
5354 styles = styles . map ( parser ) ;
5455 }
5556
56- return styles . map ( ( line ) =>
57- typeof line === 'string' ? { content : line , style : 'regular' } : line
57+ const parsedStyles = styles . map ( ( line , index ) =>
58+ typeof line === 'string'
59+ ? [ { content : line , style : 'regular' , line : index } ]
60+ : line
5861 ) ;
62+
63+ return parsedStyles . flat ( ) ;
5964}
6065
6166// '_npmVersion' is replaced with value from package.json
0 commit comments