forked from EnterpriseDB/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode-block.js
More file actions
196 lines (170 loc) · 5.35 KB
/
code-block.js
File metadata and controls
196 lines (170 loc) · 5.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import React, { useState } from "react";
import { Button } from "react-bootstrap";
import { Tooltip } from "react-bootstrap";
import { OverlayTrigger } from "react-bootstrap";
import { useLocation } from "@gatsbyjs/reach-router";
const childToString = (child) => {
if (typeof child === "string") {
return child; // hit string, unroll
} else if (child && child.props) {
return childToString(child.props.children);
}
return "";
};
const popExtraNewLines = (code) => {
while (
code.length - 1 > 0 &&
childToString(code[code.length - 1]).trim() === ""
) {
code.pop();
}
};
const splitChildrenIntoCodeAndOutput = (rawChildren, urlpath) => {
if (!rawChildren) {
return [[], []];
}
// Simplified regex to split on the __OUTPUT__ marker
const splitRegex = /(?:\n|^)[ \t\f\v]*__OUTPUT__[ \t\f\v]*(?:\n|$)/;
const code = [];
const output = [];
const children = Array.isArray(rawChildren) ? rawChildren : [rawChildren];
let splitFound = false;
for (let i = 0; i < children.length; i++) {
const child = children[i];
if (splitFound) {
// we've already split, toss it into output and move on
output.push(childToString(child));
// warn if we do find another __OUTPUT__ marker - this is likely a mistake
if (splitRegex.test(output.at(-1)))
console.warn(
urlpath +
": Found another __OUTPUT__ marker after the first one, this is likely a mistake.",
);
continue;
}
const sChild = childToString(child);
const splitChild = sChild.split(splitRegex);
if (splitChild.length > 1) {
// found split location
code.push(splitChild[0].replace(/(\n|\r\n)*$/g, "")); // will convert token to pure text, seems to be okay in practice
output.push(splitChild[1].replace(/^(\n|\r\n)*/g, ""));
popExtraNewLines(code);
splitFound = true;
} else {
code.push(child);
}
}
return [code, output];
};
const unwrappedstring = "→ Wrap";
const wrappedstring = "↩ Unwrap";
const CodePre = ({ className, content, runnable, startWrapped }) => {
const codeRef = React.createRef();
const [wrapButtonText, setWrapButtonText] = useState(
!startWrapped ? unwrappedstring : wrappedstring,
);
const [copyButtonText, setCopyButtonText] = useState("Copy");
const copyClick = (e) => {
const text = codeRef.current && codeRef.current.textContent;
navigator.clipboard.writeText(text).then(() => {
setCopyButtonText("Copied!");
setTimeout(() => {
setCopyButtonText("Copy");
}, 3000);
});
e.target.blur();
};
const [wrap, setWrap] = useState(startWrapped);
const wrapClick = (e) => {
setWrap(!wrap);
if (!wrap) {
setWrapButtonText(wrappedstring);
} else {
setWrapButtonText(unwrappedstring);
}
e.target.blur();
};
const [canRun, setCanRun] = useState(true);
const runClick = (e) => {
const text = codeRef.current && codeRef.current.textContent;
window.katacoda.write(text);
setCanRun(false);
setTimeout(() => {
setCanRun(true);
}, 3000);
e.target.blur();
};
return (
<>
<div className="codeblock-controls d-flex">
<div>
<OverlayTrigger
delay={{ hide: 450, show: 300 }}
overlay={(props) => <Tooltip {...props}>Toggle wrapping</Tooltip>}
placement="bottom"
>
<Button size="sm" variant="link" onClick={wrapClick}>
{wrapButtonText}
</Button>
</OverlayTrigger>
<Button size="sm" variant="link" onClick={copyClick}>
{copyButtonText}
</Button>
</div>
{runnable && (
<Button
size="sm"
variant="outline-info"
className="katacoda-exec-button"
onClick={runClick}
disabled={!canRun}
>
► Run
</Button>
)}
</div>
<pre
className={`${className} ${wrap && "ws-prewrap"} m-0 br-tl-0 br-tr-0`}
ref={codeRef}
>
{content}
</pre>
</>
);
};
const OutputPre = ({ content }) => (
<div className="mt-1 output-block">
<div className="codeblock-controls output-label ps-2 pe-2 pt-2">Output</div>
<pre className="language-text m-0 br-tl-0 br-tr-0">{content}</pre>
</div>
);
const CodeBlock = ({ children, codeLanguages, ...otherProps }) => {
const location = useLocation();
const childIsComponent = !!children.props; // true in normal usage, false if raw <pre> tags are used
const [codeContent, outputContent] = childIsComponent
? splitChildrenIntoCodeAndOutput(children.props.children, location.pathname)
: [children, ""];
const startWrapped = false;
const language = childIsComponent
? (children.props.className || "").replace("language-", "")
: "text";
const execLanguages = codeLanguages
? ["shell"].concat(codeLanguages?.split(",")?.map((l) => l.trim()))
: [];
if (codeContent.length > 0) {
return (
<figure className="codeblock-wrapper katacoda-enabled">
<CodePre
className={`language-${language}`}
content={codeContent}
runnable={execLanguages.includes(language)}
startWrapped={startWrapped}
/>
{outputContent.length > 0 && <OutputPre content={outputContent} />}
</figure>
);
} else {
return null;
}
};
export default CodeBlock;