Skip to content

Commit

Permalink
feat(toc_helper): add min_depth option
Browse files Browse the repository at this point in the history
  • Loading branch information
SukkaW committed Dec 20, 2019
1 parent c361e84 commit 0cfb532
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/plugins/helper/toc.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ const { tocObj } = require('hexo-util');

function tocHelper(str, options = {}) {
options = Object.assign({
min_depth: 1,
max_depth: 6,
class: 'toc',
list_number: true
}, options);

const data = tocObj(str, { max_depth: options.max_depth });
const data = tocObj(str, { min_depth: options.min_depth, max_depth: options.max_depth });

if (!data.length) return '';

Expand Down
50 changes: 50 additions & 0 deletions test/scripts/helpers/toc.js
Original file line number Diff line number Diff line change
Expand Up @@ -311,4 +311,54 @@ describe('toc', () => {

toc(html, { max_depth: 2 }).should.eql(expected);
});

it('min_depth', () => {
const className = 'toc';
const expected = [
'<ol class="' + className + '">',
'<li class="' + className + '-item toc-level-2">',
'<a class="' + className + '-link" href="#title_1_1">',
'<span class="' + className + '-number">1.</span> ',
'<span class="' + className + '-text">Title 1.1</span>',
'</a>',
'<ol class="' + className + '-child">',
'<li class="' + className + '-item toc-level-3">',
'<a class="' + className + '-link" href="#title_1_1_1">',
'<span class="' + className + '-number">1.1.</span> ',
'<span class="' + className + '-text">Title 1.1.1</span>',
'</a>',
'</li>',
'</ol>',
'</li>',
'<li class="' + className + '-item toc-level-2">',
'<a class="' + className + '-link" href="#title_1_2">',
'<span class="' + className + '-number">2.</span> ',
'<span class="' + className + '-text">Title 1.2</span>',
'</a>',
'</li>',
'<li class="' + className + '-item toc-level-2">',
'<a class="' + className + '-link" href="#title_1_3">',
'<span class="' + className + '-number">3.</span> ',
'<span class="' + className + '-text">Title 1.3</span>',
'</a>',
'<ol class="' + className + '-child">',
'<li class="' + className + '-item toc-level-3">',
'<a class="' + className + '-link" href="#title_1_3_1">',
'<span class="' + className + '-number">3.1.</span> ',
'<span class="' + className + '-text">Title 1.3.1</span>',
'</a>',
'</li>',
'</ol>',
'</li>',
'<li class="' + className + '-item toc-level-2">',
'<a class="' + className + '-link" href="#title_2_1">',
'<span class="' + className + '-number">4.</span> ',
'<span class="' + className + '-text">Title 2.1</span>',
'</a>',
'</li>',
'</ol>'
].join('');

toc(html, { min_depth: 2 }).should.eql(expected);
});
});

0 comments on commit 0cfb532

Please sign in to comment.