AI Integration Quick Reference
AI Integration Quick Reference
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.
Steps
1. Import the base class
2. Extend it
3. Implement the regex pattern
Return the regex that matches your target pattern fromgetRegex():
4. Implement the format method
Theformat() method receives the raw text and returns formatted HTML:
5. Optionally implement shouldFormat
Control when the formatter is applied:Example
A hashtag formatter used withcometchat-message-list and cometchat-message-composer.

- HashTagTextFormatter.ts
- Component Usage
Methods Reference
Override Methods
- format
- getRegex
- shouldFormat
- reset
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’stoolbarTrailingView 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 intoolbarTrailingView 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.[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: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.