From 1b446ba8bffe0a6382d9112dd56795cb8e81e537 Mon Sep 17 00:00:00 2001 From: noraj Date: Wed, 11 Dec 2019 23:05:05 +0100 Subject: [PATCH 1/2] add min_depth to toc() helper --- lib/plugins/helper/toc.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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 ''; From dad6e85e206b5eac63e0e8caa533552d9b85c269 Mon Sep 17 00:00:00 2001 From: noraj Date: Wed, 11 Dec 2019 23:22:13 +0100 Subject: [PATCH 2/2] [WIP] add test for min_depth of toc() helper --- test/scripts/helpers/toc.js | 11 +++++++++++ 1 file changed, 11 insertions(+) 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