[Enhancement] Format messages in widget (#268)

* Use message formatter in widget

* Rename the variable
This commit is contained in:
Pranav Raj S
2019-11-24 00:29:55 +05:30
committed by Sony Mathew
parent c96e2c334c
commit c6feea9f6d
6 changed files with 66 additions and 17 deletions

View File

@@ -0,0 +1,27 @@
class MessageFormatter {
constructor(message) {
this.message = message || '';
}
formatMessage() {
const linkifiedMessage = this.linkify();
return linkifiedMessage.replace(/\n/g, '<br>');
}
linkify() {
if (!this.message) {
return '';
}
const urlRegex = /(https?:\/\/[^\s]+)/g;
return this.message.replace(
urlRegex,
url => `<a href="${url}" target="_blank">${url}</a>`
);
}
get formattedMessage() {
return this.formatMessage();
}
}
export default MessageFormatter;

View File

@@ -0,0 +1,13 @@
import MessageFormatter from '../MessageFormatter';
describe('#MessageFormatter', () => {
describe('content with links', () => {
it('should format correctly', () => {
const message =
'Chatwoot is an opensource tool\nSee more at https://www.chatwoot.com';
expect(new MessageFormatter(message).formattedMessage).toEqual(
'Chatwoot is an opensource tool<br>See more at <a href="https://www.chatwoot.com" target="_blank">https://www.chatwoot.com</a>'
);
});
});
});

View File

@@ -0,0 +1,10 @@
import MessageFormatter from '../helpers/MessageFormatter';
export default {
methods: {
formatMessage(message) {
const messageFormatter = new MessageFormatter(message);
return messageFormatter.formattedMessage;
},
},
};