Skip to main content
This guide walks through building a hashtag formatter that detects #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

  1. User types #angular in the composer.
  2. On space or enter, the formatter wraps the hashtag in a styled <span>.
  3. When the message is sent, the raw text (#angular) is stored in the message body.
  4. When the message is rendered in the message list, conversation list, or edit view, the formatter re-applies the highlight.
The formatter plugs into the textFormatters input on cometchat-message-list, cometchat-message-composer, and cometchat-conversations.

Prerequisites


Step 1: Create the Formatter Class

Create a file hashtag-formatter.ts in your project:
Key points:
  • priority = 15 places it after the URL formatter (10) but before mentions (20), so URLs inside hashtags aren’t broken.
  • The regex \B#(\w{1,50})\b matches # preceded by a non-word boundary, followed by 1–50 word characters. This avoids matching # in URLs or hex colors.
  • The <span> carries the cometchat-hashtag class and no inline style. Its color comes from the CSS rule in Step 4, which uses var(--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):
The 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 to cometchat-conversations:
This ensures #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 inline style. Angular sanitizes HTML bound through [innerHTML] and removes style attributes, so an inline color would never reach the message bubble.
Add this to your global 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:
Then in your component, listen for clicks on .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.