-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDOCreator.js
76 lines (71 loc) · 2.55 KB
/
DOCreator.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
class DOCreatorNode {
constructor (element, children = [], isParent = false) {
if (isParent)
this.level = 0;
else
this.level = ({
H2:1,
H3:2,
H4:3,
H5:4,
H6:5,
})[element.tagName];
if (typeof this.level == 'undefined') {
throw "Invalid heading element. Only h2, h3, h4, h5, and h6 elements are accepted";
}
this.element = element;
this.children = children;
}
push (DOCreatorNodeObj) {
if (typeof DOCreatorNodeObj.level == 'undefined' || DOCreatorNodeObj.level <= this.level) {
throw "Invalid Heading Level";
}
if (this.children.length == 0 || DOCreatorNodeObj.level <= this.children[this.children.length - 1].level)
return this.children.push(DOCreatorNodeObj);
return this.children[this.children.length - 1].push(DOCreatorNodeObj);
}
}
class DOCreator {
static getRaw (nodes = null) {
if (!nodes) {
nodes = document.querySelectorAll('h2, h3, h4, h5, h6');
}
if (!nodes[0]) {
return false;
}
let mainNode = new DOCreatorNode(document.createElement('li'), [], true);
for (let index = 0; index < nodes.length; index++) {
mainNode.push(new DOCreatorNode(nodes[index]));
}
return mainNode;
}
static get(raw, generateHash = "doc_h", listType = 'ul', isParent = true) {
var item = document.createElement('li');
if (!isParent && generateHash) {
let anchor = document.createElement('a');
anchor.innerHTML = raw.element.innerHTML;
item.appendChild(anchor);
let hashID = raw.element.id;
if (!Boolean(hashID)) {
if (typeof DOCreator.hashNumber == 'undefined')
DOCreator.hashNumber = 0;
hashID = generateHash + DOCreator.hashNumber;
raw.element.id = hashID;
DOCreator.hashNumber++;
}
anchor.href = "#" + hashID;
} else
item.innerHTML = raw.element.innerHTML;
if (raw.children[0]) {
let list = document.createElement(listType);
raw.children.forEach(childRaw => {
list.appendChild(DOCreator.get(childRaw, generateHash, listType, false));
});
if (isParent) {
return list;
}
item.appendChild(list);
}
return item;
}
}