# All files / src/utils node.ts

100% Statements 4/4
100% Branches 0/0
100% Functions 1/1
100% Lines 4/4

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

    18x
    136x
    136x

     import cheerio from 'cheerio';
    import { DomElement } from 'htmlparser2';

    /*
     * A TextElement is a simple node that does not need complex processing.
     */
    export type TextElement = DomElement;

    /*
     * MbNode (MarkBindNode) is an element that can be operated on by cheerio and our own node processing
     * methods. It must have a name (used to identify what kind of node it is), attributes (possibly empty),
     * and children nodes (possibly empty). This type allows us to assert that these attributes exist.
     */
    export type MbNode = DomElement & cheerio.Element & {
      name: string,
      attribs: Record<string, any>,
      children: NodeOrText[],
    };

    /*
     * NodeOrText is used before a node can be casted to either TextElement or MbNode.
     */
    export type NodeOrText = TextElement | MbNode;

    /**
     * Utility function for converting HTML string to a node or text element.
     */
    export function parseHTML(html: string) {
      const node = cheerio.parseHTML(html);
      return node as unknown as NodeOrText[];
    }