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.
Capability
Description
Text formatting
Auto-format text based on regex patterns and styles
Custom styles
Set colors, fonts, and backgrounds for matched text
Dynamic replacement
Regex-based find-and-replace in user input
Input integration
Real-time monitoring of the composer input field
Key event callbacks
Hooks for keyUp and keyDown events
Formatter output is always sanitized (via DOMPurify in the text bubble) before it is rendered — there is no way to bypass sanitization. Make sure your custom HTML is sanitizer-compatible (DOMPurify-safe). Wrapping formatted output in a <span> with a CSS class (e.g. "custom-hashtag") is only a styling and identification hook; it does NOT render the output as-is or bypass sanitization.
Execution order in the pipeline (lower = earlier, default 100)
getRegex()
abstract method
Returns the regex pattern for detecting formattable content
format(text)
abstract method
Applies formatting transformations and returns formatted text
getFormattedText()
method
Returns the stored formatted text after format() is called
getOriginalText()
method
Returns the original text before formatting
getMetadata()
method
Returns metadata extracted during formatting
reset()
method
Clears original text, formatted text, and metadata
shouldFormat(text, message?)
method
Returns whether this formatter should process the given text (default: true)
Formatters are applied in priority order (lower priority number = earlier in pipeline). The built-in URL formatter uses priority 10, mentions uses 20. Choose your custom formatter’s priority accordingly.