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> 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. The UIKit merges your custom formatters with the built-in ones (URL, mentions, emoji, markdown) and applies them in priority order.

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

The inline style attribute handles the base color, but you can add a global CSS rule for more control:
Add this to your global styles.css or styles.scss.

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:

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.