aboutsummaryrefslogtreecommitdiff
path: root/frontend/src/components/renderers/Code.js
blob: 4ab1175bed4f4ea81ba526c00368c0a711313c38 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import React from 'react';
import SyntaxHighlighter from 'react-syntax-highlighter';
import virtualizedRenderer from 'react-syntax-highlighter-virtualized-renderer';
import { atomOneLight, ascetic, atomOneDark, dracula, ocean } from 'react-syntax-highlighter/dist/esm/styles/hljs';
import styled from 'styled-components'
import {Border, CodeLike, DropShadow, Rounded} from "../Common/mixins";

export const THEMES = Object.freeze({
    'atom': atomOneLight,
    'atom dark': atomOneDark,
    'plain': ascetic,
    'dracula': dracula,
    'ocean': ocean,
})

export const LANGS = Object.freeze({
    'latex': 'latex',
    'markdown': 'markdown',
    'detect': 'text',
})

export const StyledPre = styled.pre`
  ${Rounded};
  ${Border};
  ${DropShadow};
  width: calc(100%);
  padding: calc(0.6em - 1px) !important;
  margin: 1.7em 0;
  position: relative;
  outline: none;
  
  & code {
    ${CodeLike}
  }

  & code:first-child:not(:only-of-type) {
    margin-right: 10px !important;
    border-radius: 0 !important;
    border-right: 1px solid #11111155 !important;
  }
`

export const Highlighter = ({language, lineNumbers, theme, pre = StyledPre, children}) => <SyntaxHighlighter
  language={LANGS[language]}
  style={THEMES[theme]}
  showLineNumbers={lineNumbers}
  PreTag={pre}>
    {children}
</SyntaxHighlighter>

const CodeRenderer = (props) => {
    return (<Highlighter
      lineNumbers={true}
      language={props.lang}
      theme={props.theme}
      renderer={virtualizedRenderer()}
      pre={StyledPre}
    >
      {props.content}
    </Highlighter>)
};

export default CodeRenderer