Meziantou.Framework.SyntaxHighlighting 1.0.7

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

Meziantou.Framework.SyntaxHighlighting

Meziantou.Framework.SyntaxHighlighting highlights source code and returns HTML markup with CSS classes.

The package uses class names compatible with highlight.js by default, so you can reuse existing highlight.js themes without changing the generated HTML.

Usage

SyntaxHighlighter and HighlightOptions are in the Meziantou.Framework.SyntaxHighlighting namespace.

var html = SyntaxHighlighter.Highlight(
    "public class MyClass { }",
    "csharp");

Assert.Equal("""<span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title">MyClass</span> { }""", html);

Custom CSS prefix

If you do not want to use the default hljs- prefix, you can customize it:

var html = SyntaxHighlighter.Highlight(
    "public class MyClass { }",
    "csharp",
    new HighlightOptions { ClassPrefix = "syntax-" });

Assert.Equal("""<span class="syntax-keyword">public</span> <span class="syntax-keyword">class</span> <span class="syntax-title">MyClass</span> { }""", html);

The prefix may only contain ASCII letters, digits, - and _.

Invalid code

Grammars flag some constructs as illegal (for example a stray character where the language does not allow one). By default, such a lexeme is emitted as plain text and the rest of the code is still highlighted, like highlight.js does in a web page. Set IgnoreIllegals to false to get the whole result as plain text instead:

var code = """{"a": 1};""";
var html = SyntaxHighlighter.Highlight(code, "json", new HighlightOptions { IgnoreIllegals = false });
Assert.Equal("{&quot;a&quot;: 1};", html);

Embedded languages (CSS in HTML, C# in Razor, …) always ignore illegal lexemes.

Untrusted input

Everything that is not markup generated by the highlighter is HTML-encoded, so the output is safe to embed in a page whatever the code contains.

Grammars are regular expressions, and a hostile input can make some of them slow. When the code comes from untrusted users, bound each match with MatchTimeout; if a match takes longer, the code is returned as plain (encoded) text rather than blocking the caller:

var code = "public class MyClass { }";
var language = "csharp";
var html = SyntaxHighlighter.Highlight(code, language, new HighlightOptions { MatchTimeout = TimeSpan.FromSeconds(1) });

A timeout makes matching about twice as slow, which is why it is not enabled by default. Choose a value that leaves room for large documents on a busy machine, and keep it constant: each grammar is compiled again for every timeout, and only the grammars of the last few timeouts are cached.

Performance

Grammars are compiled on first use and run with the regular expression interpreter, which starts fast. Long-running processes that highlight a lot of code can opt in to compiled regular expressions, which cost more to create but scan several times faster:

<ItemGroup>
  <RuntimeHostConfigurationOption Include="Meziantou.Framework.SyntaxHighlighting.UseCompiledRegex" Value="true" />
</ItemGroup>

or AppContext.SetSwitch("Meziantou.Framework.SyntaxHighlighting.UseCompiledRegex", true) before the first call.

Styling

The package only produces HTML. You must provide the CSS rules for the generated classes.

With the default options, the generated markup is designed to work well with highlight.js themes. For example:

<pre><code><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title">MyClass</span> { }</code></pre>

Unknown languages

Highlight throws NotSupportedException when the language is not recognized. When the identifier comes from untrusted input — a Markdown fence, for example — use TryHighlight or IsSupported instead (WebUtility is in the System.Net namespace):

var code = "<b>Hello</b>";
var language = "not-a-language";
if (!SyntaxHighlighter.TryHighlight(code, language, out var html))
{
    html = WebUtility.HtmlEncode(code); // render it as plain text
}

Assert.Equal("&lt;b&gt;Hello&lt;/b&gt;", html);

SyntaxHighlighter.GetSupportedLanguages() returns every identifier and alias listed below.

Supported languages

The package currently supports these language identifiers and common aliases:

  • apache, apacheconf, htaccess
  • arduino, ino
  • armasm, arm
  • asciidoc, adoc
  • awk, gawk, mawk, nawk
  • bash, sh, zsh, ksh
  • bicep, bicepparam
  • bnf
  • c, h
  • clojure, clj, edn
  • clojure-repl
  • cmake, cmake.in
  • coffeescript, coffee, cson, iced
  • cpp, c++, cc, h++, hpp, hh, hxx, cxx
  • crystal, cr
  • csharp, cs, c#
  • css
  • d
  • dart
  • delphi, dpr, dfm, pas, pascal
  • diff, patch
  • django, jinja, jinja2, j2
  • dns, bind, zone
  • dockerfile, docker
  • dos, bat, cmd
  • elixir, ex, exs
  • elm
  • erb
  • erlang, erl
  • erlang-repl
  • fortran, f90, f95, f03, f08, f77, for, f
  • fsharp, fs, f#
  • gherkin, feature
  • glsl, vert, frag
  • go, golang
  • graphql, gql
  • groovy, gradle
  • haml
  • handlebars, hbs, html.hbs, html.handlebars, htmlbars, mustache
  • haskell, hs
  • hcl, terraform, tf, tfvars
  • html, htm, xhtml
  • http, https
  • ini, gitconfig
  • java, jsp
  • javascript, js, jsx, mjs, cjs
  • json, jsonc
  • julia, jl
  • julia-repl, jldoctest
  • kotlin, kt, kts
  • kql, kusto
  • latex, tex
  • less
  • lisp, elisp, emacs-lisp, common-lisp
  • llvm, ll
  • lua, pluto
  • makefile, mk, mak, make
  • markdown, md, mkdown, mkd
  • matlab
  • mermaid, mmd
  • msil, il, cil
  • nginx, nginxconf
  • nim, nims
  • nix, nixos
  • node-repl
  • objectivec, mm, objc, obj-c, obj-c++, objective-c++
  • ocaml, ml
  • perl, pl, pm
  • pgsql, postgres, postgresql
  • php
  • plaintext, text, txt
  • powershell, pwsh, ps, ps1
  • prolog
  • properties
  • protobuf, proto
  • puppet, pp
  • python, py, gyp, ipython
  • python-repl, pycon
  • r
  • razor, cshtml, cshtml-razor
  • ruby, rb, gemspec, podspec, thor, irb
  • rust, rs
  • scala
  • scheme, scm
  • scss
  • shell, console, shellsession
  • solidity, sol
  • sql
  • svelte
  • swift
  • tcl, tk
  • thrift
  • toml
  • twig, craftcms
  • typescript, ts, tsx, mts, cts
  • urlencoded, x-www-form-urlencoded
  • vbnet, vb
  • vbscript, vbs
  • vbscript-html
  • verilog, v, sv, svh
  • vhdl, vhd
  • vim, vimscript
  • vue
  • wasm, wat, wast
  • x86asm
  • xml, xsd, xsl, plist, rss, atom, svg
  • yaml, yml
  • zig, zon

Most identifiers resolve to the same language as in highlight.js, so Markdown fences written for highlight.js keep their highlighting. Two do not, because the language highlight.js uses for them is not supported: ml is OCaml (highlight.js: Standard ML) and gradle is Groovy (highlight.js has a Gradle grammar).

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.  net11.0 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net10.0

    • No dependencies.
  • net11.0

    • No dependencies.

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.0.7 111 11/29/2026
1.0.6 390 10/21/2026
1.0.5 138 9/30/2026
1.0.4 231 9/20/2026
1.0.3 134 9/6/2026
1.0.2 224 8/29/2026
1.0.1 405 7/7/2026
1.0.0 245 5/28/2026