import cheerio from 'cheerio'; import { MbNode, parseHTML } from '../utils/node.js'; import { MARKBIND_FOOTNOTE_POPOVER_ID_PREFIX } from './constants.js'; /* * Footnotes of the main content and s are stored, then combined by NodeProcessor at the end */ export class FootnoteProcessor { renderedFootnotes: string[]; constructor() { // Store footnotes of s and the main content this.renderedFootnotes = []; } processMbTempFootnotes(node: MbNode) { const $ = cheerio(node); const content = $.html(); if (content) { this.renderedFootnotes.push(content); } $.remove(); } combineFootnotes(processNode: (nd: MbNode) => any): string { let hasFootnote = false; const prefix = '
\n
\n
    \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(`
    ${$(li).html()}
    `)[0] as MbNode; processNode(popoverNode); popoversHtml += cheerio.html(popoverNode); }); return `${popoversHtml}\n${footNoteBlock}\n`; }).join('\n'); const suffix = '
\n
\n'; return hasFootnote ? prefix + footnotesWithPopovers + suffix : ''; } }