From 6c29971eb4482108e452120b4ab36a8fae0add62 Mon Sep 17 00:00:00 2001 From: Baoshuo Ren Date: Thu, 30 Jun 2022 23:49:42 +0800 Subject: [PATCH] feat(helper/toc): more flexible class name (#5010) --- lib/plugins/helper/toc.js | 24 ++++++++--- test/scripts/helpers/toc.js | 79 +++++++++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+), 6 deletions(-) diff --git a/lib/plugins/helper/toc.js b/lib/plugins/helper/toc.js index e6e4fa8c7b..e055ffd6f2 100644 --- a/lib/plugins/helper/toc.js +++ b/lib/plugins/helper/toc.js @@ -7,6 +7,12 @@ function tocHelper(str, options = {}) { min_depth: 1, max_depth: 6, class: 'toc', + class_item: '', + class_link: '', + class_text: '', + class_child: '', + class_number: '', + class_level: '', list_number: true }, options); @@ -15,6 +21,12 @@ function tocHelper(str, options = {}) { if (!data.length) return ''; const className = escapeHTML(options.class); + const itemClassName = escapeHTML(options.class_item || options.class + '-item'); + const linkClassName = escapeHTML(options.class_link || options.class + '-link'); + const textClassName = escapeHTML(options.class_text || options.class + '-text'); + const childClassName = escapeHTML(options.class_child || options.class + '-child'); + const numberClassName = escapeHTML(options.class_number || options.class + '-number'); + const levelClassName = escapeHTML(options.class_level || options.class + '-level'); const listNumber = options.list_number; let result = `
    `; @@ -42,7 +54,7 @@ function tocHelper(str, options = {}) { } if (level > lastLevel) { - result += `
      `; + result += `
        `; } else { result += ''; } @@ -50,15 +62,15 @@ function tocHelper(str, options = {}) { firstLevel = level; } - result += `
      1. `; + result += `
      2. `; if (href) { - result += ``; + result += ``; } else { - result += ``; + result += ``; } if (listNumber && !el.unnumbered) { - result += ``; + result += ``; for (let i = firstLevel - 1; i < level; i++) { result += `${lastNumber[i]}.`; @@ -67,7 +79,7 @@ function tocHelper(str, options = {}) { result += ' '; } - result += `${text}`; + result += `${text}`; lastLevel = level; } diff --git a/test/scripts/helpers/toc.js b/test/scripts/helpers/toc.js index 08d7c3ebeb..47f0014790 100644 --- a/test/scripts/helpers/toc.js +++ b/test/scripts/helpers/toc.js @@ -476,4 +476,83 @@ describe('toc', () => { toc(input, { list_number: true, class: className }).should.eql(expected); }); + + it('custom class', () => { + const className = 'foo'; + const childClassName = 'bar'; + const expected = [ + '
          ', + '
        1. ', + '', + '1. ', // list_number enabled + 'Title 1', + '', + '
            ', + '
          1. ', + '', + '1.1. ', // list_number enabled + 'Title 1.1', + '', + '
              ', + '
            1. ', + '', + '1.1.1. ', // list_number enabled + 'Title 1.1.1', + '', + '
            2. ', + '
            ', + '
          2. ', + '
          3. ', + '', + '1.2. ', // list_number enabled + 'Title 1.2', + '', + '
          4. ', + '
          5. ', + '', + '1.3. ', // list_number enabled + 'Title 1.3', + '', + '
              ', + '
            1. ', + '', + '1.3.1. ', // list_number enabled + 'Title 1.3.1', + '', + '
            2. ', + '
            ', + '
          6. ', + '
          ', + '
        2. ', + '
        3. ', + '', + '2. ', // list_number enabled + 'Title 2', + '', + '
            ', + '
          1. ', + '', + '2.1. ', // list_number enabled + 'Title 2.1', + '', + '
          2. ', + '
          ', + '
        4. ', + '
        5. ', + '', + '3. ', // list_number enabled + 'Title should escape &, <, ', and "', + '', + '
        6. ', + '
        7. ', + '', + '4. ', // list_number enabled + 'Chapter 1 should be printed to toc', + '', + '
        8. ', + '
        ' + ].join(''); + + toc(html, { class: 'foo', class_child: 'bar' }).should.eql(expected); + }); });