Chatwoot/app/javascript/shared/helpers/MessageFormatter.js
Pranav Raj S c6feea9f6d [Enhancement] Format messages in widget (#268)
* Use message formatter in widget

* Rename the variable
2019-11-24 00:29:55 +05:30

27 lines
538 B
JavaScript

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;