Zaiets.Markdown.Renderer 1.0.0

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

Zaiets.Markdown.Renderer

Fast Markdown to HTML renderer for .NET 10 — GFM support, syntax highlighting, and built-in sanitization.

NuGet License: MIT


Features

  • CommonMark compliant — headings, paragraphs, blockquotes, lists, code blocks, horizontal rules, inline code, emphasis, strong, links, images
  • GitHub Flavored Markdown (GFM) — tables, task lists, strikethrough, autolinks
  • Syntax highlighting — CSS-class spans (highlight.js-compatible) for C#, JavaScript, TypeScript, Python, Go, Rust, Java, SQL, and more — zero JS dependencies at render time
  • HTML sanitization — allowlist-based XSS scrubber; safe for user-supplied content
  • Typographer — optional em-dash, en-dash, ellipsis, and copyright substitutions
  • Zero dependencies — pure .NET 10, no third-party packages
  • Thread-safe — a single MarkdownRenderer instance can be used concurrently

Installation

dotnet add package Zaiets.Markdown.Renderer

Quick Start

using Zaiets.Markdown.Renderer;

// Simplest usage — extension method on string
string html = "# Hello, **world**!".ToHtml();

// With a renderer instance (recommended for repeated use)
var renderer = new MarkdownRenderer();
string html = renderer.Render("# Hello, **world**!");

// Full HTML document
string doc = renderer.RenderDocument(markdown, title: "My Page");

Options

var opts = new MarkdownOptions
{
    EnableGfm              = true,   // GitHub Flavored Markdown extensions
    EnableSyntaxHighlighting = true, // syntax-highlighted code blocks
    SanitizeOutput         = true,   // strip XSS vectors (recommended for user content)
    AutoLinks              = true,   // bare URLs → <a> tags
    ExternalLinkPolicy     = true,   // rel="nofollow noopener noreferrer" + target="_blank"
    GenerateHeadingIds     = true,   // <h2 id="slug"> for in-page anchor links
    SoftBreaks             = false,  // single newline → <br> (off by default)
    Typographer            = false,  // -- → &ndash;, --- → &mdash;, ... → &hellip;
    CodeBlockCssClass      = "code-block",
    HighlightCssPrefix     = "hljs",
    TableCssClass          = "table", // optional CSS class on <table>
    MaxNestingDepth        = 10,
};

var renderer = new MarkdownRenderer(opts);

Built-in presets

Preset Description
MarkdownOptions.Default All features enabled; safe for user content
MarkdownOptions.Trusted Sanitization and external-link policy disabled (internal use)

Fluent builder extensions

string html = markdown.ToHtml(
    MarkdownOptions.Default
        .WithTableClass("table table-striped")
        .WithTypographer()
        .WithSoftBreaks()
);

Available extension methods on MarkdownOptions:

Method Effect
WithoutGfm() Disable GFM extensions
WithoutHighlighting() Disable syntax highlighting
WithoutSanitization() Disable HTML sanitization
WithSoftBreaks() Single newlines become <br>
WithTableClass(css) Set a CSS class on rendered <table> tags
WithTypographer() Enable typographic substitutions

Async / Stream API

// From a TextReader
using var reader = new StringReader(markdownText);
string html = await reader.RenderToHtmlAsync();

// From a Stream (UTF-8)
await using var file = File.OpenRead("docs/README.md");
string html = await file.RenderToHtmlAsync(MarkdownOptions.Trusted);

// Write directly to a TextWriter
await using var output = new StringWriter();
await stream.RenderToAsync(output);

Syntax Highlighting

No JavaScript is required at render time. The renderer emits <span class="hljs-keyword"> etc. Link any highlight.js-compatible theme CSS and the code blocks will light up.

<link rel="stylesheet"
      href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/github.min.css">

Supported languages (identifier in the fenced block):

csharp / cs, javascript / js, typescript / ts, python / py, go, rust, java, sql, bash / sh, yaml / yml


GFM Tables

| Name    | Type   | Required |
|---------|--------|:--------:|
| `id`    | int    |    ✓     |
| `email` | string |    ✓     |
| `bio`   | string |          |

Renders to a properly-aligned <table> with <thead> / <tbody>.


Task Lists

- [x] Write unit tests
- [x] Add README
- [ ] Publish to NuGet

Renders to <li class="task-list-item"><input type="checkbox" disabled ...>.


HTML Sanitization

When SanitizeOutput = true (default), the sanitizer:

  • Strips <script>, <style>, <iframe>, <object>, <embed>, <form>, and similar tags entirely
  • Removes event-handler attributes (onclick, onerror, etc.)
  • Blocks dangerous URL schemes (javascript:, data:, vbscript:) in href/src
  • Allows a curated set of safe HTML elements and attributes

You can also use the sanitizer independently:

using Zaiets.Markdown.Renderer;

string safeHtml = HtmlSanitizer.Sanitize(untrustedHtml);

API Reference

MarkdownRenderer

Member Description
MarkdownRenderer() Create with default options
MarkdownRenderer(MarkdownOptions) Create with custom options
string Render(string markdown) Convert Markdown to an HTML fragment
string RenderDocument(string, string title, string? extraHead) Wrap in a full HTML5 document

HtmlSanitizer

Member Description
static string Sanitize(string html) Strip XSS vectors from an HTML string

SyntaxHighlighter

Member Description
static string Highlight(string code, string? language) Returns HTML with syntax spans

License

MIT — Copyright © 2025 Vladyslav Zaiets

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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net10.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.0 105 5/3/2026