Skip to main content
The 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:
  1. 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
  2. CometChatUrlFormatter (priority 100)
    • Detects URLs in text
    • Converts to clickable links
    • Adds security attributes (target="_blank", rel="noopener noreferrer")
These formatters are singleton instances, reused across all components for optimal performance.

Core Methods

getDefaultFormatters(): CometChatTextFormatter[]

Gets the configured default formatters. Returns: Array of text formatters in priority order Priority:
  1. Custom formatters (if set via setDefaultFormatters)
  2. Built-in defaults + additional formatters (if added via addFormatters)
  3. Built-in defaults only (mentions + URLs)
Example:

setDefaultFormatters(formatters: CometChatTextFormatter[]): void

Replaces the built-in default formatters with custom formatters. Parameters:
  • formatters: Array of custom formatters to use as defaults
Use Case: When you want complete control over which formatters are used globally. Example:
Note: Once custom formatters are set, 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
Use Case: When you want to keep the built-in formatters (mentions + URLs) and add custom ones. Example:
Note: If custom formatters have been set via 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 detection
  • alignment (optional): Message bubble alignment for direction CSS classes
Returns: Array of formatters configured with the provided context Context Configuration: When loggedInUser is provided:
  • Mentions formatter applies cometchat-mentions-you for self-mentions
  • Mentions formatter applies cometchat-mentions-other for other-user mentions
When alignment is provided:
  • MessageBubbleAlignment.left → adds cometchat-mentions-incoming class
  • MessageBubbleAlignment.right → adds cometchat-mentions-outgoing class
  • undefined → no direction classes (for composer, previews, conversation list)
Example 1: Message List (with alignment)
Example 2: Message Composer (without alignment)
Example 3: Conversation List (without alignment)

Usage Patterns

The simplest approach - let the service provide the default formatters.
When to use: Most components should use this pattern.

Pattern 2: Set Custom Formatters Globally

Replace the built-in formatters with your own custom set.
When to use: When you need complete control over formatter behavior globally.

Pattern 3: Extend Default Formatters

Keep the built-in formatters and add custom ones.
When to use: When you want to add functionality without losing the built-in formatters.

Pattern 4: Context-Aware Formatting

Configure formatters with logged-in user and message alignment.
When to use: In message lists where self-mention detection and direction classes are needed.

Pattern 5: Component-Specific Formatters

Override formatters for a specific component instance.
When to use: When a specific component needs different formatters than the global defaults.

Creating Custom Formatters

To create a custom formatter, extend CometChatTextFormatter:
Using the custom formatter:

Formatter Priority

Formatters execute in order of their priority property (lower number = earlier execution): Why priority matters:
Setting priority in custom formatters:

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 check shouldFormat() 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:
Angular’s hierarchical DI ensures the message list inside the wrapper resolves the local 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.

See Also