diff --git a/lib/plugins/helper/toc.js b/lib/plugins/helper/toc.js index 51d0ef33e0..8dbc2a50f7 100644 --- a/lib/plugins/helper/toc.js +++ b/lib/plugins/helper/toc.js @@ -8,7 +8,8 @@ function tocHelper(str, options = {}) { 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 headingsMinDepth = Object.prototype.hasOwnProperty.call(options, 'min_depth') ? options.min_depth : 1; + const headingsSelector = ['h1', 'h2', 'h3', 'h4', 'h5', 'h6'].slice(headingsMinDepth - 1, headingsMaxDepth).join(','); const headings = $(headingsSelector); if (!headings.length) return ''; diff --git a/test/scripts/helpers/toc.js b/test/scripts/helpers/toc.js index 02c2cd17f6..593a99f4aa 100644 --- a/test/scripts/helpers/toc.js +++ b/test/scripts/helpers/toc.js @@ -24,11 +24,13 @@ describe('toc', () => { options = Object.assign({ class: 'toc', list_number: true, + min_depth: 1, max_depth: 6 }, options); const className = options.class; const listNumber = options.list_number; + const minDepth = options.min_depth; const maxDepth = options.max_depth; const resultTitle_1_1_1 = [ @@ -147,6 +149,15 @@ describe('toc', () => { genResult(options).should.eql(toc(html, options)); }); + + it('min_depth', () => { + const options = { + min_depth: 5 + }; + + genResult(options).should.eql(toc(html, options)); + }); + it('max_depth', () => { const options = { max_depth: 2