-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
toc.ts
130 lines (103 loc) · 3.38 KB
/
toc.ts
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
119
120
121
122
123
124
125
126
127
128
129
130
import { tocObj, escapeHTML, encodeURL } from 'hexo-util';
interface Options {
min_depth?: number;
max_depth?: number;
max_items?: number;
class?: string;
class_item?: string;
class_link?: string;
class_text?: string;
class_child?: string;
class_number?: string;
class_level?: string;
list_number?: boolean;
}
function tocHelper(str: string, options: Options = {}) {
options = Object.assign({
min_depth: 1,
max_depth: 6,
max_items: Infinity,
class: 'toc',
class_item: '',
class_link: '',
class_text: '',
class_child: '',
class_number: '',
class_level: '',
list_number: true
}, options);
const data = getAndTruncateTocObj(str, { min_depth: options.min_depth, max_depth: options.max_depth }, options.max_items);
if (!data.length) return '';
const className = escapeHTML(options.class);
const itemClassName = escapeHTML(options.class_item || options.class + '-item');
const linkClassName = escapeHTML(options.class_link || options.class + '-link');
const textClassName = escapeHTML(options.class_text || options.class + '-text');
const childClassName = escapeHTML(options.class_child || options.class + '-child');
const numberClassName = escapeHTML(options.class_number || options.class + '-number');
const levelClassName = escapeHTML(options.class_level || options.class + '-level');
const listNumber = options.list_number;
let result = `<ol class="${className}">`;
const lastNumber = [0, 0, 0, 0, 0, 0];
let firstLevel = 0;
let lastLevel = 0;
for (let i = 0, len = data.length; i < len; i++) {
const el = data[i];
const { level, id, text } = el;
const href = id ? `#${encodeURL(id)}` : null;
if (!el.unnumbered) {
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="${childClassName}">`;
} else {
result += '</li>';
}
} else {
firstLevel = level;
}
result += `<li class="${itemClassName} ${levelClassName}-${level}">`;
if (href) {
result += `<a class="${linkClassName}" href="${href}">`;
} else {
result += `<a class="${linkClassName}">`;
}
if (listNumber && !el.unnumbered) {
result += `<span class="${numberClassName}">`;
for (let i = firstLevel - 1; i < level; i++) {
result += `${lastNumber[i]}.`;
}
result += '</span> ';
}
result += `<span class="${textClassName}">${text}</span></a>`;
lastLevel = level;
}
for (let i = firstLevel - 1; i < lastLevel; i++) {
result += '</li></ol>';
}
return result;
}
function getAndTruncateTocObj(str: string, options: {min_depth: number, max_depth: number}, max_items: number) {
let data = tocObj(str, { min_depth: options.min_depth, max_depth: options.max_depth });
if (data.length === 0) {
return data;
}
if (max_items < 1 || max_items === Infinity) {
return data;
}
const levels = data.map(item => item.level);
const min = Math.min(...levels);
const max = Math.max(...levels);
for (let currentLevel = max; data.length > max_items && currentLevel > min; currentLevel--) {
data = data.filter(item => item.level < currentLevel);
}
data = data.slice(0, max_items);
return data;
}
export = tocHelper;