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 $30 are 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

Markdown rendering screenshot

Displays Markdown formatting, headings, lists, links, and blockquotes.

2. Inline LaTeX

Inline LaTeX screenshot

Render mathematical expressions directly within paragraphs.

3. Block LaTeX

Block LaTeX screenshot

Display equations such as integrals, fractions, summations, and quadratic formulas.

4. HTML Rendering

HTML rendering screenshot

Render HTML elements, including paragraphs, lists, emphasis, links, and supported custom math tags.

5. Code and LaTeX Handling

Code protection screenshot

Test code blocks and inline code containing LaTeX-like expressions.

6. Currency and Symbols

Currency and symbols screenshot

Test currency values, escaped dollar signs, and inline mathematical expressions.

7. Incomplete Equations

Incomplete equation screenshot

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.

Rich Markdown streaming demo

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,
)
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 into List<LatexToken>.
  • LatexToken: Token data model representing LatexTokenType.text, inlineMath, or blockMath.
  • 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 Flutter BuildContext.
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.

  1. Fork the repository.
  2. Create a feature branch.
  3. Implement your changes and add tests.
  4. 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.

Libraries

rich_markdown