Skip to content

Commit

Permalink
Option to hide TOC when empty
Browse files Browse the repository at this point in the history
  • Loading branch information
johansatge committed Nov 24, 2024
1 parent 708adee commit 981b15b
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 5 deletions.
20 changes: 15 additions & 5 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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) {
Expand Down
13 changes: 13 additions & 0 deletions test/headings.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
11 changes: 11 additions & 0 deletions test/options.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ describe('Options', () => {
includeLinks: true,
minLevel: 0,
maxLevel: 0,
hideWhenEmpty: false,
debugInConsole: false,
})
})
Expand All @@ -20,6 +21,7 @@ describe('Options', () => {
minLevel: 1
maxLevel: 2 # Some other comment
includeLinks: false
hideWhenEmpty: true
debugInConsole: true
`
const options = parseOptionsFromSourceText(optionsText)
Expand All @@ -29,6 +31,7 @@ describe('Options', () => {
includeLinks: false,
minLevel: 1,
maxLevel: 2,
hideWhenEmpty: true,
debugInConsole: true,
})
})
Expand Down Expand Up @@ -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')
Expand Down

0 comments on commit 981b15b

Please sign in to comment.