From e56d8b537fe40d5380915b788e96ff47356fc914 Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Tue, 4 May 2021 13:25:50 +0200 Subject: [PATCH 1/9] feat: improve support of backtick strings Only supports code span delimited with a single backtick char and gives up if it finds an edge case. --- commit-to-output.js | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/commit-to-output.js b/commit-to-output.js index e693678..ba09120 100644 --- a/commit-to-output.js +++ b/commit-to-output.js @@ -4,9 +4,30 @@ const chalk = require('chalk') const reverts = require('./reverts') const groups = require('./groups') +function cleanUnsupportedMarkdown (txt) { + // escape _~*\[]<>` + return txt.replace(/([_~*\\[\]<>`])/g, '\\$1') +} function cleanMarkdown (txt) { - // escape _~*\[]<> - return txt.replace(/([_~*\\[\]<>])/g, '\\$1') + // Escape backticks for edge case scenarii (no code span support). + if (txt.includes('``') || txt.includes('\\`')) { + return cleanUnsupportedMarkdown(txt) + } + + const backtickSplit = txt.split('`') + // If there's an odd number of backticks, give up and escape them all. + if (backtickSplit.length % 2 === 0) return cleanUnsupportedMarkdown(txt) + + let cleanMdString = '' + for (let i = 0; i < backtickSplit.length; i++) { + const isInsideBacktickString = i % 2 + cleanMdString += isInsideBacktickString + // No escaping inside a code span. + ? `\`${backtickSplit[i]}\`` + // otherwise escape _~*\[]<> + : backtickSplit[i].replace(/([_~*\\[\]<>])/g, '\\$1') + } + return cleanMdString } const formatType = { From e68ae3cda6c9e69b18e11a8cf31f296d62c003ee Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Thu, 6 May 2021 09:41:08 +0200 Subject: [PATCH 2/9] test: commit_msg with `backtick` string From 2ab3cda07bf875a46c7a635aa14d680ecbb347aa Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Thu, 6 May 2021 09:41:29 +0200 Subject: [PATCH 3/9] test: commit_msg with `back_tick` string From 753fcf717585e0996b242bc98b110e90c79506ac Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Thu, 6 May 2021 09:41:45 +0200 Subject: [PATCH 4/9] test: commit_msg with ``backtick ` string`` From 2305fbbb3bedb521c37e4e058ee56d33e0314c97 Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Thu, 6 May 2021 09:41:58 +0200 Subject: [PATCH 5/9] test: commit_msg with `backtick\` string From 7c1eedffc3f85bd53ab67fd7b5cc6c82013fde3b Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Thu, 6 May 2021 09:43:23 +0200 Subject: [PATCH 6/9] test: `commit_msg` starting with a backtick string From 6503a6745028fbb344c9eef5f800d4d18a1f6360 Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Thu, 6 May 2021 10:14:28 +0200 Subject: [PATCH 7/9] test: `commit_msg` with an escaped \` backtick char From b0dc2f30c4c7e800052ccdf5ef91fdf9494f191e Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Thu, 6 May 2021 10:14:37 +0200 Subject: [PATCH 8/9] test: `commit_msg` with an unescaped ` backtick char From deb28cc8cef59bfdf753968717e79ba32f40ceb0 Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Thu, 6 May 2021 09:51:01 +0200 Subject: [PATCH 9/9] add tests --- test.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/test.js b/test.js index 50918e5..71b4999 100644 --- a/test.js +++ b/test.js @@ -101,3 +101,18 @@ test('test blank commit-url', (t) => { `) t.end() }) + +test('test backtick strings in commit messages', (t) => { + t.equal( + exec('--start-ref=e56d8b537f --end-ref=b0dc2f30c4 --filter-release --commit-url=https://yeehaw.com/{ref}/{ref}/{ghUser}/{ghRepo}/'), + `* [[\`b0dc2f30c4\`](https://yeehaw.com/b0dc2f30c4/b0dc2f30c4/nodejs/changelog-maker/)] - **test**: \\\`commit\\_msg\\\` with an unescaped \\\` backtick char (Antoine du Hamel) +* [[\`6503a67450\`](https://yeehaw.com/6503a67450/6503a67450/nodejs/changelog-maker/)] - **test**: \\\`commit\\_msg\\\` with an escaped \\\\\\\` backtick char (Antoine du Hamel) +* [[\`7c1eedffc3\`](https://yeehaw.com/7c1eedffc3/7c1eedffc3/nodejs/changelog-maker/)] - **test**: \`commit_msg\` starting with a backtick string (Antoine du Hamel) +* [[\`2305fbbb3b\`](https://yeehaw.com/2305fbbb3b/2305fbbb3b/nodejs/changelog-maker/)] - **test**: commit\\_msg with \\\`backtick\\\\\\\` string (Antoine du Hamel) +* [[\`753fcf7175\`](https://yeehaw.com/753fcf7175/753fcf7175/nodejs/changelog-maker/)] - **test**: commit\\_msg with \\\`\\\`backtick \\\` string\\\`\\\` (Antoine du Hamel) +* [[\`2ab3cda07b\`](https://yeehaw.com/2ab3cda07b/2ab3cda07b/nodejs/changelog-maker/)] - **test**: commit\\_msg with \`back_tick\` string (Antoine du Hamel) +* [[\`e68ae3cda6\`](https://yeehaw.com/e68ae3cda6/e68ae3cda6/nodejs/changelog-maker/)] - **test**: commit\\_msg with \`backtick\` string (Antoine du Hamel) +* [[\`e56d8b537f\`](https://yeehaw.com/e56d8b537f/e56d8b537f/nodejs/changelog-maker/)] - **feat**: improve support of backtick strings (Antoine du Hamel) +`) + t.end() +})