mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-19 15:48:04 +01:00
12922 - Use absolute user URLs on production (#12968)
This commit is contained in:
@@ -11,14 +11,14 @@ describe('highlightMentions', () => {
|
|||||||
lean () {
|
lean () {
|
||||||
return this;
|
return this;
|
||||||
},
|
},
|
||||||
exec () {
|
async exec () {
|
||||||
return Promise.resolve([
|
return [
|
||||||
{ auth: { local: { username: 'user' } }, _id: '111' },
|
{ auth: { local: { username: 'user' } }, _id: '111' },
|
||||||
{ auth: { local: { username: 'user2' } }, _id: '222' },
|
{ auth: { local: { username: 'user2' } }, _id: '222' },
|
||||||
{ auth: { local: { username: 'user3' } }, _id: '333' },
|
{ auth: { local: { username: 'user3' } }, _id: '333' },
|
||||||
{ auth: { local: { username: 'user-dash' } }, _id: '444' },
|
{ auth: { local: { username: 'user-dash' } }, _id: '444' },
|
||||||
{ auth: { local: { username: 'user_underscore' } }, _id: '555' },
|
{ 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 () => {
|
describe('issues', () => {
|
||||||
const text = '[test]';
|
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 () => {
|
describe('base url', () => {
|
||||||
const text = '`[test]`';
|
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);
|
expect(result[0]).to.equal('[@user](https://habitica.com/profile/111)');
|
||||||
});
|
process.env.NODE_ENV = OLD_NODE_ENV;
|
||||||
|
});
|
||||||
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);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -144,6 +144,11 @@ function findTextBlocks (text) {
|
|||||||
return new TextBlocks(blocks);
|
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
|
* Replaces `@user` mentions by `[@user](/profile/{user-id})` markup to inject
|
||||||
* a link towards the user's profile page.
|
* 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'])
|
.select(['auth.local.username', '_id', 'preferences.pushNotifications', 'pushDevices', 'party', 'guilds'])
|
||||||
.lean()
|
.lean()
|
||||||
.exec();
|
.exec();
|
||||||
|
const baseUrl = determineBaseUrl();
|
||||||
members.forEach(member => {
|
members.forEach(member => {
|
||||||
const { username } = member.auth.local;
|
const { username } = member.auth.local;
|
||||||
const regex = new RegExp(`@${username}(?![\\-\\w])`, 'g');
|
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));
|
textBlocks.transformValidBlocks(blockText => blockText.replace(regex, replacement));
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user