From 2fdef8048522fbc87c58f97e13c323823740900b Mon Sep 17 00:00:00 2001 From: Luis Urrutia Date: Tue, 16 Dec 2025 20:12:23 +0100 Subject: [PATCH] fix: ignore issue references inside HTML comments --- index.js | 11 ++++++---- test/index.test.js | 51 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 995f0ee..24c506b 100644 --- a/index.js +++ b/index.js @@ -12,6 +12,7 @@ const {hasOwnProperty} = Object.prototype; const FENCE_BLOCK_REGEXP = /^(([ \t]*`{3,4})([^\n]*)([\s\S]+?)(^[ \t]*\2))/gm; const CODE_BLOCK_REGEXP = /(`(?!\\))((?:.(?!\1(?!\\)))*.?)\1/g; const HTML_CODE_BLOCK_REGEXP = /()+?)[\S\s])*(<\/code>)+?/gim; +const HTML_COMMENT_REGEXP = //g; const LEADING_TRAILING_SLASH_REGEXP = /^\/?([^/]+(?:\/[^/]+)*)\/?$/; const TRAILING_SLASH_REGEXP = /\/?$/; @@ -76,13 +77,15 @@ function parse(text, regexp, mentionRegexp, {actions, issuePrefixes, hosts}) { refs: [], mentions: [], }; - let noCodeBlock = inverse(inverse(text.replace(FENCE_BLOCK_REGEXP, '')).replace(CODE_BLOCK_REGEXP, '')); + let filteredText = inverse(inverse(text.replace(FENCE_BLOCK_REGEXP, '')).replace(CODE_BLOCK_REGEXP, '')); - while (regexp.test(noCodeBlock)) { - noCodeBlock = noCodeBlock.replace(HTML_CODE_BLOCK_REGEXP, ''); + while (regexp.test(filteredText)) { + filteredText = filteredText.replace(HTML_CODE_BLOCK_REGEXP, ''); } - while ((parsed = regexp.exec(noCodeBlock)) !== null) { + filteredText = filteredText.replace(HTML_COMMENT_REGEXP, ' '); + + while ((parsed = regexp.exec(filteredText)) !== null) { let [raw, action, slug, prefix, issue, mentions] = parsed; prefix = prefix && issuePrefixes.some(issuePrefix => issuePrefix.toUpperCase() === prefix.toUpperCase()) diff --git a/test/index.test.js b/test/index.test.js index a0c941b..ca93363 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -298,6 +298,57 @@ Fix #2 ]); }); +test('Exclude HTML comments', t => { + t.deepEqual(m('github')('Fix #1 Fix #3').actions.close, [ + {issue: '1', action: 'Fix', slug: undefined, prefix: '#', raw: 'Fix #1'}, + {issue: '3', action: 'Fix', slug: undefined, prefix: '#', raw: 'Fix #3'}, + ]); + + t.deepEqual(m('github')('Fix #1 #3').actions.close, [ + {issue: '1', action: 'Fix', slug: undefined, prefix: '#', raw: 'Fix #1'}, + ]); + + t.deepEqual(m('github')('Fix #1 #3').refs, [{issue: '3', slug: undefined, prefix: '#', raw: '#3'}]); + + t.deepEqual( + m('github')(`Fix #1 Fix #4`).actions.close, + [ + {issue: '1', action: 'Fix', slug: undefined, prefix: '#', raw: 'Fix #1'}, + {issue: '4', action: 'Fix', slug: undefined, prefix: '#', raw: 'Fix #4'}, + ] + ); + + t.deepEqual(m('github')('').actions.close, []); + + t.deepEqual(m('github')('').actions.close, []); + + t.deepEqual(m('github')('Fix #1 Fix #3 Fix #5').actions.close, [ + {issue: '1', action: 'Fix', slug: undefined, prefix: '#', raw: 'Fix #1'}, + {issue: '3', action: 'Fix', slug: undefined, prefix: '#', raw: 'Fix #3'}, + {issue: '5', action: 'Fix', slug: undefined, prefix: '#', raw: 'Fix #5'}, + ]); + + t.deepEqual(m('github')('Fix #1 Fix #3 #5 #7').actions.close, [ + {issue: '1', action: 'Fix', slug: undefined, prefix: '#', raw: 'Fix #1'}, + {issue: '3', action: 'Fix', slug: undefined, prefix: '#', raw: 'Fix #3'}, + ]); + + t.deepEqual(m('github')('Fix #1 Fix #3 #5 #7').refs, [ + {issue: '5', slug: undefined, prefix: '#', raw: '#5'}, + {issue: '7', slug: undefined, prefix: '#', raw: '#7'}, + ]); + + t.deepEqual(m('github')('Fix #1Fix #3').actions.close, [ + {issue: '1', action: 'Fix', slug: undefined, prefix: '#', raw: 'Fix #1'}, + {issue: '3', action: 'Fix', slug: undefined, prefix: '#', raw: 'Fix #3'}, + ]); + + t.deepEqual(m('github')(' @other').mentions, [{raw: '@other', prefix: '@', user: 'other'}]); +}); + test('Empty options', t => { t.deepEqual(m({actions: {close: []}, issuePrefixes: [], mentionsPrefixes: []})('Fix #1, @user'), { actions: {duplicate: []},