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.
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" />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.
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.
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.
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.
It works with all of them. Even in static SSR, the first response from the server has the highlighted code.
All the languages of Prism.js are supported, including the languages that are embedded in other languages, such as PHP in HTML, and JSX.
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).
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.
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.
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.");
}
}
}<link rel="stylesheet" href="_content/Toolbelt.Blazor.Prismizor/themes/prism-tomorrow.min.css" />
<CodeBlock Code="@code" Language="@PrismizorLanguages.CSharp" />
It takes four steps. Every Blazor hosting model works the same way.
dotnet add package Toolbelt.Blazor.Prismizor
_Imports.razor@using Toolbelt.Blazor.Prismizor
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" />
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.