Skip to content

Commit

Permalink
refactor: move syntax highlighter inline
Browse files Browse the repository at this point in the history
  • Loading branch information
jerelmiller committed Oct 15, 2020
1 parent 6965354 commit 9482d11
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 51 deletions.
35 changes: 30 additions & 5 deletions src/components/Terminal/Terminal.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@ import React from 'react';
import PropTypes from 'prop-types';
import { css } from '@emotion/core';
import { Button, useClipboard } from '@newrelic/gatsby-theme-newrelic';
import SyntaxHighlighter from './SyntaxHighlighter';
import Cursor from './Cursor';
import Highlight from 'prism-react-renderer';
import Prism from 'prismjs';
import Command from './Command';
import Output from './Output';
import theme from './theme';
import rollupIntoCommands from './rollupIntoCommands';

const Terminal = ({ children }) => {
const code = children.trim();
const [copied, copy] = useClipboard();

return (
Expand Down Expand Up @@ -48,7 +52,7 @@ const Terminal = ({ children }) => {
<Button
variant={Button.VARIANT.LINK}
size={Button.SIZE.SMALL}
onClick={() => copy(filterOutput(children))}
onClick={() => copy(filterCopyOutput(children))}
className="dark-mode"
css={css`
justify-self: end;
Expand Down Expand Up @@ -91,14 +95,35 @@ const Terminal = ({ children }) => {
`}
>
<code>
<SyntaxHighlighter code={children.trim()} />
<Highlight Prism={Prism} code={code} language="shell">
{({ tokens, getTokenProps }) => {
const commands = rollupIntoCommands(tokens, code);

return commands.map(({ lines, output }, commandIdx) => (
<>
{lines.map((line, idx) => (
<Command
key={`${commandIdx}-${idx}`}
line={line}
prompt={idx > 0 ? '>' : '$'}
getTokenProps={getTokenProps}
/>
))}

{output.map((line, idx) => (
<Output key={`${commandIdx}-${idx}`} line={line} />
))}
</>
));
}}
</Highlight>
</code>
</pre>
</div>
);
};

const filterOutput = (commands) => {
const filterCopyOutput = (commands) => {
return commands
.split('\n')
.filter((line) => !line.startsWith('[output]'))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,47 +1,7 @@
import React from 'react';
import PropTypes from 'prop-types';
import Highlight from 'prism-react-renderer';
import Prism from 'prismjs';
import Command from './Command';
import Output from './Output';

const MULTILINE_COMMAND = /\\\s*$/;
const OUTPUT_TAG = /^\[output\](\s|$)/;
const OUTPUT_COLOR_TOKENS = /{([a-z]+)}(.*?(?={|$))/g;

const SyntaxHighlighter = ({ code }) => (
<Highlight Prism={Prism} code={code} language="shell">
{({ tokens, getTokenProps }) => {
const commands = rollupIntoCommands(tokens, code);

return commands.map(({ lines, output }, commandIdx) => (
<>
{lines.map((line, idx) => {
return (
<Command
key={`${commandIdx}-${idx}`}
line={line}
prompt={idx > 0 ? '>' : '$'}
getTokenProps={getTokenProps}
/>
);
})}
{output.map((line, idx) => (
<Output key={`${commandIdx}-${idx}`} line={line} />
))}
</>
));
}}
</Highlight>
);

const collapse = (line) => {
return line
.filter((token) => !token.types.includes('comment'))
.map((token) => token.content)
.join('');
};

const rollupIntoCommands = (lines, code) => {
const rawLines = code.split('\n');

Expand Down Expand Up @@ -71,6 +31,13 @@ const rollupIntoCommands = (lines, code) => {
return commands;
};

const collapse = (line) => {
return line
.filter((token) => !token.types.includes('comment'))
.map((token) => token.content)
.join('');
};

const tokenizeOutputLine = (line) => {
const text = line.replace(OUTPUT_TAG, '');
const tokens = Array.from(text.matchAll(OUTPUT_COLOR_TOKENS));
Expand All @@ -89,9 +56,4 @@ const tokenizeOutputLine = (line) => {
);
};

SyntaxHighlighter.propTypes = {
className: PropTypes.string,
code: PropTypes.string.isRequired,
};

export default SyntaxHighlighter;
export default rollupIntoCommands;

0 comments on commit 9482d11

Please sign in to comment.