MarkupString.Html 2.2.0

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

MarkupString.Html

Raw HTML tag markup for MarkupString: an anchor, a <pre>, a <div class="..."> — carried as a layer over a span of text and written as itself in the Html, Pueblo and Mxp formats.

A tag is written the same in all three, so use it for tags the three spell alike. A command link is not one of them — Pueblo writes <a xch_cmd>, MXP <send href> — so build that with MarkupString.Ansi's AnsiMarkup.Create(linkUrl: ..., linkKind: LinkKind.Command), which each format writes in its own dialect.

Where a tag has no meaning — a terminal — it does not vanish silently: b, strong, i, em, u, s, strike and del fold into the run's terminal styling through MarkupString.Ansi, and any other tag leaves its body untouched.

dotnet add package MarkupString.Html

Usage

using MarkupString;
using MarkupString.Ansi;
using MarkupString.Html;

MarkupRegistry.Default = MarkupRegistry.Empty.WithAnsi().WithHtml();

var text = MarkupText.Wrap(
  HtmlMarkup.Tag("a", new HtmlAttribute("href", "https://example.org/")),
  MarkupText.Wrap(AnsiCodeParser.Parse("hr"), "north"));

text.Render(MarkupFormat.Html);   // <a href="https://example.org/"><span style="color: #ff5555">north</span></a>
text.Render(MarkupFormat.Ansi);   // \e[1;31mnorth\e[0m
text.Render(MarkupFormat.Plain);  // north

var json = MarkupTextSerializer.Serialize(text);
var back = MarkupTextSerializer.Deserialize(json);

WithAnsi() must be applied as well: the terminal fold for b/i/u/s comes from that package.

Untrusted tags

HtmlMarkup.Create(name, attributes) writes both exactly as given. For anything you did not write yourself:

  • HtmlMarkup.Tag(name, params attributes) checks the name and encodes every value.
  • HtmlTagPolicy.TryCreate(name, rawAttributes, out markup) reads a raw attribute string and keeps only what the policy allows, re-encoded.
  • WithHtml(policy) holds every tag rendered in the Html format to that policy as it is written, so markup that arrived deserialised or built with Create is held to it too.

The library ships no security posture. HtmlTagPolicy.WellFormed allows any well-formed tag; what is safe in your application is your decision, and a policy is a record you narrow with with:

var policy = HtmlTagPolicy.WellFormed with
{
  AllowedTags = new HashSet<string> { "a", "b", "i", "u", "font", "pre", "span", "img" },
  AllowedAttributes = new HashSet<string> { "href", "src", "alt", "title", "color", "face", "size" },
  // Checked as addresses. HtmlTagPolicy.AddressAttributes is the list HTML gives an address to;
  // nothing applies it for you.
  UrlAttributes = HtmlTagPolicy.AddressAttributes,
  OnViolation = HtmlAttributeViolation.DropAllAttributes,
};

MarkupRegistry.Default = MarkupRegistry.Empty.WithAnsi().WithHtml(policy);

Styling

The HTML emitters write ms-* classes for the text attributes that have a fixed rendering, and inline style for colours, which are open-ended. Every one of those classes comes from MarkupString.Ansi, so the stylesheet does too: include AnsiCss.Fixed once per page, or copy its rules into your own sheet.

Extension points

  • IMarkup + IMarkupEmitter — register an emitter for HtmlMarkup in a format of your own, or replace this package's by adding yours to the registry afterwards.
  • IMarkupCodecHtmlMarkupCodec is the wire shape under kind "html".
  • IAnsiStyleSourceHtmlMarkup implements it; that is how a tag becomes terminal styling.

AOT and trimming

IsAotCompatible; no reflection, no dynamic code. Registration is an explicit WithHtml() call.

Licence

Apache-2.0. Source, guides and issues: SharpMUSH/MarkupString.

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.

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
2.2.0 0 9/19/2026
2.1.0 2,233 9/8/2026
2.0.0 477 9/7/2026
1.1.0 476 9/7/2026
1.0.0 99 9/7/2026