Skip to content

Commit 4ee5461

Browse files
authored
book/super-example: hide input div when empty (#6359)
1 parent 75586ff commit 4ee5461

File tree

2 files changed

+35
-15
lines changed

2 files changed

+35
-15
lines changed

book/super-example/super-example.css

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,16 @@
8181
'input result';
8282
border-radius: var(--radius);
8383

84+
&[data-layout='no-input'] {
85+
grid-template-columns: minmax(0, 1fr);
86+
grid-template-areas:
87+
'query'
88+
'result';
89+
.input {
90+
display: none;
91+
}
92+
}
93+
8494
&[data-layout='stacked'] {
8595
grid-template-columns: minmax(0, 1fr);
8696
grid-template-areas:

book/super-example/super-example.js

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,29 @@
11
import { AriaTabs } from './aria-tabs';
2-
import { SuperPlayground } from './super-playground'
2+
import { SuperPlayground } from './super-playground';
33

44
const preNodes = document.querySelectorAll('pre:has(> code.language-mdtest-spq)');
55
for (const [i, pre] of preNodes.entries()) {
6-
const codeNode = pre.querySelector('code')
7-
const attributes = Array.from(codeNode.classList)
6+
const codeNode = pre.querySelector('code');
7+
8+
// Matches one or more "#"-prefixed lines.
9+
const sectionSeparatorRE = /(?m:^#.*\n)+/;
10+
const sections = codeNode.innerText.split(sectionSeparatorRE);
11+
// Ignore sections[0], which should be empty.
12+
if (sections.length != 4) {
13+
continue;
14+
}
15+
const spq = sections[1].trim();
16+
const input = sections[2].trim();
17+
const expected = sections[3].trim();
18+
19+
let attributes = Array.from(codeNode.classList)
820
.filter((c) => c.match(/^{.*}$/))
921
.map((c) => c.slice(1, -1))
10-
.join(' ')
22+
.join(' ');
23+
if (input.length === 0) {
24+
attributes += 'data-layout="no-input"';
25+
}
26+
1127
const html = `
1228
<article class="super-example">
1329
<nav role="tablist">
@@ -63,23 +79,17 @@ for (const [i, pre] of preNodes.entries()) {
6379
const tablist = node.querySelector('[role="tablist"]');
6480
AriaTabs.setup(tablist);
6581

66-
// Matches one or more "#"-prefixed lines.
67-
const sectionSeparatorRE = /(?m:^#.*\n)+/;
68-
const sections = codeNode.innerText.split(sectionSeparatorRE);
69-
// Ignore sections[0], which should be empty.
70-
if (sections.length != 4) {
71-
continue;
72-
}
73-
const spq = sections[1].trim();
74-
const input = sections[2].trim();
75-
const expected = sections[3].trim();
7682
node.querySelector('.super-playground .query code').textContent = spq;
7783
node.querySelector('.super-playground .input code').textContent = input;
7884
node.querySelector('.super-playground .result code').textContent = expected;
7985

8086
const commandCode = node.querySelector('.super-command code')
8187
SuperPlayground.setup(node, (query, input) => {
82-
commandCode.textContent = `echo '${input}' \\\n| super -s -c '${query}' -`
88+
let command = `super -s -c '${query}'`;
89+
if (input.length > 0) {
90+
command = `echo '${input}' \\\n| ${command} -`;
91+
}
92+
commandCode.textContent = command;
8393
});
8494

8595
// Prevent keydown from bubbling up to book.js listeners.

0 commit comments

Comments
 (0)