import * as cheerio from "cheerio"; import { Doc } from "../libs/types/document.js"; /** Extract and mutate the $ if it as a "Quick_links" section. * But only if it exists. * * If you had this: * * const $ = cheerio.load(` * *

Headline

*

Text

* `) * const sidebar = extractSidebar($); * console.log(sidebar); * // '' * console.log($.html()); * // '

Headline

\n

Text

' * * ...give or take some whitespace. */ export function extractSidebar($: cheerio.CheerioAPI, doc: Partial) { const search = $("#Quick_links"); if (!search.length) { doc.sidebarHTML = ""; return; } // Open menu and highlight current page. search.find(`a[href='${doc.mdn_url}']`).each((_i, el) => { $(el).parents("details").prop("open", true); $(el).attr("aria-current", "page"); // Highlight, unless it already is highlighted (e.g. heading). if ($(el).find("em,strong").length === 0) { $(el).parent().wrapInner(""); } }); doc.sidebarHTML = search.html(); doc.sidebarMacro = search.attr("data-macro"); search.remove(); }