-
Notifications
You must be signed in to change notification settings - Fork 527
/
Copy pathextract-sidebar.ts
44 lines (39 loc) · 1.17 KB
/
extract-sidebar.ts
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
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(`
* <div id="Quick_links">Stuff</div>
* <h2>Headline<h2>
* <p>Text</p>
* `)
* const sidebar = extractSidebar($);
* console.log(sidebar);
* // '<div id="Quick_links">Stuff</div>'
* console.log($.html());
* // '<h2>Headline<h2>\n<p>Text</p>'
*
* ...give or take some whitespace.
*/
export function extractSidebar($: cheerio.CheerioAPI, doc: Partial<Doc>) {
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("<em></em>");
}
});
doc.sidebarHTML = search.html();
doc.sidebarMacro = search.attr("data-macro");
search.remove();
}