logoAnt Design X

DesignDevelopmentComponentsX MarkdownX SDKPlayground
  • Introduction
  • Code Examples
  • Themes
  • Streaming Processing
    • Syntax Processing
    • Animation Effects
  • Components
    • Overview
    • Think
    • DataChart
    • Custom Component
  • Plugins
    • Overview
    • Latex
    • HighlightCode
    • Mermaid
    • CustomPlugins
    • Theme & Locale

Syntax Processing

Resources

Ant Design
Ant Design Charts
Ant Design Pro
Pro Components
Ant Design Mobile
Ant Design Mini
Ant Design Web3
Ant Design Landing-Landing Templates
Scaffolds-Scaffold Market
Umi-React Application Framework
dumi-Component doc generator
qiankun-Micro-Frontends Framework
Ant Motion-Motion Solution
China Mirror 🇨🇳

Community

Awesome Ant Design
Medium
Twitter
yuque logoAnt Design in YuQue
Ant Design in Zhihu
Experience Cloud Blog
seeconf logoSEE Conf-Experience Tech Conference

Help

GitHub
Change Log
FAQ
Bug Report
Issues
Discussions
StackOverflow
SegmentFault

Ant XTech logoMore Products

yuque logoYuQue-Document Collaboration Platform
AntV logoAntV-Data Visualization
Egg logoEgg-Enterprise Node.js Framework
Kitchen logoKitchen-Sketch Toolkit
Galacean logoGalacean-Interactive Graphics Solution
xtech logoAnt Financial Experience Tech
Theme Editor
Made with ❤ by
Ant Group and Ant Design Community

Streaming syntax processing mechanism is designed for real-time rendering scenarios, capable of intelligently handling incomplete Markdown syntax structures to avoid rendering anomalies caused by syntax fragments.

Core Issues

During streaming transmission, Markdown syntax may be in an incomplete state:

markdown
// Incomplete link during transmission Click to visit [example website](https://example // Incomplete image syntax ![product image](https://cdn.example.com/images/produc

Rendering Anomaly Risks

Incomplete syntax structures may lead to:

  • Links unable to jump correctly
  • Image loading failures
  • Format markers displaying directly in content

Feature Demo

Configuration Guide

streaming Configuration

ParameterDescriptionTypeDefault
hasNextChunkWhether there is more streaming databooleanfalse
incompleteMarkdownComponentMapMapping configuration for converting incomplete Markdown formats to custom loading components, used to provide custom loading components for unclosed links and images during streaming rendering{ link?: string; image?: string }{ link: 'incomplete-link', image: 'incomplete-image' }

Usage Example

tsx
import { XMarkdown } from '@ant-design/x-markdown';
const App = () => {
return (
<XMarkdown
content="# Streaming Rendering Example\n\nVisit [Ant Design](https://ant.design) for design resources.\n\n![Logo](https://gw.alipayobjects.com/zos/rmsportal/KDpgvguMpGfqaHPjicRK.svg)"
streaming={{
hasNextChunk: true,
incompleteMarkdownComponentMap: {
link: 'custom-link-loading',
image: 'custom-image-loading',
},
}}
/>
);
};

Supported Syntax Types

Streaming syntax processing supports integrity checks for the following Markdown syntax:

Syntax TypeFormat ExampleProcessing Mechanism
Links[text](url)Detects unclosed link markers like [text](
Images![alt](src)Detects unclosed image markers like ![alt](
Headings# ## ### etc.Supports progressive rendering for 1-6 level headings
Emphasis*italic* **bold**Handles emphasis syntax with * and _
Codeinline code and code blocksSupports backtick code block integrity checks
Lists- + * list markersDetects spaces after list markers
Dividers--- ===Avoids conflicts between Setext headings and dividers
XML Tags<tag>Handles HTML/XML tag closure states

How It Works

When hasNextChunk=true, the component will:

  1. Tokenized Parsing: Decomposes Markdown syntax into 11 token types for state management
  2. State Stack Maintenance: Uses stack structure to track nested syntax states
  3. Smart Truncation: Pauses rendering when syntax is incomplete to avoid displaying fragments
  4. Progressive Rendering: Gradually completes syntax rendering as content is supplemented
  5. Error Recovery: Automatically falls back to safe state when syntax errors are detected

Advanced Configuration

Custom Loading Components

You can customize the loading state display for incomplete syntax through incompleteMarkdownComponentMap:

tsx
import { XMarkdown } from '@ant-design/x-markdown';
const CustomLoadingComponents = {
LinkLoading: () => <span className="loading-link">🔗 Loading...</span>,
ImageLoading: () => <div className="loading-image">🖼️ Image loading...</div>,
};
const App = () => {
return (
<XMarkdown
content="Visit [Ant Design](https://ant.design) to view documentation"
streaming={{
hasNextChunk: true,
incompleteMarkdownComponentMap: {
link: 'link-loading',
image: 'image-loading',
},
}}
components={{
'link-loading': CustomLoadingComponents.LinkLoading,
'image-loading': CustomLoadingComponents.ImageLoading,
}}
/>
);
};

State Reset Mechanism

When input content changes fundamentally (non-incremental update), the component automatically resets the parsing state:

tsx
// Old content: "Hello "
// New content: "Hi there!" - triggers state reset
// New content: "Hello world!" - continues incremental parsing

hasNextChunk Best Practices

Avoid Getting Stuck

hasNextChunk should not always be true, otherwise it will cause:

  1. Syntax Hanging: Unclosed links, images and other syntax will remain in loading state
  2. Poor User Experience: Users see continuous loading animations
  3. Memory Leaks: State data accumulates continuously and cannot be cleaned properly

Correct Usage Example

tsx
import { useState, useEffect } from 'react';
import { XMarkdown } from '@ant-design/x-markdown';
const StreamingExample = () => {
const [content, setContent] = useState('');
const [hasNextChunk, setHasNextChunk] = useState(true);
useEffect(() => {
// Simulate streaming data
const chunks = [
'# Welcome to Streaming Rendering',
'\n\nThis is a demonstration',
' showing how to handle',
'[incomplete links](https://example',
'.com) and images',
'![Example Image](https://picsum.photos/200)',
'\n\nContent completed!',
];
let currentIndex = 0;
const interval = setInterval(() => {
if (currentIndex < chunks.length) {
setContent((prev) => prev + chunks[currentIndex]);
currentIndex++;
// Set to false on last chunk
if (currentIndex === chunks.length) {
setHasNextChunk(false);
}
} else {
clearInterval(interval);
}
}, 500);
return () => clearInterval(interval);
}, []);
return (
<XMarkdown
content={content}
streaming={{
hasNextChunk,
incompleteMarkdownComponentMap: {
link: 'loading-link',
image: 'loading-image',
},
}}
/>
);
};

Performance Optimization

  • Incremental Parsing: Only processes newly added content fragments, avoiding repeated parsing
  • State Caching: Maintains parsing state to reduce repeated calculations
  • Memory Management: Automatically cleans up processed state data
  • Error Boundaries: Prevents parsing errors from affecting overall rendering
Streaming Syntax Processing

Streaming syntax processing effect demonstration

CodeSandbox Icon
codepen icon
External Link Icon
expand codeexpand code
Markdown Source
Click Stream to start
Rendered Output