diff --git a/app/javascript/dashboard/components/widgets/conversation/Message.vue b/app/javascript/dashboard/components/widgets/conversation/Message.vue index 59283b4c1..818eef088 100644 --- a/app/javascript/dashboard/components/widgets/conversation/Message.vue +++ b/app/javascript/dashboard/components/widgets/conversation/Message.vue @@ -42,6 +42,7 @@ diff --git a/app/javascript/shared/helpers/MessageFormatter.js b/app/javascript/shared/helpers/MessageFormatter.js new file mode 100644 index 000000000..32aeef48f --- /dev/null +++ b/app/javascript/shared/helpers/MessageFormatter.js @@ -0,0 +1,27 @@ +class MessageFormatter { + constructor(message) { + this.message = message || ''; + } + + formatMessage() { + const linkifiedMessage = this.linkify(); + return linkifiedMessage.replace(/\n/g, '
'); + } + + linkify() { + if (!this.message) { + return ''; + } + const urlRegex = /(https?:\/\/[^\s]+)/g; + return this.message.replace( + urlRegex, + url => `${url}` + ); + } + + get formattedMessage() { + return this.formatMessage(); + } +} + +export default MessageFormatter; diff --git a/app/javascript/shared/helpers/specs/MessageFormatter.spec.js b/app/javascript/shared/helpers/specs/MessageFormatter.spec.js new file mode 100644 index 000000000..f6ffb34f8 --- /dev/null +++ b/app/javascript/shared/helpers/specs/MessageFormatter.spec.js @@ -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
See more at https://www.chatwoot.com' + ); + }); + }); +}); diff --git a/app/javascript/shared/mixins/messageFormatterMixin.js b/app/javascript/shared/mixins/messageFormatterMixin.js new file mode 100644 index 000000000..4c6cb0c1d --- /dev/null +++ b/app/javascript/shared/mixins/messageFormatterMixin.js @@ -0,0 +1,10 @@ +import MessageFormatter from '../helpers/MessageFormatter'; + +export default { + methods: { + formatMessage(message) { + const messageFormatter = new MessageFormatter(message); + return messageFormatter.formattedMessage; + }, + }, +}; diff --git a/app/javascript/widget/components/AgentMessageBubble.vue b/app/javascript/widget/components/AgentMessageBubble.vue index 6d270dc85..54319761a 100755 --- a/app/javascript/widget/components/AgentMessageBubble.vue +++ b/app/javascript/widget/components/AgentMessageBubble.vue @@ -1,12 +1,13 @@