Chatwoot/app/javascript/shared/helpers/MessageFormatter.js
Sojan Jose a18d54b706
Chore: Cleanup attachment handling for Facebook & Whatsapp (#1051)
* Chore: Enable file upload for facebook messenger
* Chore: Fix attachments
* Chore: Fix Specs
* Fix ReplyBox file attachment logic
* Set default value for message

Co-authored-by: Pranav Raj Sreepuram <pranavrajs@gmail.com>
2020-07-17 00:32:32 +05:30

30 lines
659 B
JavaScript

import { escapeHtml } from './HTMLSanitizer';
class MessageFormatter {
constructor(message) {
this.message = escapeHtml(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 rel="noreferrer noopener nofollow" href="${url}" class="link" target="_blank">${url}</a>`
);
}
get formattedMessage() {
return this.formatMessage();
}
}
export default MessageFormatter;