Skip to content

Commit 4be0ee3

Browse files
committed
Do not replace multiple @ when using author annotation
1 parent 07698d3 commit 4be0ee3

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

__tests__/unit/actions/lib/searchAndReplaceSpecialAnnotation.test.js

+18
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,22 @@ describe('searchAndReplaceSpecialAnnotations', () => {
3636
}
3737
expect(searchAndReplaceSpecialAnnotations('this is @author', payload)).toBe('this is creator')
3838
})
39+
40+
test('@@author is replaced by @payload.user.login', () => {
41+
const payload = {
42+
user: {
43+
login: 'creator'
44+
}
45+
}
46+
expect(searchAndReplaceSpecialAnnotations('this is @@author', payload)).toBe('this is @creator')
47+
})
48+
49+
test('replaces annotation anywhere in the text', () => {
50+
const payload = {
51+
user: {
52+
login: 'creator'
53+
}
54+
}
55+
expect(searchAndReplaceSpecialAnnotations('this is something@author speaking', payload)).toBe('this is somethingcreator speaking')
56+
})
3957
})

lib/actions/lib/searchAndReplaceSpecialAnnotation.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ const searchAndReplaceSpecialAnnotations = (template, payload) => {
66
let newTemplate = template
77

88
for (const annotation of Object.keys(SPECIAL_ANNOTATION)) {
9-
const specialAnnotationRegex = new RegExp(`([^\\\\])${annotation}`)
9+
const specialAnnotationRegex = new RegExp(`(?<!\\\\)${annotation}`)
1010
const annotationAtStartRegex = new RegExp(`^${annotation}`)
1111
const escapeAnnotationRegex = new RegExp(`(\\\\){1}${annotation}`)
1212

13-
newTemplate = newTemplate.replace(specialAnnotationRegex, ` ${SPECIAL_ANNOTATION[annotation](payload)}`)
13+
newTemplate = newTemplate.replace(specialAnnotationRegex, `${SPECIAL_ANNOTATION[annotation](payload)}`)
1414
newTemplate = newTemplate.replace(escapeAnnotationRegex, annotation)
1515
newTemplate = newTemplate.replace(annotationAtStartRegex, SPECIAL_ANNOTATION[annotation](payload))
1616
}

0 commit comments

Comments
 (0)