From 9133917a6c97c6903dd0495a0a02bd1adb4fbd53 Mon Sep 17 00:00:00 2001 From: Bjarne Thorsted <885853+bjtho08@users.noreply.github.com> Date: Mon, 22 Apr 2024 12:31:56 +0200 Subject: [PATCH] add nested ordered list option --- main.js | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/main.js b/main.js index acb22bb..94f65e2 100644 --- a/main.js +++ b/main.js @@ -22,7 +22,7 @@ const availableOptions = { style: { type: 'value', default: 'nestedList', - values: ['nestedList', 'inlineFirstLevel'], + values: ['nestedList', 'nestedOrderedList', 'inlineFirstLevel'], comment: 'TOC style (nestedList|inlineFirstLevel)', }, minLevel: { @@ -127,6 +127,7 @@ class Renderer extends MarkdownRenderChild { function getMarkdownFromHeadings(headings, options) { const markdownHandlersByStyle = { nestedList: getMarkdownNestedListFromHeadings, + nestedOrderedList: getMarkdownNestedOrderedListFromHeadings, inlineFirstLevel: getMarkdownInlineFirstLevelFromHeadings, } let markdown = '' @@ -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 items = headings .filter((heading) => heading.level === 1)