/
toc.js
79 lines (59 loc) · 1.99 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
73
74
75
76
77
78
79
'use strict';
let cheerio;
const { escapeHTML } = require('hexo-util');
function tocHelper(str, options = {}) {
if (!cheerio) cheerio = require('cheerio');
const $ = cheerio.load(str);
const headingsMaxDepth = Object.prototype.hasOwnProperty.call(options, 'max_depth') ? options.max_depth : 6;
const headingsSelector = ['h1', 'h2', 'h3', 'h4', 'h5', 'h6'].slice(0, headingsMaxDepth).join(',');
const headings = $(headingsSelector);
if (!headings.length) return '';
const className = options.class || 'toc';
const listNumber = Object.prototype.hasOwnProperty.call(options, 'list_number') ? options.list_number : true;
let result = `<ol class="${className}">`;
const lastNumber = [0, 0, 0, 0, 0, 0];
let firstLevel = 0;
let lastLevel = 0;
function getId(ele) {
const id = $(ele).attr('id');
const $parent = $(ele).parent();
return id || ($parent.length < 1 ? null : getId($parent));
}
headings.each(function() {
const level = +this.name[1];
const id = getId(this);
const text = escapeHTML($(this).text());
lastNumber[level - 1]++;
for (let i = level; i <= 5; i++) {
lastNumber[i] = 0;
}
if (firstLevel) {
for (let i = level; i < lastLevel; i++) {
result += '</li></ol>';
}
if (level > lastLevel) {
result += `<ol class="${className}-child">`;
} else {
result += '</li>';
}
} else {
firstLevel = level;
}
result += `<li class="${className}-item ${className}-level-${level}">`;
result += `<a class="${className}-link" href="#${id}">`;
if (listNumber) {
result += `<span class="${className}-number">`;
for (let i = firstLevel - 1; i < level; i++) {
result += `${lastNumber[i]}.`;
}
result += '</span> ';
}
result += `<span class="${className}-text">${text}</span></a>`;
lastLevel = level;
});
for (let i = firstLevel - 1; i < lastLevel; i++) {
result += '</li></ol>';
}
return result;
}
module.exports = tocHelper;