Built-in Formatters
| Formatter | Priority | Detects | Output |
|---|---|---|---|
CometChatMarkdownFormatter | 10 | **bold**, _italic_, `code`, > quote, lists, links | HTML tags (<b>, <i>, <code>, etc.) |
CometChatMentionsFormatter | 50 | <@uid:xxx> tokens | Styled @DisplayName chips |
CometChatUrlFormatter | 100 | https://..., www. | Clickable <a> links |
The Formatter Interface
All formatters extend the abstractCometChatTextFormatter class:
Creating a Custom Formatter
Here’s a hashtag formatter that wraps#word patterns in a styled span:
src/formatters/HashtagFormatter.ts
Registering Custom Formatters
Custom formatters are registered by creating a custom text plugin that provides them:src/plugins/CustomTextPlugin.ts
plugins prop:
Formatter Details
CometChatMarkdownFormatter
Converts markdown syntax to HTML. Runs first (priority 10) so subsequent formatters operate on HTML output.**bold**→<b>bold</b>_italic_→<i>italic</i>__underline__or++underline++→<u>underline</u>~~strikethrough~~→<s>strikethrough</s>`inline code`→<code>inline code</code>```code block```→<pre><code>code block</code></pre>> blockquote→<blockquote>blockquote</blockquote>[text](url)→<a href="url">text</a>1. item→ ordered list;• item/- item→ unordered list
CometChatMentionsFormatter
Resolves SDK mention tokens (<@uid:xxx> and <@all:label>) into styled mention chips. Requires the mentioned-users list from the message to resolve UIDs to display names.
CometChatUrlFormatter
Detects bare URLs (https://... and www.) and wraps them in clickable <a> tags with target="_blank" and rel="noopener noreferrer". Protects existing markdown links and <a> tags from double-processing.
Tips
- Priority matters — markdown must run before mentions/URLs so it doesn’t break HTML tags
- Protect code blocks — formatters should skip content inside
<code>and<pre>tags - Keep it fast — formatters run on every text message render; avoid expensive operations
- Use
shouldFormat()— override to skip formatting for specific messages - Store metadata — use
this.metadatato expose extracted data (URLs, hashtags, mentions) to consumers