FormatterConfigService is a centralized Angular service for managing default text formatters across the CometChat UIKit. It provides a single source of truth for formatter configuration, allowing you to set formatters once and have them automatically applied across all text-displaying components.
Overview
The service manages text formatters that transform raw text into formatted HTML with support for:- Mentions (@user) with self-mention detection
- URLs with automatic link creation
- Custom formatters (hashtags, emails, etc.)
- Context-aware formatting (logged-in user, message alignment)
- Formatter priority ordering
- Performance optimization through instance reuse
Installation
The service is provided at root level and automatically available throughout your application:Default Behavior
When no custom formatters are configured, the service provides two built-in formatters:-
CometChatMentionsFormatter (priority 20)
- Detects mentions in format
<@uid:{uid}>or<@all:{label}> - Converts to HTML spans with CSS classes
- Applies self-mention detection
- Adds direction classes for message alignment
- Detects mentions in format
-
CometChatUrlFormatter (priority 100)
- Detects URLs in text
- Converts to clickable links
- Adds security attributes (
target="_blank",rel="noopener noreferrer")
Core Methods
getDefaultFormatters(): CometChatTextFormatter[]
Gets the configured default formatters.
Returns: Array of text formatters in priority order
Priority:
- Custom formatters (if set via
setDefaultFormatters) - Built-in defaults + additional formatters (if added via
addFormatters) - Built-in defaults only (mentions + URLs)
setDefaultFormatters(formatters: CometChatTextFormatter[]): void
Replaces the built-in default formatters with custom formatters.
Parameters:
formatters: Array of custom formatters to use as defaults
addFormatters() has no effect. To add to custom formatters, include them in the array passed to setDefaultFormatters().
addFormatters(formatters: CometChatTextFormatter[]): void
Adds additional formatters to the built-in defaults without replacing them.
Parameters:
formatters: Array of formatters to add to the default set
setDefaultFormatters(), this method has no effect.
resetToDefaults(): void
Resets to the built-in default formatters, clearing any custom or additional formatters.
Use Case: When you want to restore the original formatter configuration.
Example:
getFormattersWithContext(loggedInUser?, alignment?): CometChatTextFormatter[]
Gets formatters configured with context for self-mention detection and direction CSS classes.
Parameters:
loggedInUser(optional): The logged-in user for self-mention detectionalignment(optional): Message bubble alignment for direction CSS classes
loggedInUser is provided:
- Mentions formatter applies
cometchat-mentions-youfor self-mentions - Mentions formatter applies
cometchat-mentions-otherfor other-user mentions
alignment is provided:
MessageBubbleAlignment.left→ addscometchat-mentions-incomingclassMessageBubbleAlignment.right→ addscometchat-mentions-outgoingclassundefined→ no direction classes (for composer, previews, conversation list)
Usage Patterns
Pattern 1: Use Default Formatters (Recommended)
The simplest approach - let the service provide the default formatters.Pattern 2: Set Custom Formatters Globally
Replace the built-in formatters with your own custom set.Pattern 3: Extend Default Formatters
Keep the built-in formatters and add custom ones.Pattern 4: Context-Aware Formatting
Configure formatters with logged-in user and message alignment.Pattern 5: Component-Specific Formatters
Override formatters for a specific component instance.Creating Custom Formatters
To create a custom formatter, extendCometChatTextFormatter:
Formatter Priority
Formatters execute in order of theirpriority property (lower number = earlier execution):
| Formatter | Priority | Execution Order |
|---|---|---|
| Mentions | 20 | First |
| Emoji | 30 | Second |
| Custom (example) | 50 | Third |
| URLs | 100 | Last |
Performance Optimization
The service optimizes performance through:1. Singleton Instances
Formatters are created once and reused across all components:2. Lazy Initialization
Built-in formatters are created only when first accessed:3. Conditional Execution
Formatters checkshouldFormat() before applying regex:
Integration with Components
The service integrates seamlessly with all text-displaying components:Text Bubble
Message List
Conversations
Best Practices
1. Configure Once, Use Everywhere
Set formatters globally in app initialization:2. Use Context-Aware Formatting
Always provide logged-in user for self-mention detection:3. Respect Formatter Priority
Set appropriate priority values for custom formatters:4. Implement shouldFormat()
Optimize performance with quick checks:5. Handle Edge Cases
Ensure formatters handle empty or invalid input:Common Patterns
Pattern: Feature Flag for Formatters
Enable/disable formatters based on feature flags:Pattern: User Preference for Formatters
Allow users to customize formatter behavior:Pattern: Formatter Testing
Test custom formatters in isolation:Troubleshooting
Formatters Not Applied
Problem: Text is not being formatted. Solution: Check that formatters are configured:Self-Mentions Not Detected
Problem: All mentions show as “other” mentions. Solution: Provide logged-in user context:Custom Formatters Not Working
Problem: Custom formatters added but not executing. Solution: Check if custom formatters were set (which overrides additions):Formatters Execute in Wrong Order
Problem: Formatters execute in unexpected order. Solution: Check priority values:Scoping for Multiple Instances
FormatterConfigService is provided at the root level (providedIn: 'root'), so all components share the same formatter configuration by default. If you need different formatters for different message lists (e.g., a main chat with full formatting vs. a thread panel with minimal formatting), scope the service to a wrapper component:
FormatterConfigService instance. The main chat panel continues to use the root singleton.
See CometChatMessageList — Multiple Message Lists with Different Configurations for a complete multi-panel example.