Prismizor is a faithful port of Prism.js to Blazor

It highlights your code in pure C#. It has no JavaScript at all, and it makes the same HTML as Prism.js. So the highlighted code is already in the first response from the server.

@* Highlighted when the component is rendered. *@
<CodeBlock Code="@code"
           Language="@PrismizorLanguages.CSharp" />

All the credit goes to Prism.js

Prismizor is not an original work. The grammars of all the languages, the way the code is tokenized, the HTML that it makes, and the themes all come from Prism.js. Every original idea in this package belongs to Prism.js.

I only ported it to C#, because I wanted to use the power of Prism.js natively in the rendering lifecycle of Blazor and in server-side rendering.

My deepest thanks go to Lea Verou and all the contributors of Prism.js. Without their great work, this package would not exist.

What is Prismizor?

Prismizor is a syntax highlighting component for Blazor. It is a C# port of Prism.js, one of the most popular syntax highlighters. You give it code and a language, and you get highlighted HTML.

  • 0 bytes of JavaScript
  • 288 languages
  • 8 built-in themes
  • .NET 10 + 11 supported

Features

The same HTML as Prism.js

The output is the same as Prism.js 1.30.0, byte by byte. So the theme CSS files of Prism.js work as they are, and the tests compare the output with Prism.js.

No JavaScript

The code is highlighted in C#, when the component is rendered. You don't need any script in the browser, and no flash of plain code.

Blazor Server, WebAssembly, and static SSR

It works with all of them. Even in static SSR, the first response from the server has the highlighted code.

288 languages

All the languages of Prism.js are supported, including the languages that are embedded in other languages, such as PHP in HTML, and JSX.

Small for WebAssembly

The languages that your app doesn't use are removed when you publish. An app that uses only C# adds about 9 KB (Brotli compressed).

Any theme of Prism.js

The eight themes of Prism.js are included. The HTML is the same as Prism.js, so any other theme for Prism.js works too, such as the ones in prism-themes.

Note: the plugin system of Prism.js, such as Line Numbers and Show Language, is not supported.

Try it

Pick a language and a theme. The preview is highlighted by Prismizor right in your browser, and you can copy the code that makes the same result.

Here you can pick from 32 popular languages. Prismizor itself supports all 288 languages of Prism.js.

Theme
using System.Linq;

namespace Demo;

public record Person(string Name, int Age);

public static class Program
{
    public static void Main()
    {
        var people = new[] { new Person("Ann", 31), new Person("Bob", 27) };
        foreach (var p in people.OrderBy(p => p.Age))
        {
            Console.WriteLine($"{p.Name} is {p.Age} years old.");
        }
    }
}

1. Add the theme CSS file

<link rel="stylesheet" href="_content/Toolbelt.Blazor.Prismizor/themes/prism-tomorrow.min.css" />

2. Use the CodeBlock component

<CodeBlock Code="@code" Language="@PrismizorLanguages.CSharp" />

Get started

It takes four steps. Every Blazor hosting model works the same way.

  1. Add the package

    dotnet add package Toolbelt.Blazor.Prismizor
  2. Open the namespace in _Imports.razor

    @using Toolbelt.Blazor.Prismizor
  3. Add a link to a theme CSS file

    Put it in App.razor or index.html. This is the theme that you pick in "Try it" above, and eight themes are included.

    You are not limited to them. Prismizor makes the same HTML as Prism.js, so any theme CSS file for Prism.js works as it is, such as the extra themes in prism-themes. Just link the CSS file in the same way.

    <link rel="stylesheet" href="_content/Toolbelt.Blazor.Prismizor/themes/prism-tomorrow.min.css" />
  4. Use the CodeBlock component

    @code {
        private const string code = "var x = 1;";
    }
    
    <CodeBlock Code="@code" Language="@PrismizorLanguages.CSharp" />

To highlight the code blocks in Markdown, or to use the highlighter without the component, see the README.

An unhandled error has occurred. Reload 🗙