Skip to content

Commit

Permalink
Merge pull request #42 from bjtho08/feature/nested-ordered-list-support
Browse files Browse the repository at this point in the history
Add nested ordered list option
  • Loading branch information
johansatge authored May 19, 2024
2 parents cf11bec + 9133917 commit 3c91704
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion main.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const availableOptions = {
style: {
type: 'value',
default: 'nestedList',
values: ['nestedList', 'inlineFirstLevel'],
values: ['nestedList', 'nestedOrderedList', 'inlineFirstLevel'],
comment: 'TOC style (nestedList|inlineFirstLevel)',
},
minLevel: {
Expand Down Expand Up @@ -127,6 +127,7 @@ class Renderer extends MarkdownRenderChild {
function getMarkdownFromHeadings(headings, options) {
const markdownHandlersByStyle = {
nestedList: getMarkdownNestedListFromHeadings,
nestedOrderedList: getMarkdownNestedOrderedListFromHeadings,
inlineFirstLevel: getMarkdownInlineFirstLevelFromHeadings,
}
let markdown = ''
Expand All @@ -151,6 +152,22 @@ function getMarkdownNestedListFromHeadings(headings, options) {
return lines.length > 0 ? lines.join('\n') : null
}

function getMarkdownNestedOrderedListFromHeadings(headings, options) {
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
})
return lines.length > 0 ? lines.join('\n') : null
}

function getMarkdownInlineFirstLevelFromHeadings(headings, options) {
const minLevel = options.minLevel > 0
? options.minLevel
Expand Down

0 comments on commit 3c91704

Please sign in to comment.