diff --git a/test/api/unit/libs/highlightMentions.test.js b/test/api/unit/libs/highlightMentions.test.js index a450cf9640..30fb34c72c 100644 --- a/test/api/unit/libs/highlightMentions.test.js +++ b/test/api/unit/libs/highlightMentions.test.js @@ -11,14 +11,14 @@ describe('highlightMentions', () => { lean () { return this; }, - exec () { - return Promise.resolve([ + async exec () { + return [ { auth: { local: { username: 'user' } }, _id: '111' }, { auth: { local: { username: 'user2' } }, _id: '222' }, { auth: { local: { username: 'user3' } }, _id: '333' }, { auth: { local: { username: 'user-dash' } }, _id: '444' }, { auth: { local: { username: 'user_underscore' } }, _id: '555' }, - ]); + ]; }, }; @@ -168,35 +168,50 @@ describe('highlightMentions', () => { }); }); - it('github issue 12118, method crashes when square brackets are used', async () => { - const text = '[test]'; + describe('issues', () => { + it('github issue 12118, method crashes when square brackets are used', async () => { + const text = '[test]'; - const result = await highlightMentions(text); + const result = await highlightMentions(text); - expect(result[0]).to.equal(text); + expect(result[0]).to.equal(text); + }); + + it('github issue 12138, method crashes when regex chars are used in code block', async () => { + const text = '`[test]`'; + + const result = await highlightMentions(text); + + expect(result[0]).to.equal(text); + }); + + it('github issue 12586, method crashes when empty link is used', async () => { + const text = '[]()'; + + const result = await highlightMentions(text); + + expect(result[0]).to.equal(text); + }); + + it('github issue 12586, method crashes when link without title is used', async () => { + const text = '[](www.google.com)'; + + const result = await highlightMentions(text); + + expect(result[0]).to.equal(text); + }); }); - it('github issue 12138, method crashes when regex chars are used in code block', async () => { - const text = '`[test]`'; + describe('base url', () => { + it('should prefix habitica.com if it is production', async () => { + const OLD_NODE_ENV = process.env.NODE_ENV; + process.env.NODE_ENV = 'production'; + const text = '@user'; - const result = await highlightMentions(text); + const result = await highlightMentions(text); - expect(result[0]).to.equal(text); - }); - - it('github issue 12586, method crashes when empty link is used', async () => { - const text = '[]()'; - - const result = await highlightMentions(text); - - expect(result[0]).to.equal(text); - }); - - it('github issue 12586, method crashes when link without title is used', async () => { - const text = '[](www.google.com)'; - - const result = await highlightMentions(text); - - expect(result[0]).to.equal(text); + expect(result[0]).to.equal('[@user](https://habitica.com/profile/111)'); + process.env.NODE_ENV = OLD_NODE_ENV; + }); }); }); diff --git a/website/server/libs/highlightMentions.js b/website/server/libs/highlightMentions.js index 39f6a1fcc5..ccb074590e 100644 --- a/website/server/libs/highlightMentions.js +++ b/website/server/libs/highlightMentions.js @@ -144,6 +144,11 @@ function findTextBlocks (text) { return new TextBlocks(blocks); } +function determineBaseUrl () { + // eslint-disable-next-line no-process-env + return process.env.NODE_ENV === 'production' ? 'https://habitica.com' : ''; +} + /** * Replaces `@user` mentions by `[@user](/profile/{user-id})` markup to inject * a link towards the user's profile page. @@ -164,10 +169,11 @@ export default async function highlightMentions (text) { .select(['auth.local.username', '_id', 'preferences.pushNotifications', 'pushDevices', 'party', 'guilds']) .lean() .exec(); + const baseUrl = determineBaseUrl(); members.forEach(member => { const { username } = member.auth.local; const regex = new RegExp(`@${username}(?![\\-\\w])`, 'g'); - const replacement = `[@${username}](/profile/${member._id})`; + const replacement = `[@${username}](${baseUrl}/profile/${member._id})`; textBlocks.transformValidBlocks(blockText => blockText.replace(regex, replacement)); });