Skip to content

DearVa/LiveMarkdown.Avalonia

Repository files navigation

netstandard2.0 Avalonia License GitHub issues NuGet

demo.gif

πŸ‘‹ Introduction

LiveMarkdown.Avalonia is a High-performance Markdown viewer for Avalonia applications. It supports real-time rendering of Markdown content, so it's ideal for applications that require dynamic text updating, especially when streaming large model outputs.

⭐ Features

  • πŸš€ High-performance rendering powered by Markdig
  • πŸ”„ Real-time updates: Automatically re-renders changes in Markdown content
  • 🎨 Customizable styles: Easily style Markdown elements using Avalonia's powerful styling system
  • πŸ”— Hyperlink support: Clickable links with customizable behavior
  • πŸ“Š Table support: Render tables with proper formatting
  • πŸ“œ Code block syntax highlighting: Supports multiple languages with TextMateSharp
  • πŸ–ΌοΈ Image support: Load online, local even avares images asynchronously
  • ✍️ Selectable text: Text can be selected across different Markdown elements

Note

This library currently only supports Append and Clear operations on the Markdown content, which is enough for LLM streaming scenarios.

Warning

Known issue: Avalonia 11.3.5 and 11.3.6 changed text layout behavior, which may cause some text offset issues in certain scenarios. e.g. code inline has extra bottom margin, wried italic font rendering, etc.

Please use 11.3.0 ~ 11.3.4 or >= 11.3.7 to avoid this problem.

✈️ Roadmap

  • Basic Markdown rendering
  • Real-time updates
  • Hyperlink support
  • Table support
  • Code block syntax highlighting
  • Image support
    • Bitmap
    • SVG
    • Online images
    • Local images
    • avares images
  • Selectable text across elements
  • LaTeX support
  • HTML rendering

πŸš€ Getting Started

1. Install the NuGet package

You can install the latest version from NuGet CLI:

dotnet add package LiveMarkdown.Avalonia

or use the NuGet Package Manager in your IDE.

2. Register the Markdown styles in your Avalonia application

<Application
  x:Class="YourAppClass" xmlns="https://github.com/avaloniaui"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" RequestedThemeVariant="Default">

  <Application.Styles>
    <!-- Your other styles here -->
    <StyleInclude Source="avares://LiveMarkdown.Avalonia/Styles.axaml"/>
  </Application.Styles>

  <Application.Resources>
    <!-- Your other resources here -->
    <Color x:Key="BorderColor">#3DFFFFFF</Color>
    <Color x:Key="ForegroundColor">#FFFFFF</Color>
    <Color x:Key="CardBackgroundColor">#15000000</Color>
    <Color x:Key="SecondaryCardBackgroundColor">#99000000</Color>
  </Application.Resources>
</Application>

3. Use the MarkdownRenderer control in your XAML

Add the MarkdownRenderer control to your .axaml file:

<YourControl
  xmlns:md="clr-namespace:LiveMarkdown.Avalonia;assembly=LiveMarkdown.Avalonia">
  <md:MarkdownRenderer x:Name="MarkdownRenderer"/>
</YourControl>

Then you can manage the Markdown content in your code-behind:

// ObservableStringBuilder is used for efficient string updates
var markdownBuilder = new ObservableStringBuilder();
MarkdownRenderer.MarkdownBuilder = markdownBuilder;

// Append Markdown content, this will trigger re-rendering
markdownBuilder.Append("# Hello, Markdown!");
markdownBuilder.Append("\n\nThis is a **live** Markdown viewer for Avalonia applications.");

// Clear the content
markdownBuilder.Clear();

If you want to load local images with relative paths, you can set the MarkdownRenderer.ImageBasePath property.

πŸͺ„ Style Customization

Markdown elements can be styled using Avalonia's powerful styling system. You can override the default styles by defining your own styles in your application styles.

Avalonia Styling Docs:

4. Enable LaTeX rendering

LaTeX is supported via the LiveMarkdown.Avalonia.Math package. You can install it via NuGet:

dotnet add package LiveMarkdown.Avalonia.Math

Then register both the MathInlineNode and MathBlockNode before using LaTeX in your Markdown content:

using LiveMarkdown.Avalonia;

MarkdownNode.Register<MathInlineNode>();
MarkdownNode.Register<MathBlockNode>();

πŸ€” FAQ

  • Q: Why some emojis not rendered correctly (rendered in single color)?
  • A: This is a known issue caused by Skia (the render backend of Avalonia). You can upgrade SkiaSharp version (e.g. >= 3.117.0) to fix this. Related issue

🀝 Contributing

We welcome issues, feature ideas, and PRs! See CONTRIBUTING.md for guidelines.

πŸ“„ License

Distributed under the Apache 2.0 License. See LICENSE for more information.

Third-Party Licenses

About

High performance, real-time markdown renderer for AI/LLM

Topics

Resources

License

Contributing

Stars

Watchers

Forks

Sponsor this project

Contributors 4

  •  
  •  
  •  
  •  

Languages