Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor nested ordered list + tests #45

Merged
merged 1 commit into from
May 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading