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

62.79% Statements 27/43
50% Branches 13/26
80% Functions 4/5
62.79% Lines 27/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
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120

    4x

    8x

    10x

    10x
    10x
    16x
    8x

    8x
    8x
    8x

    10x

    4x
    2x
    2x
    2x
    2x

    2x
    8x
    8x
    8x

    8x

    8x

    2x
    2x
    2x
    2x

    2x

     // based on https://github.com/FormidableLabs/prism-react-renderer
    import { AST, RefractorNode } from 'refractor/core';

    const newLineRegex = /\n/g;
    function getNewLines(str: string) {
      return str.match(newLineRegex);
    }

    function createLineElement({
      children,
      lineNumber,
      className,
    }: {
      children: RefractorNode[];
      lineNumber?: number;
      className?: string[];
    }): AST.Element {
      return {
        type: 'element',
        tagName: 'span',
        properties: {
          className: lineNumber === void 0 ? className : ['line-number'],
        },
        children,
      };
    }

    function flattenCodeTree(tree: RefractorNode[], className: string[] = []): AST.Element[] {
      const newTree: AST.Element[] = [];
      for (const node of tree) {
        if (node.type === 'text') {
          newTree.push(
            createLineElement({
              children: [node],
              className,
            }),
          );
        } else Eif (node.children && node.properties.className !== void 0) {
          const classNames = className.concat(node.properties.className);
          newTree.push(...flattenCodeTree(node.children, classNames));
        }
      }

      return newTree;
    }

    export function lineNumberify(codeTree: RefractorNode[]): RefractorNode[] {
      const tree = flattenCodeTree(codeTree);
      const newTree = [];
      let lastLineBreakIndex = -1;
      let index = 0;

      while (index < tree.length) {
        const node = tree[index];
        const value = (node.children![0] as AST.Text).value!;
        const newLines = getNewLines(value);

        Iif (newLines) {
          const splitValue = value.split('\n');
          splitValue.forEach((text, i) => {
            const lineNumber = newTree.length + 1;
            const newChild: AST.Text = { type: 'text', value: `${text}\n` };

            if (i === 0) {
              const children = tree.slice(lastLineBreakIndex + 1, index).concat(
                createLineElement({
                  children: [newChild],
                  className: node.properties!.className,
                }),
              );
              newTree.push(createLineElement({ children, lineNumber }));
            } else if (i === splitValue.length - 1) {
              const stringChild = tree[index + 1] && tree[index + 1].children && tree[index + 1].children![0];
              if (stringChild) {
                const lastLineInPreviousSpan: AST.Text = { type: 'text', value: `${text}` };
                const newElem = createLineElement({
                  children: [lastLineInPreviousSpan],
                  className: node.properties!.className,
                });
                tree.splice(index + 1, 0, newElem);
              } else {
                newTree.push(
                  createLineElement({
                    children: [newChild],
                    lineNumber,
                    className: node.properties!.className,
                  }),
                );
              }
            } else {
              newTree.push(
                createLineElement({
                  children: [newChild],
                  lineNumber,
                  className: node.properties!.className,
                }),
              );
            }
          });

          lastLineBreakIndex = index;
        }
        index++;
      }

      Eif (lastLineBreakIndex !== tree.length - 1) {
        const children = tree.slice(lastLineBreakIndex + 1, tree.length);
        Eif (children && children.length) {
          newTree.push(
            createLineElement({
              children,
              lineNumber: newTree.length + 1,
            }),
          );
        }
      }

      return newTree;
    }