@@ -116,12 +116,13 @@ export const rehypeSyntaxHighlighting: Plugin<[RehypeSyntaxHighlightingOptions?]
116116 if ( ! codeElement ) return ;
117117
118118 let lineNumber = 0 ;
119- visit ( codeElement , 'element' , ( span , _ , spanParent ) => {
119+ visit ( codeElement , 'element' , ( span , spanIndex , spanParent ) => {
120120 if (
121121 ! spanParent ||
122122 spanParent . type !== 'element' ||
123123 spanParent . tagName !== 'code' ||
124124 span . tagName !== 'span' ||
125+ ( ! span . children . length && spanIndex === spanParent . children . length - 1 ) ||
125126 ( typeof span . properties . class !== 'string' && ! Array . isArray ( span . properties . class ) ) ||
126127 ! span . properties . class . includes ( 'line' )
127128 ) {
@@ -138,11 +139,19 @@ export const rehypeSyntaxHighlighting: Plugin<[RehypeSyntaxHighlightingOptions?]
138139 }
139140 } ) ;
140141
141- if ( node . data ?. meta ) {
142+ const preChild = codeElement . children [ 0 ] as Element ;
143+ const numberOfLines = lineNumber ;
144+
145+ node . data = node . data ?? { } ;
146+ if ( node . data . meta ) {
142147 node . data . meta = node . data . meta . replace ( lineHighlightPattern , '' ) . trim ( ) ;
143148 }
144149 codeElement . data = node . data ;
145- if ( codeElement . children [ 0 ] ) codeElement . children [ 0 ] . data = node . data ;
150+ codeElement . properties . numberOfLines = numberOfLines ;
151+ if ( preChild ) {
152+ preChild . data = node . data ;
153+ preChild . properties . numberOfLines = numberOfLines ;
154+ }
146155 parent . children . splice ( index , 1 , codeElement ) ;
147156 } catch ( err ) {
148157 if ( options . ignoreMissing && / U n k n o w n l a n g u a g e / . test ( ( err as Error ) . message ) ) {
0 commit comments