Skip to content

Commit 0cfb532

Browse files
committed
feat(toc_helper): add min_depth option
1 parent c361e84 commit 0cfb532

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

lib/plugins/helper/toc.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ const { tocObj } = require('hexo-util');
44

55
function tocHelper(str, options = {}) {
66
options = Object.assign({
7+
min_depth: 1,
78
max_depth: 6,
89
class: 'toc',
910
list_number: true
1011
}, options);
1112

12-
const data = tocObj(str, { max_depth: options.max_depth });
13+
const data = tocObj(str, { min_depth: options.min_depth, max_depth: options.max_depth });
1314

1415
if (!data.length) return '';
1516

test/scripts/helpers/toc.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,4 +311,54 @@ describe('toc', () => {
311311

312312
toc(html, { max_depth: 2 }).should.eql(expected);
313313
});
314+
315+
it('min_depth', () => {
316+
const className = 'toc';
317+
const expected = [
318+
'<ol class="' + className + '">',
319+
'<li class="' + className + '-item toc-level-2">',
320+
'<a class="' + className + '-link" href="#title_1_1">',
321+
'<span class="' + className + '-number">1.</span> ',
322+
'<span class="' + className + '-text">Title 1.1</span>',
323+
'</a>',
324+
'<ol class="' + className + '-child">',
325+
'<li class="' + className + '-item toc-level-3">',
326+
'<a class="' + className + '-link" href="#title_1_1_1">',
327+
'<span class="' + className + '-number">1.1.</span> ',
328+
'<span class="' + className + '-text">Title 1.1.1</span>',
329+
'</a>',
330+
'</li>',
331+
'</ol>',
332+
'</li>',
333+
'<li class="' + className + '-item toc-level-2">',
334+
'<a class="' + className + '-link" href="#title_1_2">',
335+
'<span class="' + className + '-number">2.</span> ',
336+
'<span class="' + className + '-text">Title 1.2</span>',
337+
'</a>',
338+
'</li>',
339+
'<li class="' + className + '-item toc-level-2">',
340+
'<a class="' + className + '-link" href="#title_1_3">',
341+
'<span class="' + className + '-number">3.</span> ',
342+
'<span class="' + className + '-text">Title 1.3</span>',
343+
'</a>',
344+
'<ol class="' + className + '-child">',
345+
'<li class="' + className + '-item toc-level-3">',
346+
'<a class="' + className + '-link" href="#title_1_3_1">',
347+
'<span class="' + className + '-number">3.1.</span> ',
348+
'<span class="' + className + '-text">Title 1.3.1</span>',
349+
'</a>',
350+
'</li>',
351+
'</ol>',
352+
'</li>',
353+
'<li class="' + className + '-item toc-level-2">',
354+
'<a class="' + className + '-link" href="#title_2_1">',
355+
'<span class="' + className + '-number">4.</span> ',
356+
'<span class="' + className + '-text">Title 2.1</span>',
357+
'</a>',
358+
'</li>',
359+
'</ol>'
360+
].join('');
361+
362+
toc(html, { min_depth: 2 }).should.eql(expected);
363+
});
314364
});

0 commit comments

Comments
 (0)