Skip to main content
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.
Formatter output is always sanitized (via DOMPurify in the text bubble) before it is rendered — there is no way to bypass sanitization. Make sure your custom HTML is sanitizer-compatible (DOMPurify-safe). Wrapping formatted output in a <span> with a CSS class (e.g. "custom-hashtag") is only a styling and identification hook; it does NOT render the output as-is or bypass sanitization.

Steps

1. Import the base class

2. Extend it

3. Implement the regex pattern

Return the regex that matches your target pattern from getRegex():

4. Implement the format method

The format() method receives the raw text and returns formatted HTML:

5. Optionally implement shouldFormat

Control when the formatter is applied:

Example

A hashtag formatter used with cometchat-message-list and cometchat-message-composer.

Methods Reference

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.

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.