Skip to main content

Documentation Index

Fetch the complete documentation index at: https://www.cometchat.com/docs/llms.txt

Use this file to discover all available pages before exploring further.

FieldValue
Package@cometchat/chat-uikit-angular
Key classCometChatTextFormatter (abstract base class for custom formatters)
Required setupCometChatUIKit.init(uiKitSettings) then CometChatUIKit.login("UID")
PurposeExtend to create custom inline text patterns with regex, styling, and callbacks
FeaturesText formatting, customizable styles, dynamic text replacement, input field integration, key event callbacks
RelatedShortCut Formatter | Mentions Formatter | All Guides
CometChatTextFormatter is an abstract class for formatting text in the message composer and message bubbles. Extend it to build custom formatters — hashtags, keywords, or any regex-based pattern.
CapabilityDescription
Text formattingAuto-format text based on regex patterns and styles
Custom stylesSet colors, fonts, and backgrounds for matched text
Dynamic replacementRegex-based find-and-replace in user input
Input integrationReal-time monitoring of the composer input field
Key event callbacksHooks for keyUp and keyDown events
Always wrap formatted output in a <span> with a unique CSS class (e.g. "custom-hashtag"). This tells the UI Kit to render it as-is instead of sanitizing it.

Steps

1. Import the base class

import { CometChatTextFormatter } from "@cometchat/chat-uikit-angular";

2. Extend it

class HashTagTextFormatter extends CometChatTextFormatter {
  readonly id = "hashtag-formatter";
  override priority = 15;

  getRegex(): RegExp {
    return /\B#(\w+)\b/g;
  }

  format(text: string): string {
    // Apply formatting logic
    return text;
  }
}

3. Implement the regex pattern

Return the regex that matches your target pattern from getRegex():
getRegex(): RegExp {
  return /\B#(\w+)\b/g;
}

4. Implement the format method

The format() method receives the raw text and returns formatted HTML:
format(text: string): string {
  if (!text) {
    this.originalText = "";
    this.formattedText = "";
    return "";
  }

  this.originalText = text;
  this.formattedText = text.replace(
    this.getRegex(),
    '<span class="custom-hashtag" style="color: #30b3ff;">#$1</span>'
  );
  return this.formattedText;
}

5. Optionally implement shouldFormat

Control when the formatter is applied:
shouldFormat(text: string, message?: CometChat.BaseMessage): boolean {
  return this.getRegex().test(text);
}

Example

A hashtag formatter used with cometchat-message-list and cometchat-message-composer.
import { CometChatTextFormatter } from "@cometchat/chat-uikit-angular";

export class HashTagTextFormatter extends CometChatTextFormatter {
  readonly id = "hashtag-text-formatter";
  override priority = 15;

  private hashtags: string[] = [];

  getRegex(): RegExp {
    return /\B#(\w+)\b/g;
  }

  format(text: string): string {
    if (!text) {
      this.originalText = "";
      this.formattedText = "";
      this.hashtags = [];
      this.metadata = { hashtags: this.hashtags };
      return "";
    }

    this.originalText = text;
    this.hashtags = [];

    this.formattedText = text.replace(this.getRegex(), (match, tag) => {
      this.hashtags.push(`#${tag}`);
      return `<span class="custom-hashtag" style="color: #5dff05;">#${tag}</span>`;
    });

    this.metadata = { hashtags: this.hashtags };
    return this.formattedText;
  }

  getHashtags(): string[] {
    return [...this.hashtags];
  }

  override reset(): void {
    super.reset();
    this.hashtags = [];
  }
}

Methods Reference

FieldTypeDescription
idabstract readonly stringUnique identifier for the formatter instance
prioritynumberExecution order in the pipeline (lower = earlier, default 100)
getRegex()abstract methodReturns the regex pattern for detecting formattable content
format(text)abstract methodApplies formatting transformations and returns formatted text
getFormattedText()methodReturns the stored formatted text after format() is called
getOriginalText()methodReturns the original text before formatting
getMetadata()methodReturns metadata extracted during formatting
reset()methodClears original text, formatted text, and metadata
shouldFormat(text, message?)methodReturns whether this formatter should process the given text (default: true)
Formatters are applied in priority order (lower priority number = earlier in pipeline). The built-in URL formatter uses priority 10, mentions uses 20. Choose your custom formatter’s priority accordingly.

Override Methods

The core method that applies formatting. Store original text, apply transformations, store metadata, and return the result.
format(text: string): string {
  if (!text) {
    this.originalText = "";
    this.formattedText = "";
    return "";
  }
  this.originalText = text;
  this.formattedText = this.customLogicToFormatText(text);
  return this.formattedText;
}

Next Steps

Mentions Formatter

Add @mentions with styled tokens.

Message Composer

Customize the message input component.

All Guides

Browse all feature and formatter guides.

ShortCut Formatter

Implement text expansion shortcuts.