Files
freeCodeCamp/client/src/components/SolutionViewer/SolutionViewer.js

71 lines
1.7 KiB
JavaScript
Raw Normal View History

2018-09-24 18:45:11 +01:00
import { Panel } from '@freecodecamp/react-bootstrap';
import Prism from 'prismjs';
import PropTypes from 'prop-types';
import React from 'react';
2018-09-24 18:45:11 +01:00
const prismLang = {
css: 'css',
js: 'javascript',
jsx: 'javascript',
html: 'markup'
};
const SolutionViewer = ({
challengeFiles,
2018-09-24 18:45:11 +01:00
solution = '// The solution is not available for this project'
}) =>
challengeFiles?.length ? (
challengeFiles.map(challengeFile => (
<Panel
bsStyle='primary'
className='solution-viewer'
key={challengeFile.ext}
>
<Panel.Heading>{challengeFile.ext.toUpperCase()}</Panel.Heading>
2018-09-24 18:45:11 +01:00
<Panel.Body>
<pre>
<code
className={`language-${prismLang[challengeFile.ext]}`}
2018-09-24 18:45:11 +01:00
dangerouslySetInnerHTML={{
__html: Prism.highlight(
challengeFile.contents.trim(),
Prism.languages[prismLang[challengeFile.ext]]
2018-09-24 18:45:11 +01:00
)
}}
/>
</pre>
</Panel.Body>
</Panel>
))
) : (
<Panel
bsStyle='primary'
className='solution-viewer'
key={solution.slice(0, 10)}
>
<Panel.Heading>JS</Panel.Heading>
<Panel.Body>
<pre>
<code
className='language-markup'
dangerouslySetInnerHTML={{
__html: Prism.highlight(
solution.trim(),
Prism.languages.js,
'javascript'
)
}}
/>
</pre>
</Panel.Body>
</Panel>
2018-09-24 18:45:11 +01:00
);
SolutionViewer.displayName = 'SolutionViewer';
SolutionViewer.propTypes = {
challengeFiles: PropTypes.array,
2018-09-24 18:45:11 +01:00
solution: PropTypes.string
};
export default SolutionViewer;