AI Integration Quick Reference
AI Integration Quick Reference
#word patterns and renders them as highlighted, styled tokens. The formatter works across all text surfaces: the message composer (live as you type), message bubbles, conversation subtitle (last message preview), and the message edit view.
How It Works
- User types
#angularin the composer. - On space or enter, the formatter wraps the hashtag in a styled
<span>. - When the message is sent, the raw text (
#angular) is stored in the message body. - When the message is rendered in the message list, conversation list, or edit view, the formatter re-applies the highlight.
textFormatters input on cometchat-message-list, cometchat-message-composer, and cometchat-conversations.
Prerequisites
- Angular UIKit installed and initialized (Integration Guide)
- A logged-in CometChat user
- Basic understanding of the Custom Text Formatter pattern
Step 1: Create the Formatter Class
Create a filehashtag-formatter.ts in your project:
priority = 15places it after the URL formatter (10) but before mentions (20), so URLs inside hashtags aren’t broken.- The regex
\B#(\w{1,50})\bmatches#preceded by a non-word boundary, followed by 1–50 word characters. This avoids matching#in URLs or hex colors. - The
<span>carries thecometchat-hashtagclass and no inline style. Its color comes from the CSS rule in Step 4, which usesvar(--cometchat-primary-color)so it respects the active theme.
Step 2: Use in Message List and Composer
Pass the formatter to both the message list (for rendering) and the composer (for live preview and edit view):textFormatters input accepts an array, and passing it replaces the built-in formatters on that component. Include the built-ins you still want in the same array, or markdown, links, and mentions stop rendering. See Custom Text Formatter for the full list.
Step 3: Use in Conversations (Last Message Preview)
To highlight hashtags in the conversation list’s last message subtitle, pass the same formatter tocometchat-conversations:
#angular in the last message preview renders with the same highlight styling.
Step 4: Style the Hashtag
Style the hashtag with a CSS class, not an inlinestyle. Angular sanitizes HTML bound through [innerHTML] and removes style attributes, so an inline color would never reach the message bubble.
styles.css or styles.scss. The span renders inside the UI Kit’s components, so a component’s encapsulated styles will not reach it.
Step 5: Handle Hashtag Clicks (Optional)
To make hashtags interactive (e.g., filter messages by tag), extend the formatter to emit click events:.cometchat-hashtag elements. Take the tag from the span’s text, not a data- attribute: Angular’s sanitizer strips data-* attributes along with style, so they never reach the bubble.
Complete Example
A full two-panel layout with hashtag formatting across all surfaces:Where Hashtags Appear
The raw message text stored on the server is always plain text (e.g.,
Check out #angular). Formatting is applied at render time by the formatter pipeline. This means hashtag highlighting works retroactively on older messages too.Next Steps
Custom Text Formatter
Learn the full formatter API and lifecycle.
Mentions Formatter
Add @mentions with suggestion lists.
Message Composer
Customize the message input component.
All Guides
Browse all feature and formatter guides.