Skip to content

Commit 779423b

Browse files
authored
fix: ignore issue references inside HTML comments (#144)
1 parent c7b00ff commit 779423b

2 files changed

Lines changed: 58 additions & 4 deletions

File tree

index.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const {hasOwnProperty} = Object.prototype;
1212
const FENCE_BLOCK_REGEXP = /^(([ \t]*`{3,4})([^\n]*)([\s\S]+?)(^[ \t]*\2))/gm;
1313
const CODE_BLOCK_REGEXP = /(`(?!\\))((?:.(?!\1(?!\\)))*.?)\1/g;
1414
const HTML_CODE_BLOCK_REGEXP = /(<code)+?((?!(<code|<\/code>)+?)[\S\s])*(<\/code>)+?/gim;
15+
const HTML_COMMENT_REGEXP = /<!--[\s\S]*?-->/g;
1516
const LEADING_TRAILING_SLASH_REGEXP = /^\/?([^/]+(?:\/[^/]+)*)\/?$/;
1617
const TRAILING_SLASH_REGEXP = /\/?$/;
1718

@@ -76,13 +77,15 @@ function parse(text, regexp, mentionRegexp, {actions, issuePrefixes, hosts}) {
7677
refs: [],
7778
mentions: [],
7879
};
79-
let noCodeBlock = inverse(inverse(text.replace(FENCE_BLOCK_REGEXP, '')).replace(CODE_BLOCK_REGEXP, ''));
80+
let filteredText = inverse(inverse(text.replace(FENCE_BLOCK_REGEXP, '')).replace(CODE_BLOCK_REGEXP, ''));
8081

81-
while (regexp.test(noCodeBlock)) {
82-
noCodeBlock = noCodeBlock.replace(HTML_CODE_BLOCK_REGEXP, '');
82+
while (regexp.test(filteredText)) {
83+
filteredText = filteredText.replace(HTML_CODE_BLOCK_REGEXP, '');
8384
}
8485

85-
while ((parsed = regexp.exec(noCodeBlock)) !== null) {
86+
filteredText = filteredText.replace(HTML_COMMENT_REGEXP, ' ');
87+
88+
while ((parsed = regexp.exec(filteredText)) !== null) {
8689
let [raw, action, slug, prefix, issue, mentions] = parsed;
8790
prefix =
8891
prefix && issuePrefixes.some(issuePrefix => issuePrefix.toUpperCase() === prefix.toUpperCase())

test/index.test.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,57 @@ Fix #2
298298
]);
299299
});
300300

301+
test('Exclude HTML comments', t => {
302+
t.deepEqual(m('github')('Fix #1 <!-- Fix #2 --> Fix #3').actions.close, [
303+
{issue: '1', action: 'Fix', slug: undefined, prefix: '#', raw: 'Fix #1'},
304+
{issue: '3', action: 'Fix', slug: undefined, prefix: '#', raw: 'Fix #3'},
305+
]);
306+
307+
t.deepEqual(m('github')('Fix #1 <!-- Fixes #2 --> #3').actions.close, [
308+
{issue: '1', action: 'Fix', slug: undefined, prefix: '#', raw: 'Fix #1'},
309+
]);
310+
311+
t.deepEqual(m('github')('Fix #1 <!-- Fixes #2 --> #3').refs, [{issue: '3', slug: undefined, prefix: '#', raw: '#3'}]);
312+
313+
t.deepEqual(
314+
m('github')(`Fix #1 <!--
315+
Fix #2
316+
Closes #3
317+
--> Fix #4`).actions.close,
318+
[
319+
{issue: '1', action: 'Fix', slug: undefined, prefix: '#', raw: 'Fix #1'},
320+
{issue: '4', action: 'Fix', slug: undefined, prefix: '#', raw: 'Fix #4'},
321+
]
322+
);
323+
324+
t.deepEqual(m('github')('<!-- Fix #1 -->').actions.close, []);
325+
326+
t.deepEqual(m('github')('<!--Fix #1-->').actions.close, []);
327+
328+
t.deepEqual(m('github')('Fix #1 <!-- Fix #2 --> Fix #3 <!-- Fix #4 --> Fix #5').actions.close, [
329+
{issue: '1', action: 'Fix', slug: undefined, prefix: '#', raw: 'Fix #1'},
330+
{issue: '3', action: 'Fix', slug: undefined, prefix: '#', raw: 'Fix #3'},
331+
{issue: '5', action: 'Fix', slug: undefined, prefix: '#', raw: 'Fix #5'},
332+
]);
333+
334+
t.deepEqual(m('github')('Fix #1 <!-- Fix #2 --> Fix #3 <!-- Fix #4 --> #5 <!-- Fix #6 --> #7').actions.close, [
335+
{issue: '1', action: 'Fix', slug: undefined, prefix: '#', raw: 'Fix #1'},
336+
{issue: '3', action: 'Fix', slug: undefined, prefix: '#', raw: 'Fix #3'},
337+
]);
338+
339+
t.deepEqual(m('github')('Fix #1 <!-- Fix #2 --> Fix #3 <!-- Fix #4 --> #5 <!-- Fix #6 --> #7').refs, [
340+
{issue: '5', slug: undefined, prefix: '#', raw: '#5'},
341+
{issue: '7', slug: undefined, prefix: '#', raw: '#7'},
342+
]);
343+
344+
t.deepEqual(m('github')('Fix #1<!-- Fix #2 -->Fix #3').actions.close, [
345+
{issue: '1', action: 'Fix', slug: undefined, prefix: '#', raw: 'Fix #1'},
346+
{issue: '3', action: 'Fix', slug: undefined, prefix: '#', raw: 'Fix #3'},
347+
]);
348+
349+
t.deepEqual(m('github')('<!-- @user --> @other').mentions, [{raw: '@other', prefix: '@', user: 'other'}]);
350+
});
351+
301352
test('Empty options', t => {
302353
t.deepEqual(m({actions: {close: []}, issuePrefixes: [], mentionsPrefixes: []})('Fix #1, @user'), {
303354
actions: {duplicate: []},

0 commit comments

Comments
 (0)