diff --git a/main.js b/main.js index bbc2b0f..557cdd9 100644 --- a/main.js +++ b/main.js @@ -40,6 +40,11 @@ const availableOptions = { default: true, comment: 'Make headings clickable', }, + hideWhenEmpty: { + type: 'boolean', + default: false, + comment: 'Hide TOC if no headings are found' + }, debugInConsole: { type: 'boolean', default: false, @@ -130,13 +135,18 @@ function getMarkdownFromHeadings(headings, options) { nestedOrderedList: getMarkdownNestedOrderedListFromHeadings, inlineFirstLevel: getMarkdownInlineFirstLevelFromHeadings, } - let markdown = '' + let titleMarkdown = '' if (options.title && options.title.length > 0) { - markdown += options.title + '\n' + titleMarkdown += options.title + '\n' + } + const markdownHeadings = markdownHandlersByStyle[options.style](headings, options) + if (markdownHeadings === null) { + if (options.hideWhenEmpty) { + return '' + } + return titleMarkdown + '_Table of contents: no headings found_' } - const noHeadingMessage = '_Table of contents: no headings found_' - markdown += markdownHandlersByStyle[options.style](headings, options) || noHeadingMessage - return markdown + return titleMarkdown + markdownHeadings } function getMarkdownNestedListFromHeadings(headings, options) { diff --git a/test/headings.test.js b/test/headings.test.js index 67a3a29..44b9ab9 100644 --- a/test/headings.test.js +++ b/test/headings.test.js @@ -29,6 +29,19 @@ const testHeadingsWithSpecialChars = [ ] describe('Headings', () => { + test('Returns default message if no headings', () => { + const options = parseOptionsFromSourceText('') + const md = getMarkdownFromHeadings([], options) + expect(md).toContain('no headings found') + }) + + test('Returns empty TOC if no headings & option enabled', () => { + const options = parseOptionsFromSourceText('') + options.hideWhenEmpty = true + const md = getMarkdownFromHeadings([], options) + expect(md).toEqual('') + }) + test('Returns indented list with links', () => { const options = parseOptionsFromSourceText('') const md = getMarkdownFromHeadings(testStandardHeadings, options) diff --git a/test/options.test.js b/test/options.test.js index df1de9c..057f478 100644 --- a/test/options.test.js +++ b/test/options.test.js @@ -9,6 +9,7 @@ describe('Options', () => { includeLinks: true, minLevel: 0, maxLevel: 0, + hideWhenEmpty: false, debugInConsole: false, }) }) @@ -20,6 +21,7 @@ describe('Options', () => { minLevel: 1 maxLevel: 2 # Some other comment includeLinks: false + hideWhenEmpty: true debugInConsole: true ` const options = parseOptionsFromSourceText(optionsText) @@ -29,6 +31,7 @@ describe('Options', () => { includeLinks: false, minLevel: 1, maxLevel: 2, + hideWhenEmpty: true, debugInConsole: true, }) }) @@ -79,6 +82,14 @@ describe('Options', () => { expect(error.message).toContain('Invalid value') } }) + test('On hideWhenEmpty', () => { + try { + const options = parseOptionsFromSourceText('hideWhenEmpty: maybe') + expect(options.hideWhenEmpty).toEqual('Should have thrown') + } catch(error) { + expect(error.message).toContain('Invalid value') + } + }) test('On debugInConsole', () => { try { const options = parseOptionsFromSourceText('debugInConsole: yes')