diff --git a/doc/api/wasi.md b/doc/api/wasi.md index de0e4b8e8138b5..f1ae3ae14f5da3 100644 --- a/doc/api/wasi.md +++ b/doc/api/wasi.md @@ -10,7 +10,7 @@ The WASI API provides an implementation of the [WebAssembly System Interface][] specification. WASI gives sandboxed WebAssembly applications access to the underlying operating system via a collection of POSIX-like functions. -```js esm +```mjs import fs from 'fs'; import { WASI } from 'wasi'; @@ -28,7 +28,7 @@ const instance = await WebAssembly.instantiate(wasm, importObject); wasi.start(instance); ``` -```js cjs +```cjs 'use strict'; const fs = require('fs'); const { WASI } = require('wasi'); diff --git a/doc/api_assets/style.css b/doc/api_assets/style.css index c9f9d457e50a42..fb34bcf12a2195 100644 --- a/doc/api_assets/style.css +++ b/doc/api_assets/style.css @@ -859,7 +859,7 @@ kbd { .js-flavor-selector ~ .cjs { background-image: url(./js-flavor-cjs.svg); } - .js-flavor-selector ~ .esm { + .js-flavor-selector ~ .mjs { background-image: url(./js-flavor-esm.svg); } } diff --git a/test/fixtures/document_with_cjs_and_esm_code_snippet.md b/test/fixtures/document_with_cjs_and_esm_code_snippet.md index 117f9fb1b910b0..aae5ea28353a7d 100644 --- a/test/fixtures/document_with_cjs_and_esm_code_snippet.md +++ b/test/fixtures/document_with_cjs_and_esm_code_snippet.md @@ -2,10 +2,10 @@ CJS snippet is first, it should be the one displayed by default. -```js cjs +```cjs require('path'); ``` -```js esm +```mjs import 'node:url'; ``` \ No newline at end of file diff --git a/test/fixtures/document_with_esm_and_cjs_code_snippet.md b/test/fixtures/document_with_esm_and_cjs_code_snippet.md index 7dd067944d9564..a7e14383762979 100644 --- a/test/fixtures/document_with_esm_and_cjs_code_snippet.md +++ b/test/fixtures/document_with_esm_and_cjs_code_snippet.md @@ -2,10 +2,10 @@ ESM snippet is first, it should be the one displayed by default. -```js esm +```mjs import 'node:url'; ``` -```js cjs +```cjs require('path'); ``` \ No newline at end of file diff --git a/tools/doc/html.js b/tools/doc/html.js index ae49f1ff0cbc8d..67157ba6b5fb09 100644 --- a/tools/doc/html.js +++ b/tools/doc/html.js @@ -187,7 +187,7 @@ function linkJsTypeDocs(text) { return parts.join('`'); } -const isJSFlavorSnippet = (node) => node.lang === 'js' && node.meta; +const isJSFlavorSnippet = (node) => node.lang === 'cjs' || node.lang === 'mjs'; // Preprocess headers, stability blockquotes, and YAML blocks. function preprocessElements({ filename }) { @@ -206,8 +206,11 @@ function preprocessElements({ filename }) { `No language set in ${filename}, ` + `line ${node.position.start.line}`); } + const className = isJSFlavorSnippet(node) ? + `language-js ${node.lang}` : + `language-${node.lang}`; const highlighted = - `` + + `` + (getLanguage(node.lang || '') ? highlight(node.lang, node.value) : node).value + ''; @@ -217,33 +220,26 @@ function preprocessElements({ filename }) { const previousNode = parent.children[index - 1] || {}; const nextNode = parent.children[index + 1] || {}; - if (node.meta !== 'esm' && node.meta !== 'cjs') { - console.warn( - `Unknown JavaScript flavor in ${filename}` + - `:${node.position.start.line}:${node.position.start.column}`); - node.value = `
${highlighted}
`; - node.meta = null; - } else if (!isJSFlavorSnippet(previousNode) && - isJSFlavorSnippet(nextNode) && - nextNode.meta !== node.meta) { + if (!isJSFlavorSnippet(previousNode) && + isJSFlavorSnippet(nextNode) && + nextNode.lang !== node.lang) { + // Saving the highlight code as value to be added in the next node. node.value = highlighted; } else if (isJSFlavorSnippet(previousNode)) { node.value = '
' +
               '' +
               previousNode.value +
               highlighted +
               '
'; - node.meta = null; + node.lang = null; previousNode.value = ''; - previousNode.meta = null; + previousNode.lang = null; } else { - console.warn( - `Unused JavaScript flavored block in ${filename}` + - `:${node.position.start.line}:${node.position.start.column}`); + // Isolated JS snippet, no need to add the checkbox. node.value = `
${highlighted}
`; - node.meta = null; } } else { node.value = `
${highlighted}
`;