Skip to content

Commit 886be29

Browse files
committed
fixup! tools,doc: add support for several flavors of JS code snippets
1 parent 0ecaf39 commit 886be29

File tree

5 files changed

+21
-25
lines changed

5 files changed

+21
-25
lines changed

doc/api/wasi.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ The WASI API provides an implementation of the [WebAssembly System Interface][]
1010
specification. WASI gives sandboxed WebAssembly applications access to the
1111
underlying operating system via a collection of POSIX-like functions.
1212

13-
```js esm
13+
```mjs
1414
import fs from 'fs';
1515
import { WASI } from 'wasi';
1616

@@ -28,7 +28,7 @@ const instance = await WebAssembly.instantiate(wasm, importObject);
2828

2929
wasi.start(instance);
3030
```
31-
```js cjs
31+
```cjs
3232
'use strict';
3333
const fs = require('fs');
3434
const { WASI } = require('wasi');

doc/api_assets/style.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -859,7 +859,7 @@ kbd {
859859
.js-flavor-selector ~ .cjs {
860860
background-image: url(./js-flavor-cjs.svg);
861861
}
862-
.js-flavor-selector ~ .esm {
862+
.js-flavor-selector ~ .mjs {
863863
background-image: url(./js-flavor-esm.svg);
864864
}
865865
}

test/fixtures/document_with_cjs_and_esm_code_snippet.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
CJS snippet is first, it should be the one displayed by default.
44

5-
```js cjs
5+
```cjs
66
require('path');
77
```
88

9-
```js esm
9+
```mjs
1010
import 'node:url';
1111
```

test/fixtures/document_with_esm_and_cjs_code_snippet.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
ESM snippet is first, it should be the one displayed by default.
44

5-
```js esm
5+
```mjs
66
import 'node:url';
77
```
88

9-
```js cjs
9+
```cjs
1010
require('path');
1111
```

tools/doc/html.js

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
193193
function 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

Comments
 (0)