@@ -187,7 +187,7 @@ function linkJsTypeDocs(text) {
187187 return parts . join ( '`' ) ;
188188}
189189
190- const isJSFlavorSnippet = ( node ) => node . lang === 'js' && node . meta ;
190+ const isJSFlavorSnippet = ( node ) => node . lang === 'cjs' || node . lang === 'mjs' ;
191191
192192// Preprocess headers, stability blockquotes, and YAML blocks.
193193function preprocessElements ( { filename } ) {
@@ -206,8 +206,11 @@ function preprocessElements({ filename }) {
206206 `No language set in ${ filename } , ` +
207207 `line ${ node . position . start . line } ` ) ;
208208 }
209+ const className = isJSFlavorSnippet ( node ) ?
210+ `language-js ${ node . lang } ` :
211+ `language-${ node . lang } ` ;
209212 const highlighted =
210- `<code class='language- ${ node . lang } ${ node . meta } '>` +
213+ `<code class='${ className } '>` +
211214 ( getLanguage ( node . lang || '' ) ?
212215 highlight ( node . lang , node . value ) : node ) . value +
213216 '</code>' ;
@@ -217,33 +220,26 @@ function preprocessElements({ filename }) {
217220 const previousNode = parent . children [ index - 1 ] || { } ;
218221 const nextNode = parent . children [ index + 1 ] || { } ;
219222
220- if ( node . meta !== 'esm' && node . meta !== 'cjs' ) {
221- console . warn (
222- `Unknown JavaScript flavor in ${ filename } ` +
223- `:${ node . position . start . line } :${ node . position . start . column } ` ) ;
224- node . value = `<pre>${ highlighted } </pre>` ;
225- node . meta = null ;
226- } else if ( ! isJSFlavorSnippet ( previousNode ) &&
227- isJSFlavorSnippet ( nextNode ) &&
228- nextNode . meta !== node . meta ) {
223+ if ( ! isJSFlavorSnippet ( previousNode ) &&
224+ isJSFlavorSnippet ( nextNode ) &&
225+ nextNode . lang !== node . lang ) {
226+ // Saving the highlight code as value to be added in the next node.
229227 node . value = highlighted ;
230228 } else if ( isJSFlavorSnippet ( previousNode ) ) {
231229 node . value = '<pre>' +
232230 '<input class="js-flavor-selector" type="checkbox"' +
233- ( node . meta === 'cjs' ? ' checked' : '' ) +
231+ // If CJS comes in second, ESM should display by default.
232+ ( node . lang === 'cjs' ? ' checked' : '' ) +
234233 ' aria-label="Show modern ES modules syntax">' +
235234 previousNode . value +
236235 highlighted +
237236 '</pre>' ;
238- node . meta = null ;
237+ node . lang = null ;
239238 previousNode . value = '' ;
240- previousNode . meta = null ;
239+ previousNode . lang = null ;
241240 } else {
242- console . warn (
243- `Unused JavaScript flavored block in ${ filename } ` +
244- `:${ node . position . start . line } :${ node . position . start . column } ` ) ;
241+ // Isolated JS snippet, no need to add the checkbox.
245242 node . value = `<pre>${ highlighted } </pre>` ;
246- node . meta = null ;
247243 }
248244 } else {
249245 node . value = `<pre>${ highlighted } </pre>` ;
0 commit comments