Skip to content

Commit

Permalink
Refactor nested ordered list + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
johansatge committed May 19, 2024
1 parent 3c91704 commit cd827a4
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 16 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ The following options are available:
| Option | Default value | Description |
| --- | --- | --- |
| `title` | _None_ | Title to display before the table of contents (supports Markdown) |
| `style` | `nestedList` | Table of contents style (can be `nestedList` or `inlineFirstLevel`) |
| `style` | `nestedList` | Table of contents style (can be `nestedList`, `nestedOrderedList` or `inlineFirstLevel`) |
| `minLevel` | `0` | Include headings from the specified level (`0` for no limit) |
| `maxLevel` | `0` | Include headings up to the specified level (`0` for no limit) |
| `includeLinks` | `true` | Make headings clickable |
Expand Down
23 changes: 8 additions & 15 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const availableOptions = {
type: 'value',
default: 'nestedList',
values: ['nestedList', 'nestedOrderedList', 'inlineFirstLevel'],
comment: 'TOC style (nestedList|inlineFirstLevel)',
comment: 'TOC style (nestedList|nestedOrderedList|inlineFirstLevel)',
},
minLevel: {
type: 'number',
Expand Down Expand Up @@ -140,30 +140,23 @@ function getMarkdownFromHeadings(headings, options) {
}

function getMarkdownNestedListFromHeadings(headings, options) {
const lines = []
const minLevel = options.minLevel > 0
? options.minLevel
: Math.min(...headings.map((heading) => heading.level))
headings.forEach((heading) => {
if (heading.level < minLevel) return
if (options.maxLevel > 0 && heading.level > options.maxLevel) return
lines.push(`${'\t'.repeat(heading.level - minLevel)}- ${getMarkdownHeading(heading, options)}`)
})
return lines.length > 0 ? lines.join('\n') : null
return getMarkdownListFromHeadings(headings, false, options)
}

function getMarkdownNestedOrderedListFromHeadings(headings, options) {
return getMarkdownListFromHeadings(headings, true, options)
}

function getMarkdownListFromHeadings(headings, isOrdered, options) {
const prefix = isOrdered ? '1.' : '-'
const lines = []
const levelEntries = {}
const minLevel = options.minLevel > 0
? options.minLevel
: Math.min(...headings.map((heading) => heading.level))
headings.forEach((heading) => {
if (heading.level < minLevel) return
if (options.maxLevel > 0 && heading.level > options.maxLevel) return
if (!levelEntries[heading.level]) levelEntries[heading.level] = 1
lines.push(`${'\t'.repeat(heading.level - minLevel)}${levelEntries[heading.level]}. ${getMarkdownHeading(heading, options)}`)
levelEntries[heading.level] += 1
lines.push(`${'\t'.repeat(heading.level - minLevel)}${prefix} ${getMarkdownHeading(heading, options)}`)
})
return lines.length > 0 ? lines.join('\n') : null
}
Expand Down
15 changes: 15 additions & 0 deletions test/headings.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,21 @@ describe('Headings', () => {
expect(md).toEqual(expectedMd)
})

test('Returns indented ordered list with links', () => {
const options = parseOptionsFromSourceText('')
options.style = 'nestedOrderedList'
const md = getMarkdownFromHeadings(testStandardHeadings, options)
const expectedMd = sanitizeMd(`
1. [[#Title 1 level 1|Title 1 level 1]]
1. [[#Title 1 level 2|Title 1 level 2]]
1. [[#Title 1 level 3|Title 1 level 3]]
1. [[#Title 2 level 1|Title 2 level 1]]
1. [[#Title 3 level 1|Title 3 level 1]]
1. [[#Title 3 level 2|Title 3 level 2]]
`)
expect(md).toEqual(expectedMd)
})

test('Returns indented list with min level', () => {
const options = parseOptionsFromSourceText('')
options.minLevel = 2
Expand Down

0 comments on commit cd827a4

Please sign in to comment.