import * as cheerio from "cheerio"; // Over the years we have accumulated some weird
tags whose // brush is more or less "junk". // TODO: Perhaps, if you have a doc withtags that matches // this, it should become a flaw. const IGNORE = new Set(["none", "text", "plain", "unix"]); /** * Mutate the `$` instance by adding headers totags containing code blocks. * */ export function wrapCodeExamples($: cheerio.CheerioAPI) { // Our content will be like this: `` or // `` so we're technically not looking for an exact // match. The wildcard would technically match `` // too. But within the loop, we do a more careful regex on the class name // and only proceed if it's something sensible. $("pre[class*=brush]").each((_, element) => { // The language is whatever string comes after the `brush(:)` // portion of the class name. const $pre = $(element); const className = $pre.attr("class").toLowerCase(); const match = className.match(/brush:?\s*([\w_-]+)/); if (!match) { return; } const name = match[1].replace("-nolint", ""); if (IGNORE.has(name)) { // Seems to exist a couple of these in our docs. Just bail. return; } const code = $pre.text(); $pre.wrapAll(``); if (!$pre.hasClass("hidden")) { $( `${name}` ).insertBefore($pre); } const $code = $("").text(code); $pre.empty().append($code); }); }