-
Notifications
You must be signed in to change notification settings - Fork 17
/
filter.js
60 lines (47 loc) · 1.79 KB
/
filter.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
var toc = require('markdown-toc');
var assign = require('object-assign');
var cheerio = require('cheerio');
exports.insert = function (data) {
var options = assign({}, this.config.toc);
// add class option
if (options.class) {
data.content = data.content.replace("<!-- toc -->", '<div class="' + options.class + 'Start"></div><!-- toc --><div class="' + options.class + 'End"></div>');
}
data.content = toc.insert(data.content, options);
return data;
};
exports.heading = function (data) {
var options = assign({}, this.config.toc);
var $ = cheerio.load(data.content, { decodeEntities: ( options.decodeEntities !== undefined ? options.decodeEntities : false ) });
var headings = $('h1, h2, h3, h4, h5, h6');
headings.each(function () {
var $title = $(this);
var title = $title.text();
var id = toc.slugify(title, options);
// $title.attr('id', id);
$title.children('a').remove();
$title.html( '<span id="' + id + '">' + $title.html() + '</span>' );
$title.removeAttr('id');
if (options.anchor) {
var anchorOpts = assign(
{
position: 'after',
symbol: '#',
style: 'header-anchor'
}, options.anchor);
// Put the anchor after the title by default, unless says otherwise
var link = '<a href="#' + id + '" class="' + anchorOpts.style + '">' + anchorOpts.symbol + '</a>';
if (anchorOpts.position === 'before') {
$title.prepend(link);
} else {
$title.append(link);
}
}
});
data.content = $.html();
// add class option
if (options.class) {
data.content = data.content.replace('<div class="' + options.class + 'Start"></div>', '<div class="' + options.class + '">').replace('<div class="' + options.class + 'End"></div>', '</div>');
}
return data;
};