# All files / src/CodeEditor/utils highlightCode.ts

77.78% Statements 35/45
36.36% Branches 4/11
50% Functions 1/2
79.07% Lines 34/43

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    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
    582x

    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x

    2x
    3x
    3x

    3x
    3x

    3x

    3x

     import * as Prism from 'prismjs';

    import 'prismjs/components/prism-bash';
    import 'prismjs/components/prism-c';
    import 'prismjs/components/prism-csharp';
    import 'prismjs/components/prism-diff';
    import 'prismjs/components/prism-git';
    import 'prismjs/components/prism-go';
    import 'prismjs/components/prism-graphql';
    import 'prismjs/components/prism-http';
    import 'prismjs/components/prism-java';
    import 'prismjs/components/prism-javascript';
    import 'prismjs/components/prism-json';
    import 'prismjs/components/prism-jsx';
    import 'prismjs/components/prism-markdown';
    import 'prismjs/components/prism-markup';
    import 'prismjs/components/prism-markup-templating';
    import 'prismjs/components/prism-objectivec';
    import 'prismjs/components/prism-ocaml';
    import 'prismjs/components/prism-php';
    import 'prismjs/components/prism-powershell';
    import 'prismjs/components/prism-protobuf';
    import 'prismjs/components/prism-python';
    import 'prismjs/components/prism-ruby';
    import 'prismjs/components/prism-sql';
    import 'prismjs/components/prism-swift';
    import 'prismjs/components/prism-typescript';
    import 'prismjs/components/prism-yaml';

    export const highlightCode = (code: string = '', language: string, showLineNumbers?: boolean) => {
      const langDef = Prism.languages[language];
      Iif (!code || !langDef) {
        if (showLineNumbers) {
          return `<span class="line-number"></span>${code}`;
        }

        return code;
      }

      try {
        const result = Prism.highlight(code, langDef, language);

        Iif (showLineNumbers) {
          const splitOnNewLines = result.split('\n');
          if (splitOnNewLines.length) {
            return splitOnNewLines.map(line => `<span class="line-number"></span>${line}`).join('\n');
          }

          return `<span class="line-number"></span>${splitOnNewLines[0]}`;
        }

        return result;
      } catch (error) {
        console.log('Error highlighting code:', error, code);
        return code;
      }
    };