diff --git a/test/api/unit/libs/highlightMentions.test.js b/test/api/unit/libs/highlightMentions.test.js index 492bed033e..bd3955162e 100644 --- a/test/api/unit/libs/highlightMentions.test.js +++ b/test/api/unit/libs/highlightMentions.test.js @@ -149,6 +149,14 @@ describe('highlightMentions', () => { expect(result[0]).to.equal('[@user](/profile/111) `@user`'); }); + + it('matches a link in between two the same links', async () => { + const text = '[here](http://habitica.wikia.com/wiki/The_Keep:Pirate_Cove/FAQ)\n@user\n[hier](http://habitica.wikia.com/wiki/The_Keep:Pirate_Cove/FAQ)'; + + const result = await highlightMentions(text); + + expect(result[0]).to.equal('[here](http://habitica.wikia.com/wiki/The_Keep:Pirate_Cove/FAQ)\n[@user](/profile/111)\n[hier](http://habitica.wikia.com/wiki/The_Keep:Pirate_Cove/FAQ)'); + }); }); it('github issue 12118, method crashes when square brackets are used', async () => { diff --git a/website/server/libs/highlightMentions.js b/website/server/libs/highlightMentions.js index 900a9547a0..e6f2ac5ad8 100644 --- a/website/server/libs/highlightMentions.js +++ b/website/server/libs/highlightMentions.js @@ -86,7 +86,7 @@ function toSourceMapRegex (token) { } else if (type === 'link_open') { const texts = token.textContents.map(escapeRegExp); regexStr = markup === 'linkify' || markup === 'autolink' ? texts[0] - : `\\[.*${texts.join('.*')}.*\\]\\([^)]+\\)`; + : `\\[[^\\]]*${texts.join('[^\\]]*')}[^\\]]*\\]\\([^)]+\\)`; } else { throw new Error(`No source mapping regex defined for ignore blocks of type ${type}`); } @@ -111,6 +111,11 @@ function findTextBlocks (text) { const targetText = text.substr(index); const match = targetText.match(regex); + if (!match) { + // Should not happen, but insert to handle bugs gracefully + return; + } + if (match.index) { blocks.push({ text: targetText.substr(0, match.index), ignore: false }); }