@@ -33,6 +33,9 @@ import { visit } from 'unist-util-visit';
3333
3434import * as common from './common.mjs' ;
3535import * as typeParser from './type-parser.mjs' ;
36+ import buildCSSForFlavoredJS from './buildCSSForFlavoredJS.mjs' ;
37+
38+ const dynamicSizes = Object . create ( null ) ;
3639
3740const { highlight, getLanguage } = highlightJs ;
3841
@@ -90,6 +93,8 @@ function processContent(content) {
9093}
9194
9295export function toHTML ( { input, content, filename, nodeVersion, versions } ) {
96+ const dynamicSizesForThisFile = dynamicSizes [ filename ] ;
97+
9398 filename = path . basename ( filename , '.md' ) ;
9499
95100 const id = filename . replace ( / \W + / g, '-' ) ;
@@ -99,6 +104,7 @@ export function toHTML({ input, content, filename, nodeVersion, versions }) {
99104 . replace ( '__SECTION__' , content . section )
100105 . replace ( / _ _ V E R S I O N _ _ / g, nodeVersion )
101106 . replace ( / _ _ T O C _ _ / g, content . toc )
107+ . replace ( '__JS_FLAVORED_DYNAMIC_CSS__' , buildCSSForFlavoredJS ( dynamicSizesForThisFile ) )
102108 . replace ( / _ _ T O C _ P I C K E R _ _ / g, tocPicker ( id , content ) )
103109 . replace ( / _ _ G T O C _ P I C K E R _ _ / g, gtocPicker ( id ) )
104110 . replace ( / _ _ G T O C _ _ / g, gtocHTML . replace (
@@ -228,14 +234,19 @@ export function preprocessElements({ filename }) {
228234 const previousNode = parent . children [ index - 1 ] || { } ;
229235 const nextNode = parent . children [ index + 1 ] || { } ;
230236
237+ const charCountFirstTwoLines = Math . max ( ...node . value . split ( '\n' , 2 ) . map ( ( str ) => str . length ) ) ;
238+
231239 if ( ! isJSFlavorSnippet ( previousNode ) &&
232240 isJSFlavorSnippet ( nextNode ) &&
233241 nextNode . lang !== node . lang ) {
234242 // Saving the highlight code as value to be added in the next node.
235243 node . value = highlighted ;
244+ node . charCountFirstTwoLines = charCountFirstTwoLines ;
236245 } else if ( isJSFlavorSnippet ( previousNode ) &&
237246 previousNode . lang !== node . lang ) {
238- node . value = '<pre>' +
247+ const actualCharCount = Math . max ( charCountFirstTwoLines , previousNode . charCountFirstTwoLines ) ;
248+ ( dynamicSizes [ filename ] ??= new Set ( ) ) . add ( actualCharCount ) ;
249+ node . value = `<pre class="with-${ actualCharCount } -chars">` +
239250 '<input class="js-flavor-selector" type="checkbox"' +
240251 // If CJS comes in second, ESM should display by default.
241252 ( node . lang === 'cjs' ? ' checked' : '' ) +
0 commit comments