-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathtoc.jsx
187 lines (173 loc) · 5.19 KB
/
toc.jsx
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/**
* Table of contents widget JSX component.
* @module view/widget/toc
*/
const { tocObj: getTocObj, unescapeHTML } = require('hexo-util');
const { Component } = require('inferno');
const { cacheComponent } = require('../../util/cache');
/**
* Export a tree of headings of an article
* @private
* @example
* getToc('HTML content...');
* // {
* // "1": {
* // "id": "How-to-enable-table-of-content-for-a-post",
* // "text": "How to enable table of content for a post",
* // "index": "1"
* // },
* // "2": {
* // "1": {
* // "1": {
* // "id": "Third-level-title",
* // "text": "Third level title",
* // "index": "2.1.1"
* // },
* // "id": "Second-level-title",
* // "text": "Second level title",
* // "index": "2.1"
* // },
* // "2": {
* // "id": "Another-second-level-title",
* // "text": "Another second level title",
* // "index": "2.2"
* // },
* // "id": "First-level-title",
* // "text": "First level title",
* // "index": "2"
* // }
* // }
*/
function getToc(content, maxDepth) {
const toc = {};
const tocObj = getTocObj(content, { min_depth: 1, max_depth: 6 });
const minLevels = Array.from(new Set(tocObj.map((item) => item.level)))
.sort((a, b) => a - b)
.slice(0, maxDepth);
const levels = new Array(minLevels.length).fill(0);
tocObj.forEach((item) => {
if (!minLevels.includes(item.level)) {
return;
}
const { text, id } = item;
const level = item.level - minLevels[0];
for (let i = 0; i < levels.length; i++) {
if (i > level) {
levels[i] = 0;
} else if (i < level) {
if (levels[i] === 0) {
// if headings start with a lower level heading, set the former heading index to 1
// e.g. h3, h2, h1, h2, h3 => 1.1.1, 1.2, 2, 2.1, 2.1.1
levels[i] = 1;
}
} else {
levels[i] += 1;
}
}
let node = toc;
for (const i of levels.slice(0, level + 1)) {
if (!(i in node)) {
node[i] = {};
}
node = node[i];
}
node.id = id;
node.text = text;
node.index = levels.slice(0, level + 1).join('.');
});
return toc;
}
/**
* Table of contents widget JSX component.
*
* @example
* <Toc
* title="Widget title"
* content="HTML content"
* showIndex={true}
* collapsed={true}
* maxDepth={3}
* jsUrl="******" />
*/
class Toc extends Component {
renderToc(toc, showIndex = true) {
let result;
const keys = Object.keys(toc)
.filter((key) => !['id', 'index', 'text'].includes(key))
.map((key) => parseInt(key, 10))
.sort((a, b) => a - b);
if (keys.length > 0) {
result = <ul class="menu-list">{keys.map((i) => this.renderToc(toc[i], showIndex))}</ul>;
}
if ('id' in toc && 'index' in toc && 'text' in toc) {
result = (
<li>
<a class="level is-mobile" href={'#' + toc.id}>
<span class="level-left">
{showIndex ? <span class="level-item">{toc.index}</span> : null}
<span class="level-item">{unescapeHTML(toc.text)}</span>
</span>
</a>
{result}
</li>
);
}
return result;
}
render() {
const { showIndex, maxDepth = 3, collapsed = true } = this.props;
const toc = getToc(this.props.content, maxDepth);
if (!Object.keys(toc).length) {
return null;
}
const css =
'#toc .menu-list > li > a.is-active + .menu-list { display: block; }' +
'#toc .menu-list > li > a + .menu-list { display: none; }';
return (
<div class="card widget" id="toc" data-type="toc">
<div class="card-content">
<div class="menu">
<h3 class="menu-label">{this.props.title}</h3>
{this.renderToc(toc, showIndex)}
</div>
</div>
{collapsed ? <style dangerouslySetInnerHTML={{ __html: css }}></style> : null}
<script src={this.props.jsUrl} defer={true}></script>
</div>
);
}
}
/**
* Cacheable table of contents widget JSX component.
* <p>
* This class is supposed to be used in combination with the <code>locals</code> hexo filter
* ({@link module:hexo/filter/locals}).
*
* @see module:util/cache.cacheComponent
* @example
* <Toc.Cacheable
* config={{ toc: true }}
* page={{ layout: 'post', content: 'HTML content' }}
* widget={{ index: true, collapsed: true, depth: 3 }}
* helper={{
* _p: function() {...},
* url_for: function() {...}
* }} /> />
*/
Toc.Cacheable = cacheComponent(Toc, 'widget.toc', (props) => {
const { config, page, widget, helper } = props;
const { layout, content, encrypt, origin } = page;
const { index, collapsed = true, depth = 3 } = widget;
if (config.toc !== true || (layout !== 'page' && layout !== 'post')) {
return null;
}
return {
title: helper._p('widget.catalogue', Infinity),
collapsed: collapsed !== false,
maxDepth: depth | 0,
showIndex: index !== false,
content: encrypt ? origin : content,
jsUrl: helper.url_for('/js/toc.js'),
};
});
module.exports = Toc;