Overview
CometChatMarkdownRenderer converts a markdown string to sanitized HTML using a custom TypeScript parser — no third-party markdown library is required. It is used internally by CometChatAIAssistantMessageBubble (completed messages) and CometChatStreamMessageBubble (live streaming), but can be used standalone anywhere you need markdown rendering.
Key capabilities:
- Zero dependencies — custom
CometChatMarkdownParserclass; nomarked,remark, orhighlight.js - Streaming-safe — when
streamingistrue, incomplete syntax at the end of the text is rendered as plain text rather than dropped or throwing an error - XSS sanitization — all raw HTML in the input is escaped before markdown rules are applied; only the supported markdown constructs produce HTML elements
- Code block copy — each fenced code block gets a copy button; state is tracked per-block as an independent
WritableSignal<boolean> - Image click — clicking a rendered image emits
imageClickwith the image URL - Extensible parser —
CometChatMarkdownParseris exported from the public API so you can extend it with custom node types
Supported Markdown Constructs
| Syntax | Output |
|---|---|
# Heading through ###### Heading | <h1> – <h6> |
**bold** | <strong> |
_italic_ | <em> |
~~strikethrough~~ | <del> |
`inline code` | <code> |
```lang\ncode\n``` | <pre><code class="language-{lang}"> |
> blockquote | <blockquote> |
1. item | <ol><li> |
- item or * item | <ul><li> |
| Nested lists (up to 3 levels) | Nested <ul> / <ol> |
[text](url) | <a target="_blank" rel="noopener noreferrer"> |
 | Clickable <img> |
--- | <hr> |
Two trailing spaces or \n\n | <br> / paragraph |
GFM tables | col | col | | <table><thead><tbody> |
Basic Usage
Inputs
| Input | Type | Default | Description |
|---|---|---|---|
text | string | required | The markdown string to parse and render |
streaming | boolean | false | When true, tolerates incomplete syntax at the end of the string (for live streaming use cases) |
Outputs
| Output | Payload | Description |
|---|---|---|
imageClick | string | Emitted with the image URL when a rendered image is clicked; use this to open CometChatFullScreenViewer |
Streaming Mode
When rendering a live AI response, setstreaming to true. The parser will render any incomplete markdown at the end of the string as plain text rather than dropping it or throwing an error. This prevents visual glitches as tokens arrive incrementally.
Image Click Handling
WireimageClick to open the full-screen viewer:
Extending the Parser
CometChatMarkdownParser is exported from the public API. Extend it to add custom node types — for example, a chart block or a video embed — without modifying the renderer component.
CometChatMarkdownRenderer or by rendering the HTML yourself using the parser directly:
BEM Class Reference
All rendered elements carry BEM class names prefixed withcometchat-markdown-renderer. Use these to apply custom styles:
| Element | Class |
|---|---|
| Headings | cometchat-markdown-renderer__heading, cometchat-markdown-renderer__heading--1 … --6 |
| Paragraph | cometchat-markdown-renderer__paragraph |
| Bold | cometchat-markdown-renderer__bold |
| Italic | cometchat-markdown-renderer__italic |
| Strikethrough | cometchat-markdown-renderer__strikethrough |
| Inline code | cometchat-markdown-renderer__inline-code |
| Code block wrapper | cometchat-markdown-renderer__code-block |
| Copy button | cometchat-markdown-renderer__code-copy-btn |
| Blockquote | cometchat-markdown-renderer__blockquote |
| Ordered list | cometchat-markdown-renderer__ordered-list |
| Unordered list | cometchat-markdown-renderer__unordered-list |
| List item | cometchat-markdown-renderer__list-item |
| Link | cometchat-markdown-renderer__link |
| Image | cometchat-markdown-renderer__image |
| Horizontal rule | cometchat-markdown-renderer__hr |
| Table | cometchat-markdown-renderer__table |
| Table head | cometchat-markdown-renderer__table-head |
| Table header cell | cometchat-markdown-renderer__table-header-cell |
| Table body | cometchat-markdown-renderer__table-body |
| Table row | cometchat-markdown-renderer__table-row |
| Table cell | cometchat-markdown-renderer__table-cell |
Example: Custom Code Block Styling
Localization Keys
| Key | Default (en-us) |
|---|---|
ai_assistant_chat_code_copied | ”Copied!” |
CometChatLocalize.updateKeys():