Skip to main content

Introduction

CometChatUrlTextFormatter is a specialized subclass of CometChatTextFormatter designed to automatically detect URLs in text messages and convert them into clickable links, allowing users to navigate to the web addresses effortlessly within your CometChat application.

Prerequisites

To implement the URL Text Formatter in your chat application, make sure you have:
  • A valid CometChat account and application set up.
  • Basic knowledge of JavaScript and familiarity with DOM manipulation.
  • An understanding of regular expressions for pattern matching.

Implementation

CometChatUrlTextFormatter utilizes regular expressions to identify URLs and applies styles to make them visually distinct as clickable links. Here’s an example of how to extend the CometChatTextFormatter to create a URL text formatter:

Customizing Style and Behavior

When implementing the CometChatUrlTextFormatter, you can customize the appearance of links and the behavior when clicked:
  • Modify the onRegexMatch method to wrap detected URLs in a span element with custom classes for styling.
  • In registerEventListeners, define the onUrlClick method to handle opening URLs, tracking analytics, or other custom behavior upon click.
Apply CSS to style your clickable links, for example:

Handling Click Events

Implement the onUrlClick method within your formatter class to define custom logic for when a link is clicked:

UrlFormatterStyle

Styling properties for CometChatUrlTextFormatter component.