WebSpark.PrismSpark

A powerful C# port of PrismJS for server-side syntax highlighting with advanced features, theming support, and 20+ programming languages.

Key Features

20+ Programming Languages

Support for C#, JavaScript, Python, SQL, JSON, HTML, CSS, XML, YAML, Bash, PowerShell, Java, Rust, C, C++, and more.

Advanced Theming System

Built-in themes (Prism, Dark, Tomorrow Night, Solarized) with CSS generation and custom theme creation support.

Plugin Architecture

Extensible plugin system with built-in plugins for line numbers, copy-to-clipboard, syntax validation, and more.

High Performance

Optimized tokenization engine with efficient parsing and highlighting for excellent server-side performance.

Multiple Highlighters

Choose from HtmlHighlighter, EnhancedHtmlHighlighter, or ThemedHtmlHighlighter based on your needs.

Easy Integration

Simple NuGet package installation with dependency injection support for ASP.NET Core applications.

Installation & Setup

Install NuGet Package

Install the WebSpark.PrismSpark package using the Package Manager Console:

dotnet add package WebSpark.PrismSpark --version 1.0.0

Or using Package Manager:

Install-Package WebSpark.PrismSpark -Version 1.0.0

Configure Services (ASP.NET Core)

Register PrismSpark services in your Program.cs:

using WebSpark.PrismSpark; using WebSpark.PrismSpark.Highlighting; // Register PrismSpark services builder.Services.AddSingleton<IHighlighter, HtmlHighlighter>(); builder.Services.AddSingleton<ThemedHtmlHighlighter>();

Quick Start Examples

Basic Tokenization

Simple code tokenization using the core Prism engine:

using WebSpark.PrismSpark; var code = @"<p>Hello world!</p>"; var grammar = LanguageGrammars.Html; var tokens = Prism.Tokenize(code, grammar); // Process tokens as needed foreach (var token in tokens) { Console.WriteLine($"{token.Type}: {token.Content}"); }
HTML Highlighting

Generate highlighted HTML with CSS classes:

using WebSpark.PrismSpark; using WebSpark.PrismSpark.Highlighting; var code = @"function hello() { console.log('Hello World!'); }"; var highlighter = new HtmlHighlighter(); var html = highlighter.Highlight(code, LanguageGrammars.JavaScript, "javascript"); // html contains: <span class="token keyword">function</span>...

Advanced Features

Create complete HTML pages with embedded CSS using the ThemedHtmlHighlighter:

var highlighter = new ThemedHtmlHighlighter("dark"); // Generate just the highlighted HTML var html = highlighter.Highlight(code, "python"); // Or generate a complete HTML page with CSS var htmlPage = highlighter.GenerateHtmlPage(code, "python", "My Code"); // Generate standalone CSS var css = highlighter.GenerateCss();

Use EnhancedHtmlHighlighter for advanced features like line numbers and highlighted lines:

var highlighter = new EnhancedHtmlHighlighter(); var options = new HighlightOptions { EnableLineNumbers = true, HighlightedLines = new[] { 2, 4, 6 }, CustomCssClasses = new Dictionary<string, string> { ["keyword"] = "my-custom-keyword-style" } }; var html = highlighter.Highlight(code, "csharp", options);

Create and register custom themes for unique styling:

var customTheme = new Theme { Name = "my-awesome-theme", Description = "A custom theme with vibrant colors", Background = new ThemeStyle { BackgroundColor = "#1e1e1e" }, Foreground = new ThemeStyle { Color = "#d4d4d4" }, TokenStyles = new Dictionary<string, ThemeStyle> { ["keyword"] = new() { Color = "#569cd6", FontWeight = "bold" }, ["string"] = new() { Color = "#ce9178" }, ["comment"] = new() { Color = "#6a9955", FontStyle = "italic" }, ["function"] = new() { Color = "#dcdcaa" } } }; ThemeManager.RegisterTheme(customTheme); ThemeManager.SetDefaultTheme("my-awesome-theme");

Extend functionality with the built-in plugin system:

// Available built-in plugins: // - Line Numbers Plugin // - Copy to Clipboard Plugin // - Syntax Validation Plugin // - Theme Switching Plugin // - Custom CSS Classes Plugin // Plugins are automatically integrated with Enhanced and Themed highlighters var highlighter = new EnhancedHtmlHighlighter(); // Line numbers and other plugins work automatically

Supported Programming Languages

C# / .NET
JavaScript / TypeScript
Python
Java
SQL
HTML / CSS
JSON / XML
YAML
Bash / PowerShell
Rust
C / C++
PHP
Go
Ruby
Markdown
And more...

Built-in Themes

# Prism (Default)
function hello() {
  console.log('Hi!');
}
Prism

Classic light theme

# Dark
function hello() {
  console.log('Hi!');
}
Dark

Modern dark theme

# Tomorrow Night
function hello() {
  console.log('Hi!');
}
Tomorrow Night

Popular dark theme

# Solarized Light
function hello() {
  console.log('Hi!');
}
Solarized Light

Elegant light theme

API Documentation

Core Classes
  • Prism - Core tokenization engine
  • LanguageGrammars - Grammar definitions
  • IHighlighter - Highlighter interface
  • HtmlHighlighter - Basic HTML output
  • EnhancedHtmlHighlighter - Advanced features
  • ThemedHtmlHighlighter - With theming
Theme System
  • ThemeManager - Theme management
  • Theme - Theme configuration
  • ThemeStyle - Style definitions
  • CssGenerator - CSS generation
  • CssOptions - CSS configuration
Plugin System
  • PluginManager - Plugin coordination
  • IPlugin - Plugin interface
  • HighlightOptions - Configuration
  • HooksSystem - Event hooks

Ready to Get Started?

Experience the power of WebSpark.PrismSpark in your next project