S97SP.Prism.Blazor 1.1.1

dotnet add package S97SP.Prism.Blazor --version 1.1.1
                    
NuGet\Install-Package S97SP.Prism.Blazor -Version 1.1.1
                    
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="S97SP.Prism.Blazor" Version="1.1.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="S97SP.Prism.Blazor" Version="1.1.1" />
                    
Directory.Packages.props
<PackageReference Include="S97SP.Prism.Blazor" />
                    
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 S97SP.Prism.Blazor --version 1.1.1
                    
#r "nuget: S97SP.Prism.Blazor, 1.1.1"
                    
#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 S97SP.Prism.Blazor@1.1.1
                    
#: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=S97SP.Prism.Blazor&version=1.1.1
                    
Install as a Cake Addin
#tool nuget:?package=S97SP.Prism.Blazor&version=1.1.1
                    
Install as a Cake Tool

Prism.Blazor

Build Status NuGet Version License: MIT

Prism.Blazor is a lightweight, extensible, and Blazor-native component for client-side syntax highlighting. It leverages regular expressions to tokenize code and applies CSS classes for theming, bringing Prism.js-like capabilities directly into the .NET Blazor ecosystem without JavaScript interop for the core highlighting logic.

Table of Contents

Why Prism.Blazor?

  • Blazor Native: Pure .NET and Blazor implementation. No JavaScript interop for the core highlighting engine means a smaller deployment size and a development experience that stays within the C#/Razor paradigm.
  • Lightweight & Themable: Designed to be lean and efficient. Both the CodeHighlighter and MarkdownRenderer components support fully customizable, CSS-variable-based theming.
  • Extensible: Easily define new language syntaxes or extend existing ones using C#.

Features

  • Client-Side Highlighting: Processes and renders highlighted code directly in the browser.
  • Regex-Based Tokenization: Uses regular expressions for flexible and powerful language parsing.
  • CSS Theming: A robust theming system based on CSS variables allows for complete control over the appearance of both code and rendered markdown.
  • Preset Languages: Includes a growing set of common language definitions out-of-the-box.
  • Configurable Messages: Customize loading and error messages.
  • Asynchronous Processing: Highlighting is performed asynchronously to prevent UI blocking.
  • Markdown Rendering: Includes a MarkdownRenderer component that also supports the theming system.

Getting Started

Installation

Install via the .NET CLI or NuGet Package Manager:

dotnet add package S97SP.Prism.Blazor

Importing

Add the following to your project's _Imports.razor file:

@using Prism.Blazor
@using Prism.Blazor.PresetDefinitions 

Usage

Basic Code Highlighting

Use the CodeHighlighter component, providing your code Content and a LanguageDefinition.

<CodeHighlighter Content="@csharpCode"
                 LanguageDefinition="@(new CSharpLanguageDefinition())" />
@code {
    private string csharpCode = "public void SayHello() => Console.WriteLine(\"Hello, World!\");";
}

Markdown Rendering

Use the MarkdownRenderer component to render markdown strings to HTML.

<MarkdownRenderer MarkdownContent="@markdownText" />

@code {
    private string markdownText = "# Title\n\n**Bold text** and `inline code`.";
}

Component Parameters

  • Content / MarkdownContent (string?): The code or markdown string to render.
  • LanguageDefinition (ILanguageDefinition?): (CodeHighlighter only) An instance of a language definition.
  • CustomTheme (Dictionary<string, string>?): A dictionary of key-value pairs to override the default theme colors. See the Theming section.
  • UseHardLineBreaks (bool): (MarkdownRenderer only) If true, renders newlines in markdown as <br> tags.
  • LoadingMessage (string): Text displayed during processing.
  • ErrorMessage (string): Text displayed on error.

Available Language Definitions

  • CSharpLanguageDefinition, CssLanguageDefinition, JavaScriptLanguageDefinition, JsxLanguageDefinition, RazorLanguageDefinition, SqlLanguageDefinition, TypeScriptLanguageDefinition, TsxLanguageDefinition, XmlLanguageDefinition, PlainTextLanguageDefinition

Theming

Both components use a CSS variable-based system for theming. You can override the default dark theme by passing a dictionary to the CustomTheme parameter.

CodeHighlighter Theming

The keys for the CustomTheme dictionary correspond to token names.

<CodeHighlighter Content="@csharpCode"
                 LanguageDefinition="@(new CSharpLanguageDefinition())"
                 CustomTheme="@LightThemeForCode" />
@code {
    private string csharpCode = "public void SayHello() => Console.WriteLine(\"Hello, World!\");";

    // Example Light Theme
    private Dictionary<string, string> LightThemeForCode = new()
    {
        { "background", "#f5f2f0" },
        { "text", "#383a42" },
        { "comment", "#a0a1a7" },
        { "keyword", "#a626a4" },
        { "string", "#50a14f" },
        { "number", "#d19a66" },
        { "type-known", "#c18401" },
        { "identifier-method", "#4078f2" },
        { "punctuation", "#383a42" }
    };
}

MarkdownRenderer Theming

The keys correspond to markdown element types.

<MarkdownRenderer MarkdownContent="@markdownText" CustomTheme="@LightThemeForMarkdown" />

@code {
    private string markdownText = "# Hello\nThis is a *light* theme!";

    // Example Light Theme
    private Dictionary<string, string> LightThemeForMarkdown = new()
    {
        { "text", "#24292e" },
        { "heading", "#24292e" },
        { "link", "#0366d6" },
        { "border", "#e1e4e8" },
        { "code-background", "#f6f8fa" },
        { "blockquote-text", "#6a737d" },
        { "blockquote-border", "#dfe2e5" }
    };
}

Advanced Customization

Creating Custom Language Definitions

Implement the ILanguageDefinition interface to define new languages or modify existing ones.

public class MyLangDefinition : ILanguageDefinition
{
    public string Name => "MyLang";

    private static readonly List<TokenRule> Rules =
    [
        // Priority 10, will be styled via the 'mylang-special-keyword' CSS class
        new TokenRule(@"\b(BEGIN|END)\b", 10, "mylang-special-keyword", null),
    ];

    public IEnumerable<TokenRule> GetRules() => Rules;
}

Understanding TokenRule Logic

The highlighting engine finds all regex matches and sorts them by: 1. Start Index (ascending), 2. Priority (descending), 3. Match Length (descending). It then processes the sorted, non-overlapping matches to build the final highlighted output.

Building from Source

  1. Clone the repository.
  2. Run dotnet build Prism.Blazor.sln -c Release.

Contributing

Contributions are welcome! Please open an issue to discuss your ideas or submit a pull request.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Acknowledgements

  • Inspired by the original Prism.js library.
  • Color palettes for preset themes are inspired by popular code editors.
Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  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
1.1.1 91 8/11/2025
1.1.0 87 8/11/2025
1.0.3 243 5/25/2025
1.0.2 154 5/25/2025
1.0.1 149 5/25/2025
1.0.0 141 5/24/2025

Added CSS variable-based theming support for both CodeHighlighter and MarkdownRenderer components. You can now pass a `CustomTheme` dictionary to override default colors.