From cd827a4050626fadd96c66470bee89d4eda4ecb0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johan=20Satg=C3=A9?= Date: Sun, 19 May 2024 10:13:26 +0200 Subject: [PATCH] Refactor nested ordered list + tests --- README.md | 2 +- main.js | 23 ++++++++--------------- test/headings.test.js | 15 +++++++++++++++ 3 files changed, 24 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index dfffd0e..1629f38 100644 --- a/README.md +++ b/README.md @@ -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 | diff --git a/main.js b/main.js index 1de3861..bbc2b0f 100644 --- a/main.js +++ b/main.js @@ -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', @@ -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 } diff --git a/test/headings.test.js b/test/headings.test.js index 614ad8d..67a3a29 100644 --- a/test/headings.test.js +++ b/test/headings.test.js @@ -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