-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathtocrenderer.js
39 lines (31 loc) · 1013 Bytes
/
tocrenderer.js
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
import logger from "./logger.js";
import Utils from "./utils.js";
class TocRenderer {
constructor(p_node) {
this.containerNode = p_node;
this.toc = [];
}
// Render the toc of @p_contentNode into this.containerNode.
render(p_contentNode) {
this.containerNode.empty();
this.toc = [];
let utils = new Utils();
// Fetch the outline.
let headers = p_contentNode.find('h1, h2, h3, h4, h5, h6');
for (let i = 0; i < headers.length; ++i) {
let header = headers[i];
this.toc.push({
level: parseInt(header.tagName.substr(1)),
anchor: header.id,
title: utils.escapeHtml(header.textContent)
});
}
if (this.toc.length === 0) {
return;
}
let tocTree = utils.tocToTree(this.toc);
this.containerNode.html(tocTree);
utils.rewriteAnchorInToc(this.containerNode);
}
}
export default TocRenderer;