-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
74 lines (60 loc) · 1.9 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
(function(module) {
"use strict";
var MarkdownToc = {},
cheerio = require('cheerio');
MarkdownToc.parse = function(data, callback) {
var postContent = data.postData.content;
var titleRegexp = /<h(.)>(.*?)<\/h(.)>/gm;
var tocRegexp = /\[toc\]/mg;
var titles = postContent.match(titleRegexp);
var toc = postContent.match(tocRegexp);
if(titles && titles.length && toc && toc.length) {
var tocContent = '<div class="toc">';
var ids = [];
var uls = {};
var parentNode = '';
var parentObject = {};
var $ = cheerio.load('<div class="toc"></div>');
titles.forEach(function (title) {
var str = title.replace(titleRegexp, '{"num":"$1","id":"$2"}');
var object = JSON.parse(str);
var id = object.id;
var current = '';
if(ids.indexOf(object.id) != -1) {
id = object.id+'-'+ids.length;
}
ids.push(id);
postContent = postContent.replace(title, '<h'+object.num+' id="'+id+'">'+object.id+'</h'+object.num+'>');
var li = '<li><a href="#' + id + '">' + object.id + '</a></li>';
var i = 1;
if(!parentNode) {
$('div').append('<ul></ul>');
parentNode = $('div').children().first();
while(i<object.num) {
parentNode.append('<ul></ul>');
parentNode = parentNode.children().last();
i++;
}
parentNode.append(li);
parentNode = parentNode.children().last();
}else if(parentObject.num == object.num) {
parentNode.append(li);
parentNode = parentNode.children().last();
}else{
parentNode = $('div').children().first();
while(i<object.num){
parentNode.append('<ul></ul>');
parentNode = parentNode.children().last();
i++;
}
parentNode.append(li);
}
parentObject = object;
});
postContent = postContent.replace(tocRegexp, $.html());
data.postData.content = postContent;
}
callback(null, data);
};
module.exports = MarkdownToc;
}(module));