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 02c2cd17f..61f0525cc 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,349 @@ 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)); + toc(html, { list_number: false }).should.eql(expected); }); - it('list_number', () => { - const options = { - list_number: false - }; + it('max_depth', () => { + 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); }); - it('max_depth', () => { - const options = { - max_depth: 2 - }; + 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(''); - genResult(options).should.eql(toc(html, options)); + toc(html, { min_depth: 2 }).should.eql(expected); }); });