-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
118 lines (96 loc) · 3.04 KB
/
index.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
(function (global) {
/**
* Transform current path into real object path
*/
function getObjectPath (path) {
return (path.length === 0)
? path
: ['children'].concat(path.join('.children.').split('.'));
}
/**
* Get level from heading node
*/
function getLevel (node) {
return parseInt(node.tagName.slice(1), 10);
}
/**
* Poor man’s _.get(..)
*/
function get (object, path) {
return path.reduce(function (prev, curr) {
return prev[curr];
}, object);
}
function auditOutline (outline) {
var warnings = [];
outline.children.forEach(function (heading) {
heading.level > outline.level + 1 && warnings.push({
message: '〰 Heading “' + heading.text + '” is level ' + heading.level + ' but previous heading was level ' + outline.level + ' (“' + outline.text + '”).',
node: heading.node
});
warnings = warnings.concat(auditOutline(heading));
});
return warnings;
}
var Outline = function (node) {
this.node = node || document;
this.outline = this.parse();
this.warnings = this.audit();
};
Outline.prototype.parse = function () {
var outline = { children: [] };
var headings = this.node.querySelectorAll('h1, h2, h3, h4, h5, h6');
var lastLevel = 0;
var currentPath = [];
Array.prototype.forEach.call(headings, function (heading) {
var level = getLevel(heading);
var data = {
text: heading.textContent || heading.innerText,
level: level,
node: heading,
children: []
};
if (level < lastLevel) {
for (var i = lastLevel; i >= level; i--) currentPath.pop();
} else if (level === lastLevel) {
currentPath.pop();
}
var prop = get(outline, getObjectPath(currentPath));
prop.children.push(data);
currentPath.push(prop.children.length - 1);
lastLevel = level;
});
return outline;
};
Outline.prototype.warn = function () {
var end = '';
if (this.warnings.length === 0) {
end = '💯 ' + 'Outline audit over. 0 warnings found. Congratulations!';
} else if (this.warnings.length === 1) {
end = '❗️ ' + 'Outline audit over. 1 warning found. Not bad!';
} else {
end = '💢 ' + 'Outline audit over. ' + this.warnings.length + ' warnings found. Uh-oh!';
}
console.log('Auditing heading outline in:');
console.log(this.node);
console.log('');
this.warnings.forEach(function (warning) {
console.log(warning.message);
console.log(warning.node);
});
console.log('');
console.log(end);
};
Outline.prototype.audit = function () {
var warnings = [];
this.outline.children.forEach(function (heading) {
heading.level !== 1 && warnings.push({
message: '〰 Heading “' + heading.text + '” is level ' + heading.level + ' but it comes at root of document.',
node: heading.node
});
warnings = warnings.concat(auditOutline(heading));
});
return warnings;
};
global.Outline = Outline;
}(window));