@@ -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 (
@@ -353,11 +353,12 @@ export function rehypePrettyCode(
353353 if ( ! isElement ( codeElement ) ) return ;
354354 const [ textElement ] = codeElement . children ;
355355
356- const { title, caption, meta, lang } = parseBlockMetaString (
357- codeElement ,
358- filterMetaString ,
359- defaultCodeBlockLang ,
360- ) ;
356+ const { title, caption, meta, lang, showLineNumbers } =
357+ parseBlockMetaString (
358+ codeElement ,
359+ filterMetaString ,
360+ defaultCodeBlockLang ,
361+ ) ;
361362
362363 if ( ! lang || lang === 'math' ) return ;
363364
@@ -434,25 +435,23 @@ export function rehypePrettyCode(
434435
435436 // biome-ignore lint/complexity/noExcessiveCognitiveComplexity: <explanation>
436437 visit ( codeTree , 'element' , ( element ) => {
437- if ( element . tagName === 'code' ) {
438- const showLineNumbers = / (?: ^ | \s ) s h o w L i n e N u m b e r s (?: \s | $ ) / . test (
439- meta ,
438+ if (
439+ ( element . tagName === 'code' || element . tagName === 'pre' ) &&
440+ showLineNumbers
441+ ) {
442+ if ( element . properties ) {
443+ element . properties [ 'data-line-numbers' ] = '' ;
444+ }
445+
446+ const lineNumbersStartAtMatch = meta . match (
447+ / s h o w L i n e N u m b e r s = ( \d + ) / i,
440448 ) ;
441- if ( showLineNumbers ) {
449+ const startNumberString = lineNumbersStartAtMatch ?. [ 1 ] ;
450+ if ( startNumberString ) {
451+ const startAt = Number ( startNumberString ) - 1 ;
452+ lineNumbersMaxDigits = startAt ;
442453 if ( element . properties ) {
443- element . properties [ 'data-line-numbers' ] = '' ;
444- }
445-
446- const lineNumbersStartAtMatch = meta . match (
447- / s h o w L i n e N u m b e r s = ( \d + ) / ,
448- ) ;
449- const startNumberString = lineNumbersStartAtMatch ?. [ 1 ] ;
450- if ( startNumberString ) {
451- const startAt = Number ( startNumberString ) - 1 ;
452- lineNumbersMaxDigits = startAt ;
453- if ( element . properties ) {
454- element . properties . style = `counter-set: line ${ startAt } ;` ;
455- }
454+ element . properties . style = `counter-set: line ${ startAt } ;` ;
456455 }
457456 }
458457 }
0 commit comments