-
Notifications
You must be signed in to change notification settings - Fork 6
/
toc.js
72 lines (59 loc) · 1.92 KB
/
toc.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
'use strict';
var hbs = require('express-hbs'),
_ = require('lodash'),
registerHelper,
cheerio = require('cheerio');
var registerHelper = function () {
//
// ### Table of Contents helper
//
// @param {Object} options object,
// start is the starting level and
// end the max depth of the headline level
//
// *Usage example:*
// `{{toc}}`
// `{{toc start="1" end="3"}}`
//
// Defaults to start="1"
// Defaults to end="4"
//
// **returns** SafeString content html.
//
hbs.registerHelper('toc', function(options) {
var options = options || {};
options.hash = options.hash || {};
var toc = [];
var $ = cheerio.load(this.html);
var startLevel = options.hash.start || 1;
var maxDepth = options.hash.end || 4;
var getHeadlines = function(start, end, current, elem) {
if(parseInt(current) > parseInt(end)) { return; }
if(_.isUndefined(current)) {
$('h' + start).each(function(i, elem) {
toc.push('<li><a href="#' + $(elem).attr('id') + '">' + $(elem).text() + '</a></li>');
getHeadlines(start, end, parseInt(start) + 1, elem);
});
} else {
var $subHeaders = $(elem).nextUntil('h' + (parseInt(current) - 1), 'h' + current);
if($subHeaders.length !== 0) {
toc.push('<ul>');
$subHeaders.each(function(i, elem) {
toc.push('<li><a href="#' + $(elem).attr('id') + '">' + $(elem).text() + '</a></li>');
getHeadlines(start, end, parseInt(current) + 1, elem);
});
toc.push('</ul>');
}
}
};
// Ignore the cases where no header with startLevel exists
while(startLevel <= maxDepth && $('h' + startLevel).length === 0){
startLevel++;
}
if (startLevel <= maxDepth) {
getHeadlines(startLevel, maxDepth);
}
return new hbs.handlebars.SafeString(toc.join(' '));
});
};
module.exports = registerHelper;