MarkView.Avalonia.SyntaxHighlighting 12.0.2

dotnet add package MarkView.Avalonia.SyntaxHighlighting --version 12.0.2
                    
NuGet\Install-Package MarkView.Avalonia.SyntaxHighlighting -Version 12.0.2
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="MarkView.Avalonia.SyntaxHighlighting" Version="12.0.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="MarkView.Avalonia.SyntaxHighlighting" Version="12.0.2" />
                    
Directory.Packages.props
<PackageReference Include="MarkView.Avalonia.SyntaxHighlighting" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add MarkView.Avalonia.SyntaxHighlighting --version 12.0.2
                    
#r "nuget: MarkView.Avalonia.SyntaxHighlighting, 12.0.2"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package MarkView.Avalonia.SyntaxHighlighting@12.0.2
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=MarkView.Avalonia.SyntaxHighlighting&version=12.0.2
                    
Install as a Cake Addin
#tool nuget:?package=MarkView.Avalonia.SyntaxHighlighting&version=12.0.2
                    
Install as a Cake Tool

MarkView.Avalonia.SyntaxHighlighting

NuGet Version NuGet Downloads Avalonia CI License

TextMate grammar-based syntax highlighting for MarkView.Avalonia fenced code blocks. Colours update in-place when the user switches between light and dark themes — no document rebuild or scroll reset.

Installation

dotnet add package MarkView.Avalonia.SyntaxHighlighting

Quick Start

Call UseTextMateHighlighting() before setting Markdown:

var viewer = new MarkdownViewer();
viewer.UseTextMateHighlighting();
viewer.Markdown = markdownText;

Or activate globally at application startup so every MarkdownViewer in the app gets highlighting automatically:

// App.axaml.cs
MarkdownViewerDefaults.Extensions.AddTextMateHighlighting();

By default, DarkPlus is used for dark themes and LightPlus for light themes. Both highlighters are created lazily — only the one matching the active variant is loaded at startup.

Theme Selection

Pass explicit ThemeName values to override the defaults:

using TextMateSharp.Grammars;

viewer.UseTextMateHighlighting(
    darkTheme:  ThemeName.Monokai,
    lightTheme: ThemeName.QuietLight);

Available themes (from TextMateSharp.Grammars.ThemeName):

Dark Light
DarkPlus LightPlus
Monokai QuietLight
SolarizedDark SolarizedLight
TomorrowNightBlue Abyss
HighContrastLight HighContrastLight
KimbieDark

How It Works

UseTextMateHighlighting() registers a TextMateExtension which:

  1. Creates a DualThemeTextMateHighlighter wrapping two TextMateHighlighter instances (one per variant).
  2. Replaces the built-in CodeBlockRenderer with TextMateCodeBlockRenderer.

At render time, each line is tokenised via IGrammar.TokenizeLine and emitted as coloured Run elements inside a TextBlock. Grammars are cached per language per highlighter instance.

When the user switches between light and dark themes, only TextBlock.Inlines is rebuilt — the surrounding Border and the rest of the document are untouched. Images stay loaded, scroll position is preserved, and Mermaid diagrams are not retriggered.

Languages not supported by the grammar registry fall back to the default monochrome rendering automatically.

Low-Level API

Use TextMateExtension directly for full control:

using MarkView.Avalonia.SyntaxHighlighting;
using MarkView.Avalonia.Rendering;
using TextMateSharp.Grammars;

var renderer = new AvaloniaRenderer();
new TextMateExtension(ThemeName.Monokai, ThemeName.QuietLight).Register(renderer);

Or supply a TextMateHighlighter as a standalone ICodeHighlighter (for use outside MarkdownViewer):

var highlighter = new TextMateHighlighter(ThemeName.DarkPlus);
var tokens = highlighter.Highlight("var x = 1;", "csharp");

Implementing a Custom Highlighter

Any ICodeHighlighter from the core package can be used instead:

using Avalonia.Media;
using MarkView.Avalonia.Extensions;

public class MyHighlighter : ICodeHighlighter
{
    public IReadOnlyList<(string Text, IBrush? Foreground)>? Highlight(
        ReadOnlyMemory<char> line, string? language)
    {
        // return null to fall back to monochrome rendering
        return null;
    }
}

renderer.CodeHighlighter = new MyHighlighter();

For automatic dark/light updates without a document rebuild, implement IThemeAwareCodeHighlighter instead:

public class MyHighlighter : IThemeAwareCodeHighlighter
{
    public IReadOnlyList<(string Text, IBrush? Foreground)>? Highlight(
        ReadOnlyMemory<char> line, string? language)
        => HighlightVariant(line, language, isDark: false);

    public IReadOnlyList<(string Text, IBrush? Foreground)>? HighlightVariant(
        ReadOnlyMemory<char> line, string? language, bool isDark)
    {
        // return coloured tokens for the requested variant
        return null;
    }
}

License

MIT © Nicolas Musset

Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
12.0.2 107 4/21/2026
12.0.1 96 4/16/2026
12.0.1-beta.3 52 4/15/2026
12.0.1-beta.2 50 4/10/2026