# All files / src/CodeViewer/utils parseCode.ts

95.12% Statements 39/41
83.33% Branches 5/6
100% Functions 4/4
95% Lines 38/40

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
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    693x
    3x

    3x
    3x
    3x
    3x
    3x
    3x
    3x
    3x
    3x
    3x
    3x
    3x
    3x
    3x
    3x
    3x
    3x
    3x
    3x
    3x
    3x
    3x
    3x
    3x
    3x
    3x

    4x

    4x
    2x
    2x

    3x

    3x
    4x
    4x
    4x

    4x

     import * as refractor from 'refractor/core';
    import { lineNumberify } from './lineNumberify';

    refractor.register(require('refractor/lang/bash'));
    refractor.register(require('refractor/lang/c'));
    refractor.register(require('refractor/lang/csharp'));
    refractor.register(require('refractor/lang/diff'));
    refractor.register(require('refractor/lang/git'));
    refractor.register(require('refractor/lang/go'));
    refractor.register(require('refractor/lang/graphql'));
    refractor.register(require('refractor/lang/http'));
    refractor.register(require('refractor/lang/java'));
    refractor.register(require('refractor/lang/javascript'));
    refractor.register(require('refractor/lang/json'));
    refractor.register(require('refractor/lang/jsx'));
    refractor.register(require('refractor/lang/markdown'));
    refractor.register(require('refractor/lang/markup'));
    refractor.register(require('refractor/lang/markup-templating'));
    refractor.register(require('refractor/lang/objectivec'));
    refractor.register(require('refractor/lang/ocaml'));
    refractor.register(require('refractor/lang/php'));
    refractor.register(require('refractor/lang/powershell'));
    refractor.register(require('refractor/lang/protobuf'));
    refractor.register(require('refractor/lang/python'));
    refractor.register(require('refractor/lang/ruby'));
    refractor.register(require('refractor/lang/sql'));
    refractor.register(require('refractor/lang/swift'));
    refractor.register(require('refractor/lang/typescript'));
    refractor.register(require('refractor/lang/yaml'));

    function parsePlainText(code: string): refractor.RefractorNode[] {
      return code.split('\n').map((value, i, arr) => ({
        type: 'element',
        tagName: 'span',
        properties: {},
        children: [
          {
            type: 'text',
            value: arr.length - 1 === i ? value : `${value}\n`,
          },
        ],
      }));
    }

    function safeParse(code: string, language?: string): refractor.RefractorNode[] {
      if (language) {
        try {
          return refractor.highlight(code, language);
        } catch (ex) {
          // let's fallback to plain text
        }
      }

      return parsePlainText(code);
    }

    export function parseCode(code: string, language?: string, addLineNumbers?: boolean) {
      try {
        const ast = safeParse(code, language);
        Iif (addLineNumbers) {
          return lineNumberify(ast);
        }

        return ast;
      } catch (ex) {
        return null;
      }
    }