From c361e84efbd6f514972f2a8946d16f46e76d9b1e Mon Sep 17 00:00:00 2001 From: SukkaW Date: Wed, 18 Dec 2019 17:52:43 +0800 Subject: [PATCH 1/2] test(toc_helper): refactor --- test/scripts/helpers/toc.js | 299 +++++++++++++++++++++++++++--------- 1 file changed, 228 insertions(+), 71 deletions(-) diff --git a/test/scripts/helpers/toc.js b/test/scripts/helpers/toc.js index 02c2cd17f..8d5f9fcb5 100644 --- a/test/scripts/helpers/toc.js +++ b/test/scripts/helpers/toc.js @@ -1,9 +1,5 @@ 'use strict'; -function ifTrue(cond, yes, no) { - return cond ? yes : no; -} - describe('toc', () => { const toc = require('../../../lib/plugins/helper/toc'); @@ -20,138 +16,299 @@ describe('toc', () => { '

Chapter 1 should be printed to toc

' ].join(''); - const genResult = options => { - options = Object.assign({ - class: 'toc', - list_number: true, - max_depth: 6 - }, options); - - const className = options.class; - const listNumber = options.list_number; - const maxDepth = options.max_depth; - - const resultTitle_1_1_1 = [ + it('default', () => { + const className = 'toc'; + const expected = [ + '
    ', + '
  1. ', + '', + '1. ', // list_number enabled + 'Title 1', + '', + '
      ', + '
    1. ', + '', + '1.1. ', // list_number enabled + 'Title 1.1', + '', '
        ', '
      1. ', '', - ifTrue(listNumber, '1.1.1. ', ''), + '1.1.1. ', // list_number enabled 'Title 1.1.1', '', '
      2. ', - '
      ' - ].join(''); - - const resultTitle_1_3_1 = [ + '
    ', + '
  2. ', + '
  3. ', + '', + '1.2. ', // list_number enabled + 'Title 1.2', + '', + '
  4. ', + '
  5. ', + '', + '1.3. ', // list_number enabled + 'Title 1.3', + '', '
      ', '
    1. ', '', - ifTrue(listNumber, '1.3.1. ', ''), + '1.3.1. ', // list_number enabled 'Title 1.3.1', '', '
    2. ', + '
    ', + '
  6. ', + '
', + '', + '
  • ', + '', + '2. ', // list_number enabled + 'Title 2', + '', + '
      ', + '
    1. ', + '', + '2.1. ', // list_number enabled + 'Title 2.1', + '', + '
    2. ', + '
    ', + '
  • ', + '
  • ', + '', + '3. ', // list_number enabled + 'Title should escape &, <, ', and "', + '', + '
  • ', + '
  • ', + '', + '4. ', // list_number enabled + 'Chapter 1 should be printed to toc', + '', + '
  • ', '' ].join(''); - const resultTitle_1_1 = [ + toc(html).should.eql(expected); + }); + + it('class', () => { + const className = 'foo'; + const expected = [ + '
      ', + '
    1. ', + '', + '1. ', // list_number enabled + 'Title 1', + '', '
        ', '
      1. ', '', - ifTrue(listNumber, '1.1. ', ''), + '1.1. ', // list_number enabled 'Title 1.1', '', - ifTrue(maxDepth >= 3, resultTitle_1_1_1, ''), + '
          ', + '
        1. ', + '', + '1.1.1. ', // list_number enabled + 'Title 1.1.1', + '', + '
        2. ', + '
        ', '
      2. ', '
      3. ', '', - ifTrue(listNumber, '1.2. ', ''), + '1.2. ', // list_number enabled 'Title 1.2', '', '
      4. ', '
      5. ', '', - ifTrue(listNumber, '1.3. ', ''), + '1.3. ', // list_number enabled 'Title 1.3', '', - ifTrue(maxDepth >= 3, resultTitle_1_3_1, ''), + '
          ', + '
        1. ', + '', + '1.3.1. ', // list_number enabled + 'Title 1.3.1', + '', '
        2. ', - '
        ' - ].join(''); - - const resultTitle_2_1 = [ + '
      ', + '
    2. ', + '
    ', + '', + '
  • ', + '', + '2. ', // list_number enabled + 'Title 2', + '', '
      ', '
    1. ', '', - ifTrue(listNumber, '2.1. ', ''), + '2.1. ', // list_number enabled 'Title 2.1', '', '
    2. ', + '
    ', + '
  • ', + '
  • ', + '', + '3. ', // list_number enabled + 'Title should escape &, <, ', and "', + '', + '
  • ', + '
  • ', + '', + '4. ', // list_number enabled + 'Chapter 1 should be printed to toc', + '', + '
  • ', '' ].join(''); - const resultAllTitles_Level1 = [ + toc(html, { class: 'foo' }).should.eql(expected); + }); + + it('list_number', () => { + const className = 'toc'; + const expected = [ + '
      ', '
    1. ', '', - ifTrue(listNumber, '1. ', ''), + // '1. ', 'Title 1', '', - ifTrue(maxDepth >= 2, resultTitle_1_1, ''), + '
        ', + '
      1. ', + '', + // '1.1. ', + 'Title 1.1', + '', + '
          ', + '
        1. ', + '', + // '1.1.1. ', + 'Title 1.1.1', + '', + '
        2. ', + '
        ', + '
      2. ', + '
      3. ', + '', + // '1.2. ', + 'Title 1.2', + '', + '
      4. ', + '
      5. ', + '', + // '1.3. ', + 'Title 1.3', + '', + '
          ', + '
        1. ', + '', + // '1.3.1. ', + 'Title 1.3.1', + '', + '
        2. ', + '
        ', + '
      6. ', + '
      ', '
    2. ', '
    3. ', '', - ifTrue(listNumber, '2. ', ''), + // '2. ', 'Title 2', '', - ifTrue(maxDepth >= 2, resultTitle_2_1, ''), + '
        ', + '
      1. ', + '', + // '2.1. ', + 'Title 2.1', + '', + '
      2. ', + '
      ', '
    4. ', '
    5. ', '', - ifTrue(listNumber, '3. ', ''), + // '3. ', 'Title should escape &, <, ', and "', '', '
    6. ', '
    7. ', '', - ifTrue(listNumber, '4. ', ''), + // '4. ', 'Chapter 1 should be printed to toc', '', - '
    8. ' - ].join(''); - - const result = [ - '
        ', - ifTrue(maxDepth >= 1, resultAllTitles_Level1, ''), + '', '
      ' ].join(''); - return result; - }; - - it('default', () => { - genResult().should.eql(toc(html)); - }); - - it('class', () => { - const options = { - class: 'foo' - }; - - genResult(options).should.eql(toc(html, options)); - }); - - it('list_number', () => { - const options = { - list_number: false - }; - - genResult(options).should.eql(toc(html, options)); + toc(html, { list_number: false }).should.eql(expected); }); it('max_depth', () => { - const options = { - max_depth: 2 - }; + const className = 'toc'; + const expected = [ + '
        ', + '
      1. ', + '', + '1. ', + 'Title 1', + '', + '
          ', + '
        1. ', + '', + '1.1. ', + 'Title 1.1', + '', + '
        2. ', + '
        3. ', + '', + '1.2. ', + 'Title 1.2', + '', + '
        4. ', + '
        5. ', + '', + '1.3. ', + 'Title 1.3', + '', + '
        6. ', + '
        ', + '
      2. ', + '
      3. ', + '', + '2. ', + 'Title 2', + '', + '
          ', + '
        1. ', + '', + '2.1. ', + 'Title 2.1', + '', + '
        2. ', + '
        ', + '
      4. ', + '
      5. ', + '', + '3. ', + 'Title should escape &, <, ', and "', + '', + '
      6. ', + '
      7. ', + '', + '4. ', + 'Chapter 1 should be printed to toc', + '', + '
      8. ', + '
      ' + ].join(''); - genResult(options).should.eql(toc(html, options)); + toc(html, { max_depth: 2 }).should.eql(expected); }); }); From 0cfb532b601e7cb938778292c11bd41250e467b5 Mon Sep 17 00:00:00 2001 From: SukkaW Date: Fri, 20 Dec 2019 22:23:26 +0800 Subject: [PATCH 2/2] feat(toc_helper): add min_depth option --- lib/plugins/helper/toc.js | 3 ++- test/scripts/helpers/toc.js | 50 +++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/lib/plugins/helper/toc.js b/lib/plugins/helper/toc.js index 4eb5093e8..4e55c1fd4 100644 --- a/lib/plugins/helper/toc.js +++ b/lib/plugins/helper/toc.js @@ -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 ''; diff --git a/test/scripts/helpers/toc.js b/test/scripts/helpers/toc.js index 8d5f9fcb5..61f0525cc 100644 --- a/test/scripts/helpers/toc.js +++ b/test/scripts/helpers/toc.js @@ -311,4 +311,54 @@ describe('toc', () => { toc(html, { max_depth: 2 }).should.eql(expected); }); + + it('min_depth', () => { + const className = 'toc'; + const expected = [ + '
        ', + '
      1. ', + '', + '1. ', + 'Title 1.1', + '', + '
          ', + '
        1. ', + '', + '1.1. ', + 'Title 1.1.1', + '', + '
        2. ', + '
        ', + '
      2. ', + '
      3. ', + '', + '2. ', + 'Title 1.2', + '', + '
      4. ', + '
      5. ', + '', + '3. ', + 'Title 1.3', + '', + '
          ', + '
        1. ', + '', + '3.1. ', + 'Title 1.3.1', + '', + '
        2. ', + '
        ', + '
      6. ', + '
      7. ', + '', + '4. ', + 'Title 2.1', + '', + '
      8. ', + '
      ' + ].join(''); + + toc(html, { min_depth: 2 }).should.eql(expected); + }); });