Skip to content

Commit

Permalink
Do not replace multiple @ when using author annotation
Browse files Browse the repository at this point in the history
  • Loading branch information
mesaugat committed Feb 27, 2024
1 parent 07698d3 commit 8556335
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,22 @@ describe('searchAndReplaceSpecialAnnotations', () => {
}
expect(searchAndReplaceSpecialAnnotations('this is @author', payload)).toBe('this is creator')
})

test('@@author is replaced by @payload.user.login', () => {
const payload = {
user: {
login: 'creator'
}
}
expect(searchAndReplaceSpecialAnnotations('this is @@author', payload)).toBe('this is @creator')
})

test('replaces annotation anywhere in the text', () => {
const payload = {
user: {
login: 'creator'
}
}
expect(searchAndReplaceSpecialAnnotations('this is something@author', payload)).toBe('this is somethingcreator')
})
})
4 changes: 2 additions & 2 deletions lib/actions/lib/searchAndReplaceSpecialAnnotation.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ const searchAndReplaceSpecialAnnotations = (template, payload) => {
let newTemplate = template

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

newTemplate = newTemplate.replace(specialAnnotationRegex, ` ${SPECIAL_ANNOTATION[annotation](payload)}`)
newTemplate = newTemplate.replace(specialAnnotationRegex, `${SPECIAL_ANNOTATION[annotation](payload)}`)
newTemplate = newTemplate.replace(escapeAnnotationRegex, annotation)
newTemplate = newTemplate.replace(annotationAtStartRegex, SPECIAL_ANNOTATION[annotation](payload))
}
Expand Down

0 comments on commit 8556335

Please sign in to comment.