htmlCode.tsx 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. import space from 'sentry/styles/space';
  7. type Props = {
  8. code: string;
  9. };
  10. function HTMLCode({code}: Props) {
  11. const ref = useRef<HTMLModElement | null>(null);
  12. const formattedCode = beautify.html(code, {indent_size: 2});
  13. useEffect(
  14. () => void (ref.current && Prism.highlightElement(ref.current, false)),
  15. [code]
  16. );
  17. return (
  18. <StyledPre>
  19. <code ref={ref} className="language-html">
  20. {formattedCode}
  21. </code>
  22. </StyledPre>
  23. );
  24. }
  25. const StyledPre = styled('pre')`
  26. overflow: auto !important;
  27. padding: ${space(1)} ${space(1.5)} !important;
  28. word-break: break-all;
  29. white-space: pre-wrap;
  30. margin-bottom: 0;
  31. /* Need font-size twice here, so ReactVirtualizedList can measure height correctly */
  32. font-size: ${p => p.theme.fontSizeSmall} !important;
  33. code {
  34. font-size: ${p => p.theme.fontSizeSmall} !important;
  35. }
  36. `;
  37. export default HTMLCode;