htmlCode.tsx 780 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import 'prism-sentry/index.css';
  2. import {useEffect, useRef} from 'react';
  3. import styled from '@emotion/styled';
  4. import beautify from 'js-beautify';
  5. import Prism from 'prismjs';
  6. type Props = {
  7. code: string;
  8. };
  9. function HTMLCode({code}: Props) {
  10. const codeRef = useRef<HTMLModElement | null>(null);
  11. const formattedCode = beautify.html(code, {indent_size: 2});
  12. useEffect(() => {
  13. Prism.highlightElement(codeRef.current, false);
  14. }, []);
  15. return (
  16. <CodeWrapper>
  17. <pre>
  18. <code ref={codeRef} className="language-html">
  19. {formattedCode}
  20. </code>
  21. </pre>
  22. </CodeWrapper>
  23. );
  24. }
  25. const CodeWrapper = styled('div')`
  26. line-height: 1.5;
  27. pre {
  28. word-break: break-all;
  29. white-space: pre-wrap;
  30. }
  31. `;
  32. export default HTMLCode;