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.

Giving Users a Way to Author It

A formatter has two halves. Everything above is the rendering half — turning a marker in the raw message text into styled output wherever the message is displayed. The other half is authoring: giving users a way to produce that marker in the first place. The composer’s toolbarTrailingView is where that control goes. It renders at the trailing end of the rich-text formatting toolbar, after the built-in groups and an automatically inserted separator, and its template context carries the composer itself so your button can write into the editor.

1. The formatter

Say the marker is {color=VALUE}…{/color}. The formatter turns it into a colored <span>: File: src/app/formatters/color-formatter.ts
format() must store originalText, set formattedText, and return the formatted string — the pipeline reads those fields. Keep it fast: it runs on every text message render.

2. The toolbar button

Put the button in toolbarTrailingView and let it write the marker through the composer handle the template context provides. File: src/app/chat/chat.component.html
(mousedown)="$event.preventDefault()" is the detail that matters. Without it, clicking the button moves focus out of the editor and clears the selection before your handler runs.
The toolbar — and therefore the trailing view — renders only while the rich-text editor and its toolbar are enabled, so pass [enableRichText]="true" with [hideRichTextToolbar]="false".

3. Register it on every surface

The marker only becomes color where a surface actually runs the formatter. Register the same formatter everywhere the message can appear:
A formatter applies only where it is registered. Add textFormatters to the composer but not the message list and the author sees color while readers see raw {color=…} text. To set one list app-wide instead of per component, use textFormatters in Global Configuration — every component falls back to it when its own input is unset.

How it round-trips

The marker is plain text on the message, so it survives storage and delivery untouched. Each display surface turns it into color independently, through the formatter you registered there.

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.