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
<PackageReference Include="MarkView.Avalonia.SyntaxHighlighting" Version="12.0.2" />
<PackageVersion Include="MarkView.Avalonia.SyntaxHighlighting" Version="12.0.2" />
<PackageReference Include="MarkView.Avalonia.SyntaxHighlighting" />
paket add MarkView.Avalonia.SyntaxHighlighting --version 12.0.2
#r "nuget: MarkView.Avalonia.SyntaxHighlighting, 12.0.2"
#:package MarkView.Avalonia.SyntaxHighlighting@12.0.2
#addin nuget:?package=MarkView.Avalonia.SyntaxHighlighting&version=12.0.2
#tool nuget:?package=MarkView.Avalonia.SyntaxHighlighting&version=12.0.2
MarkView.Avalonia.SyntaxHighlighting
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:
- Creates a
DualThemeTextMateHighlighterwrapping twoTextMateHighlighterinstances (one per variant). - Replaces the built-in
CodeBlockRendererwithTextMateCodeBlockRenderer.
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 | Versions 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. |
-
net10.0
- MarkView.Avalonia (>= 12.0.2)
- TextMateSharp (>= 2.0.3)
- TextMateSharp.Grammars (>= 2.0.3)
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 |