Skip to content

Commit d20c651

Browse files
authored
Merge pull request WATonomous#52 from WATonomous/feat/51-use-react-markdown
fix(WATonomous#51): Convert server instructions to markdown
2 parents 4db6343 + ca59c8c commit d20c651

File tree

6 files changed

+1729
-1128
lines changed

6 files changed

+1729
-1128
lines changed

components/Check.js

Lines changed: 20 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4,38 +4,27 @@ import { PauseIcon } from '@heroicons/react/outline';
44
import { ExclamationCircleIcon } from '@heroicons/react/outline';
55
import { ClockIcon } from '@heroicons/react/solid';
66
import { Modal, Button } from 'react-bootstrap';
7-
import { CopyBlock, atomOneLight } from 'react-code-blocks';
87
import moment from 'moment';
98
import { useState } from 'react';
9+
import ReactMarkdown from 'react-markdown';
10+
import CodeBlock from '../components/CodeBlock';
1011

1112
const getInstructionBody = (name, machineName) => {
12-
if (name === 'Bastion') {
13-
return (
14-
<>
15-
<CopyBlock
16-
text={`ssh <username>@bastion.watonomous.ca`}
17-
language="shell"
18-
theme={atomOneLight}
19-
showLineNumbers={false}
20-
codeBlock
21-
/>
22-
</>
23-
);
24-
} else {
25-
return (
26-
<>
27-
<div>
28-
<CopyBlock
29-
text={`ssh -i </path/to/ssh_key> -J <username>@bastion.watonomous.ca <username>@${machineName}`}
30-
language="shell"
31-
theme={atomOneLight}
32-
showLineNumbers={false}
33-
codeBlock
34-
/>
35-
</div>
36-
</>
37-
);
38-
}
13+
const sshInstructions =
14+
name === 'Bastion'
15+
? `ssh <username>@bastion.watonomous.ca`
16+
: `ssh -i </path/to/ssh_key> -J <username>@bastion.watonomous.ca <username>@${machineName}`;
17+
const instructionBody = `
18+
Access ${name}${
19+
name !== 'Bastion'
20+
? ' by using Bastion as an [SSH jumphost](https://www.tecmint.com/access-linux-server-using-a-jump-host/)'
21+
: ''
22+
}:
23+
~~~shell
24+
${sshInstructions}
25+
~~~
26+
`;
27+
return instructionBody;
3928
};
4029

4130
const Check = ({ name, checksData, FQDN, machineName }) => {
@@ -121,15 +110,9 @@ const Check = ({ name, checksData, FQDN, machineName }) => {
121110
<Modal.Title>{name}</Modal.Title>
122111
</Modal.Header>
123112
<Modal.Body>
124-
Access {name} by using Bastion as an{' '}
125-
<a
126-
className="text-blue-500"
127-
href="https://www.tecmint.com/access-linux-server-using-a-jump-host/"
128-
>
129-
SSH jumphost
130-
</a>
131-
: <br /> <br />
132-
{getInstructionBody(name, machineName)}
113+
<ReactMarkdown components={{ code: CodeBlock }}>
114+
{getInstructionBody(name, machineName)}
115+
</ReactMarkdown>
133116
</Modal.Body>
134117
<Modal.Footer>
135118
<Button variant="secondary" onClick={handleClose}>

components/CodeBlock.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { useState } from 'react';
2+
import { ClipboardListIcon } from '@heroicons/react/outline';
3+
import { ClipboardCheckIcon } from '@heroicons/react/solid';
4+
import { Prism as SyntaxHighlighter } from 'react-syntax-highlighter';
5+
import { prism } from 'react-syntax-highlighter/dist/cjs/styles/prism';
6+
7+
function CodeBlock({ node, inline, className, children, ...props }) {
8+
const [copied, setCopied] = useState(false);
9+
const match = /language-(\w+)/.exec(className || '');
10+
const code = String(children).replace(/\n$/, '');
11+
const codeBlock =
12+
!inline && match ? (
13+
<SyntaxHighlighter style={prism} language={match[1]} {...props}>
14+
{code}
15+
</SyntaxHighlighter>
16+
) : (
17+
<code className={className} {...props}>
18+
{children}
19+
</code>
20+
);
21+
22+
return (
23+
<div className="relative">
24+
{codeBlock}
25+
<button
26+
className="absolute top-2.5 right-1 p-2 bg-gray-100 rounded"
27+
onClick={() => {
28+
navigator.clipboard.writeText(code);
29+
setCopied(true);
30+
}}
31+
>
32+
{copied ? (
33+
<ClipboardCheckIcon className="h-5 w-5 text-green-500" />
34+
) : (
35+
<ClipboardListIcon className="h-5 w-5 text-black" />
36+
)}
37+
</button>
38+
</div>
39+
);
40+
}
41+
42+
export default CodeBlock;

0 commit comments

Comments
 (0)