@@ -116,12 +116,13 @@ export const rehypeSyntaxHighlighting: Plugin<[RehypeSyntaxHighlightingOptions?]
116
116
if ( ! codeElement ) return ;
117
117
118
118
let lineNumber = 0 ;
119
- visit ( codeElement , 'element' , ( span , _ , spanParent ) => {
119
+ visit ( codeElement , 'element' , ( span , spanIndex , spanParent ) => {
120
120
if (
121
121
! spanParent ||
122
122
spanParent . type !== 'element' ||
123
123
spanParent . tagName !== 'code' ||
124
124
span . tagName !== 'span' ||
125
+ ( ! span . children . length && spanIndex === spanParent . children . length - 1 ) ||
125
126
( typeof span . properties . class !== 'string' && ! Array . isArray ( span . properties . class ) ) ||
126
127
! span . properties . class . includes ( 'line' )
127
128
) {
@@ -138,11 +139,19 @@ export const rehypeSyntaxHighlighting: Plugin<[RehypeSyntaxHighlightingOptions?]
138
139
}
139
140
} ) ;
140
141
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 ) {
142
147
node . data . meta = node . data . meta . replace ( lineHighlightPattern , '' ) . trim ( ) ;
143
148
}
144
149
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
+ }
146
155
parent . children . splice ( index , 1 , codeElement ) ;
147
156
} catch ( err ) {
148
157
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