# All files / src/html FootnoteProcessor.ts

41.66% Statements 10/24
33.33% Branches 1/3
40% Functions 2/5
41.66% Lines 10/24

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
    569x
    9x
    9x

    9x

    80x

    38x
    38x

    38x

    38x

    38x

     import cheerio from 'cheerio';
    import { MbNode, parseHTML } from '../utils/node';
    import { MARKBIND_FOOTNOTE_POPOVER_ID_PREFIX } from './constants';

    /*
     * Footnotes of the main content and <include>s are stored, then combined by NodeProcessor at the end
     */
    export class FootnoteProcessor {
      renderedFootnotes: string[];

      constructor() {
        // Store footnotes of <include>s and the main content
        this.renderedFootnotes = [];
      }

      processMbTempFootnotes(node: MbNode) {
        const $ = cheerio(node);
        const content = $.html();
        Iif (content) {
          this.renderedFootnotes.push(content);
        }
        $.remove();
      }

      combineFootnotes(processNode: (nd: MbNode) => any): string {
        let hasFootnote = false;
        const prefix = '<hr class="footnotes-sep">\n<section class="footnotes">\n<ol class="footnotes-list">\n';

        const footnotesWithPopovers = this.renderedFootnotes.map((footNoteBlock) => {
          const $ = cheerio.load(footNoteBlock);
          let popoversHtml = '';

          $('li.footnote-item').each((_index, li) => {
            hasFootnote = true;
            const popoverId = `${MARKBIND_FOOTNOTE_POPOVER_ID_PREFIX}${(li as MbNode).attribs.id}`;
            const popoverNode = parseHTML(`<popover id="${popoverId}">
                <div #content>
                  ${$(li).html()}
                </div>
              </popover>`)[0] as MbNode;
            processNode(popoverNode);

            popoversHtml += cheerio.html(popoverNode);
          });

          return `${popoversHtml}\n${footNoteBlock}\n`;
        }).join('\n');

        const suffix = '</ol>\n</section>\n';

        return hasFootnote
          ? prefix + footnotesWithPopovers + suffix
          : '';
      }
    }