@@ -68,15 +68,15 @@ function apply(
6868
6969 // biome-ignore lint/complexity/noExcessiveCognitiveComplexity: <explanation>
7070 element . children = [ tree ] . flatMap ( ( tree ) => {
71- const pre = tree . children [ 0 ] ;
71+ const [ pre ] = tree . children ;
7272 const themeNames = getThemeNames ( theme ) ;
7373 const themeNamesString = themeNames . join ( ' ' ) ;
7474
7575 if ( ! ( isElement ( pre ) && pre . properties ) ) {
7676 return [ ] ;
7777 }
7878
79- const code = pre . children [ 0 ] ;
79+ const [ code ] = pre . children ;
8080
8181 // Remove extraneous classes
8282 if (
@@ -352,11 +352,12 @@ export function rehypePrettyCode(
352352 if ( ! isElement ( codeElement ) ) return ;
353353 const [ textElement ] = codeElement . children ;
354354
355- const { title, caption, meta, lang } = parseBlockMetaString (
356- codeElement ,
357- filterMetaString ,
358- defaultCodeBlockLang ,
359- ) ;
355+ const { title, caption, meta, lang, showLineNumbers } =
356+ parseBlockMetaString (
357+ codeElement ,
358+ filterMetaString ,
359+ defaultCodeBlockLang ,
360+ ) ;
360361
361362 if ( ! lang || lang === 'math' ) return ;
362363
@@ -433,25 +434,23 @@ export function rehypePrettyCode(
433434
434435 // biome-ignore lint/complexity/noExcessiveCognitiveComplexity: <explanation>
435436 visit ( codeTree , 'element' , ( element ) => {
436- if ( element . tagName === 'code' ) {
437- const showLineNumbers = / (?: ^ | \s ) s h o w L i n e N u m b e r s (?: \s | $ ) / . test (
438- meta ,
437+ if (
438+ ( element . tagName === 'code' || element . tagName === 'pre' ) &&
439+ showLineNumbers
440+ ) {
441+ if ( element . properties ) {
442+ element . properties [ 'data-line-numbers' ] = '' ;
443+ }
444+
445+ const lineNumbersStartAtMatch = meta . match (
446+ / s h o w L i n e N u m b e r s = ( \d + ) / i,
439447 ) ;
440- if ( showLineNumbers ) {
448+ const startNumberString = lineNumbersStartAtMatch ?. [ 1 ] ;
449+ if ( startNumberString ) {
450+ const startAt = Number ( startNumberString ) - 1 ;
451+ lineNumbersMaxDigits = startAt ;
441452 if ( element . properties ) {
442- element . properties [ 'data-line-numbers' ] = '' ;
443- }
444-
445- const lineNumbersStartAtMatch = meta . match (
446- / s h o w L i n e N u m b e r s = ( \d + ) / ,
447- ) ;
448- const startNumberString = lineNumbersStartAtMatch ?. [ 1 ] ;
449- if ( startNumberString ) {
450- const startAt = Number ( startNumberString ) - 1 ;
451- lineNumbersMaxDigits = startAt ;
452- if ( element . properties ) {
453- element . properties . style = `counter-set: line ${ startAt } ;` ;
454- }
453+ element . properties . style = `counter-set: line ${ startAt } ;` ;
455454 }
456455 }
457456 }
0 commit comments