aboutsummaryrefslogtreecommitdiff
path: root/frontend/src/components/renderers/Code.js
blob: a312c517eec5bff3366dcf14352a8566be3e0775 (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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import React from 'react';
import SyntaxHighlighter from 'react-syntax-highlighter';
import { atomOneLight, ascetic, atomOneDark, dracula, ocean } from 'react-syntax-highlighter/dist/esm/styles/hljs';
import styled from 'styled-components'

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

export const LANGS = Object.freeze({
    'bash': 'bash',
    'c': 'c',
    'c++': 'cpp',
    'c#': 'cs',
    'css': 'css',
    'docker': 'dockerfile',
    'go': 'go',
    'haskell': 'haskell',
    'html': 'html',
    'java': 'java',
    'js': 'javascript',
    'jsx': 'jsx',
    'latex': 'latex',
    'lisp': 'lisp',
    'makefile': 'makefile',
    'markdown': 'markdown',
    'php': 'php',
    'python': 'python',
    'raw': 'text',
    'ruby': 'ruby',
    'scala': 'scala',
    'yaml': 'yaml'
})

const StyledPre = styled.pre`
  width: 100%;
  font-size: 0.8em;
  min-height: 1.2em;
  border-radius: 3px !important;
  border: 1px solid #565656 !important;
  padding: calc(0.8em - 1px) !important;
  outline: none;
  margin: 1.7em 0;

  & code:first-child {
    margin-right: 10px !important;
    border-radius: 0 !important;
    border-right: 1px solid #11111155 !important;
  }
`

const CodeRenderer = React.forwardRef((props, ref) => {

    const Pre = (props) => {
        return (
            <StyledPre {...props} ref={ref} />
        );
    }

    return (
        <div className="lt-shadow">
            <SyntaxHighlighter
                language={LANGS[props.lang]}
                style={THEMES[props.theme]}
                showLineNumbers
                PreTag={Pre}>
                {props.content}
            </SyntaxHighlighter>
        </div>
    );
});

export default CodeRenderer