From 80aca15d7235082187d6eee6054a69ceed9c45db Mon Sep 17 00:00:00 2001 From: SethCohen Date: Fri, 3 Apr 2026 12:32:35 -0400 Subject: [PATCH] feat: enhance reduceHeadings function to handle indented and closed markdown headings and add tests for new functionality. resolves #51 --- .gitignore | 1 + index.js | 17 +++++++++++++++-- tests/index.test.js | 10 ++++++++++ 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 2c901c7..0fd2342 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,4 @@ release.json repository.json user.json tests/sample-test-release-v2.json +tests/sample-test-release-v3.json diff --git a/index.js b/index.js index d9f9197..4cc8c33 100644 --- a/index.js +++ b/index.js @@ -61,8 +61,21 @@ const removeGithubReferenceLinks = (text) => text * @returns {string} The text with reduced heading sizes. */ const reduceHeadings = (text) => text - .replace(/^###\s+(.+)$/gm, '**__$1__**') // Convert H3 to bold + underline - .replace(/^##\s+(.+)$/gm, '**$1**'); // Convert H2 to bold + .split('\n') + .map((line) => { + const h3 = line.match(/^\s*###\s+(.+?)\s*#*\s*$/); + if (h3) { + return `**__${h3[1].trim()}__**`; + } + + const h2 = line.match(/^\s*##\s+(.+?)\s*#*\s*$/); + if (h2) { + return `**${h2[1].trim()}**`; + } + + return line; + }) + .join('\n'); /** * Converts PR, issue, and changelog links to markdown format, ignoring existing markdown links. diff --git a/tests/index.test.js b/tests/index.test.js index 9c25880..36f9129 100644 --- a/tests/index.test.js +++ b/tests/index.test.js @@ -70,6 +70,16 @@ describe('index.js utility functions', () => { expect(reduceHeadings('## Heading2')).toBe('**Heading2**'); }); + test('reduceHeadings reduces repeated section headings', () => { + const input = '### Bug Fixes\n- fix thing\n\n### Features\n- add thing'; + expect(reduceHeadings(input)).toBe('**__Bug Fixes__**\n- fix thing\n\n**__Features__**\n- add thing'); + }); + + test('reduceHeadings handles indented and closed markdown headings', () => { + const input = ' ## Changes ##\n ### Features ###'; + expect(reduceHeadings(input)).toBe('**Changes**\n**__Features__**'); + }); + test('convertLinksToMarkdown converts PR/issue/changelog links', () => { const pr = 'https://github.com/owner/repo/pull/1'; const issue = 'https://github.com/owner/repo/issues/2';