rich_markdown 1.1.0
rich_markdown: ^1.1.0 copied to clipboard
A Flutter package for rendering Markdown, LaTeX, and HTML content.
Rich Markdown #
A Flutter package for rendering Markdown, LaTeX equations, and HTML content in a single reusable widget. Designed for AI chat applications, streaming responses, educational apps, documentation viewers, and content-rich mobile applications.
โจ Features #
- ๐ Markdown Rendering โ Headings, paragraphs, bold, italic, strikethrough, lists, blockquotes, links, and GitHub Flavored Markdown.
- ๐งฎ LaTeX Support โ Render inline and block mathematical expressions using
flutter_math_fork. - ๐ HTML Rendering โ Display HTML content with custom LaTeX tags.
- ๐ฌ AI Chat Ready โ Designed to support Markdown and math in AI-generated responses.
- โก Streaming-Friendly โ Supports progressively updated content as responses arrive.
- ๐ Link Callbacks โ Handle link taps with your own navigation logic.
- ๐จ Custom Styling โ Customize text size, color, font family, and layout.
- ๐งฉ Simple API โ One widget for multiple content formats.
๐ฆ Installation #
Add the package to your pubspec.yaml:
dependencies:
rich_markdown: ^1.1.0
Then run:
flutter pub get
๐ Quick Start #
Import the package:
import 'package:flutter/material.dart';
import 'package:rich_markdown/rich_markdown.dart';
Render Markdown content:
const content = r'''
# Hello, Rich Markdown!
This is **bold text** and *italic text*.
Inline equation: \(E = mc^2\)
Block equation:
$$
\int_0^1 x^2\,dx = \frac{1}{3}
$$
''';
RichMarkdownView(
data: content,
type: ContentType.markdown,
textStyle: const TextStyle(
fontSize: 16,
height: 1.5,
),
)
๐ Supported LaTeX Syntax #
| Syntax | Example | Type | Description |
|---|---|---|---|
$...$ |
$x^2$ |
Inline | Standard inline math |
$$...$$ |
$$x^2$$ |
Block | Display / block equation |
\(...\) |
\(x^2\) |
Inline | LaTeX parenthesized inline math |
\[...\] |
\[x^2\] |
Block | LaTeX bracketed display math |
๐ก๏ธ Robust Edge Case Handling:
- Code Block Isolation: LaTeX expressions inside fenced code blocks (
```or~~~), inline code spans (`...`), and HTML<pre>/<code>tags remain literal code. - Currency Protection: Currency values such as
$100,$100.50,$10-$20, and$10, $20, and $30are preserved as literal text. - Escaped Delimiters: Delimiters preceded by backslashes (like
\$or\() do not start equations. - Streaming-Safe: Incomplete expressions (e.g.
$x +or$$\frac{1}{2}) remain text without crashing or throwing exceptions. - Multiple Equations: Multiple expressions in the same paragraph parse and render independently.
- Multiline Block Equations: Equations spanning across line breaks work smoothly with both
$$and\[.
๐ธ Screenshots #
1. Markdown Rendering #

Displays Markdown formatting, headings, lists, links, and blockquotes.
2. Inline LaTeX #

Render mathematical expressions directly within paragraphs.
3. Block LaTeX #

Display equations such as integrals, fractions, summations, and quadratic formulas.
4. HTML Rendering #

Render HTML elements, including paragraphs, lists, emphasis, links, and supported custom math tags.
5. Code and LaTeX Handling #

Test code blocks and inline code containing LaTeX-like expressions.
6. Currency and Symbols #

Test currency values, escaped dollar signs, and inline mathematical expressions.
7. Incomplete Equations #

Test how unfinished equations are handled while content is being generated.
๐ฌ AI Streaming Demo #
Watch Rich Markdown render progressively updated AI-style responses containing Markdown and LaTeX.

The animation demonstrates simulated streaming in the example application. Actual streaming behavior depends on how your application supplies updated content and how incomplete Markdown and LaTeX expressions are handled.
๐ ๏ธ Usage #
Markdown #
RichMarkdownView(
data: '# Welcome\n\nThis is **Markdown**.',
type: ContentType.markdown,
)
LaTeX #
Use raw strings to preserve LaTeX backslashes:
RichMarkdownView(
data: r'\int_0^\infty e^{-x}\,dx = 1',
type: ContentType.latex,
)
The latex content type renders the supplied expression in display math mode.
HTML #
RichMarkdownView(
data: '''
<h2>Hello</h2>
<p>This is <strong>HTML</strong>.</p>
''',
type: ContentType.html,
)
HTML with LaTeX #
The package supports custom tex and texblock tags containing Base64-encoded LaTeX.
RichMarkdownView(
data: '''
<p>Inline math: <tex>Uj1hXjI=</tex></p>
''',
type: ContentType.html,
)
The encoded example represents R=a^2.
Custom Styling #
RichMarkdownView(
data: '# Custom Content\n\nStyled text.',
type: ContentType.markdown,
textStyle: const TextStyle(
fontSize: 18,
height: 1.6,
color: Colors.black87,
),
shrinkWrap: true,
)
Handle Link Taps #
RichMarkdownView(
data: '[Visit Flutter](https://flutter.dev)',
onLinkTap: (url) {
debugPrint('Tapped link: $url');
// Add your navigation or URL-launching logic here.
},
)
๐ API Reference #
RichMarkdownView #
| Parameter | Type | Default | Description |
|---|---|---|---|
data |
String |
Required | Content to render |
type |
ContentType |
ContentType.markdown |
Content format |
textStyle |
TextStyle? |
null |
Text styling |
onLinkTap |
void Function(String? url)? |
null |
Link tap callback |
shrinkWrap |
bool |
true |
Controls HTML content shrink wrapping |
mathErrorBuilder |
Widget Function(BuildContext, String, String)? |
null |
Custom fallback builder for malformed LaTeX |
ContentType #
| Value | Description |
|---|---|
ContentType.markdown |
Renders Markdown and supported LaTeX expressions |
ContentType.latex |
Renders the input as a block LaTeX expression |
ContentType.html |
Renders HTML with supported custom math tags |
๐งฉ Modular Architecture (Separating Parsing from Rendering) #
Rich Markdown separates parsing logic from UI rendering:
LatexTokenizer: Streaming-safe tokenizer that converts raw text intoList<LatexToken>.LatexToken: Token data model representingLatexTokenType.text,inlineMath, orblockMath.LatexProcessor: Base64 encoding/decoding and converting LaTeX into HTML<tex>/<texblock>tags.MarkdownProcessor: Converts Markdown or HTML strings to HTML with protected LaTeX equations without needing a FlutterBuildContext.
import 'package:rich_markdown/rich_markdown.dart';
// Tokenize raw text directly:
final tokens = LatexTokenizer.tokenize(r'Formula: $E = mc^2$ and $100');
// Process Markdown to HTML without rendering:
final html = MarkdownProcessor.process(r'# Analysis of $f(x)$');
๐งช Example App #
Explore the example application to try different content types and test mathematical expressions.
git clone https://github.com/rohit-bon/rich_markdown.git
cd rich_markdown/example
flutter pub get
flutter run
Replace YOUR_USERNAME with your GitHub username.
๐งฐ Built With #
๐บ๏ธ Roadmap #
- โ More comprehensive Markdown and LaTeX parsing tests
- โ Improved handling of incomplete equations during streaming
- โ Additional HTML and Markdown customization
- โ Performance improvements for frequently updated AI responses
- โ More examples and documentation
๐ค Contributing #
Contributions, bug reports, and feature requests are welcome.
- Fork the repository.
- Create a feature branch.
- Implement your changes and add tests.
- Submit a pull request.
๐ License #
This project is distributed under the MIT License. See the LICENSE file for details.
โญ Support #
If you find Rich Markdown useful, consider giving the project a star on GitHub and sharing feedback through issues or pull requests.