@@ -33,6 +33,9 @@ import { visit } from 'unist-util-visit';
33
33
34
34
import * as common from './common.mjs' ;
35
35
import * as typeParser from './type-parser.mjs' ;
36
+ import buildCSSForFlavoredJS from './buildCSSForFlavoredJS.mjs' ;
37
+
38
+ const dynamicSizes = Object . create ( null ) ;
36
39
37
40
const { highlight, getLanguage } = highlightJs ;
38
41
@@ -90,6 +93,8 @@ function processContent(content) {
90
93
}
91
94
92
95
export function toHTML ( { input, content, filename, nodeVersion, versions } ) {
96
+ const dynamicSizesForThisFile = dynamicSizes [ filename ] ;
97
+
93
98
filename = path . basename ( filename , '.md' ) ;
94
99
95
100
const id = filename . replace ( / \W + / g, '-' ) ;
@@ -99,6 +104,7 @@ export function toHTML({ input, content, filename, nodeVersion, versions }) {
99
104
. replace ( '__SECTION__' , content . section )
100
105
. replace ( / _ _ V E R S I O N _ _ / g, nodeVersion )
101
106
. replace ( / _ _ T O C _ _ / g, content . toc )
107
+ . replace ( '__JS_FLAVORED_DYNAMIC_CSS__' , buildCSSForFlavoredJS ( dynamicSizesForThisFile ) )
102
108
. replace ( / _ _ T O C _ P I C K E R _ _ / g, tocPicker ( id , content ) )
103
109
. replace ( / _ _ G T O C _ P I C K E R _ _ / g, gtocPicker ( id ) )
104
110
. replace ( / _ _ G T O C _ _ / g, gtocHTML . replace (
@@ -228,14 +234,19 @@ export function preprocessElements({ filename }) {
228
234
const previousNode = parent . children [ index - 1 ] || { } ;
229
235
const nextNode = parent . children [ index + 1 ] || { } ;
230
236
237
+ const charCountFirstTwoLines = Math . max ( ...node . value . split ( '\n' , 2 ) . map ( ( str ) => str . length ) ) ;
238
+
231
239
if ( ! isJSFlavorSnippet ( previousNode ) &&
232
240
isJSFlavorSnippet ( nextNode ) &&
233
241
nextNode . lang !== node . lang ) {
234
242
// Saving the highlight code as value to be added in the next node.
235
243
node . value = highlighted ;
244
+ node . charCountFirstTwoLines = charCountFirstTwoLines ;
236
245
} else if ( isJSFlavorSnippet ( previousNode ) &&
237
246
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">` +
239
250
'<input class="js-flavor-selector" type="checkbox"' +
240
251
// If CJS comes in second, ESM should display by default.
241
252
( node . lang === 'cjs' ? ' checked' : '' ) +
0 commit comments