MarkdownEscaper Class

Provides methods to write text into Markdown, so that it is parsed back to the same text instead of being interpreted as Markdown syntax.

public static class MarkdownEscaper

Example

// Escape the text so that Markdown displays it as is: every ASCII punctuation character is escaped
Assert.Equal(@"\*Note\* \[docs\]\!", MarkdownEscaper.Escape("*Note* [docs]!"));

// The delimiters of code are longer than the runs of backticks of the content
Assert.Equal("``a`b``", MarkdownEscaper.CreateCodeSpan("a`b"));
Assert.Equal("`` `code` ``", MarkdownEscaper.CreateCodeSpan("`code`"));
Assert.Equal("```csharp\nvar a = 1;\n```\n", MarkdownEscaper.CreateFencedCodeBlock("var a = 1;", "csharp"));

// Build a link from untrusted parts
var text = MarkdownEscaper.Escape("Docs [v2]");
var destination = MarkdownEscaper.EscapeLinkDestination("https://example.com/a b");
var title = MarkdownEscaper.EscapeLinkTitle("Say \"hi\"");
Assert.Equal("<https://example.com/a b>", destination);
Assert.Equal("\"Say \\\"hi\\\"\"", title);

var html = MarkdownConverter.ToHtml($"[{text}]({destination} {title})");
Assert.Equal("<p><a href=\"https://example.com/a%20b\" title=\"Say &quot;hi&quot;\">Docs [v2]</a></p>\n", html);

// The label of a reference link
Assert.Equal(@"see \[1\]", MarkdownEscaper.EscapeLinkLabel("see [1]"));

// A cell of a pipe table cannot contain a pipe or a line ending
Assert.Equal(@"a \| b c", MarkdownEscaper.EscapeTableCell("a | b\nc"));

Methods

NameDescription
CreateCodeSpan(string)Creates an inline code span that contains the specified text.
CreateFencedCodeBlock(string, string?)Creates a fenced code block that contains the specified code.
Escape(string)Escapes every ASCII punctuation character with a backslash, so that the text is displayed as is.
EscapeLinkDestination(string)Escapes a URL, so that it can be used as the destination of a link or an image: [text](destination).
EscapeLinkLabel(string)Escapes a link label, so that it can be used in a reference link and its link reference definition: [text][label] and [label]: destination.
EscapeLinkTitle(string)Escapes a link title and encloses it in double quotes, so that it can be used as the title of a link, an image, or a link reference definition: [text](destination "title").
EscapeTableCell(string)Escapes the text of a cell of a pipe table, so that it is displayed as is.

CreateCodeSpan(string)

MarkdownEscaper.cs

Creates an inline code span that contains the specified text.

public static string CreateCodeSpan(string content)

Parameters

content string
The content of the code span.

Returns

string The code span, including its backtick delimiters.

Exceptions

ArgumentException
content is empty: a code span cannot be empty.

Remarks

The delimiters are one backtick longer than the longest run of backticks of the content. The parser converts the line endings of a code span to spaces, and a blank line ends the paragraph.

CreateFencedCodeBlock(string, string?)

MarkdownEscaper.cs

Creates a fenced code block that contains the specified code.

public static string CreateFencedCodeBlock(string code, string? infoString = null)

Parameters

code string
The code. The lines are written as is.
infoString string?
The info string, such as the language of the code (csharp), or null for none.

Returns

string The fenced code block, ending with a line ending.

Exceptions

ArgumentException
infoString contains a line ending.

Remarks

The fence is at least three backticks, and longer than any line of the code that would close it. A line ending at the end of code is optional: it does not add an empty line to the block.

Escape(string)

MarkdownEscaper.cs

Escapes every ASCII punctuation character with a backslash, so that the text is displayed as is.

public static string Escape(string text)

Parameters

text string
The text to escape.

Returns

string The escaped text.

Remarks

The whitespace at the start and at the end of a line is not preserved, and a blank line starts a new paragraph.

EscapeLinkDestination(string)

MarkdownEscaper.cs

Escapes a URL, so that it can be used as the destination of a link or an image: [text](destination).

public static string EscapeLinkDestination(string url)

Parameters

url string
The URL.

Returns

string The escaped destination. It is enclosed in angle brackets when the URL is empty or contains spaces.

EscapeLinkLabel(string)

MarkdownEscaper.cs

Escapes a link label, so that it can be used in a reference link and its link reference definition: [text][label] and [label]: destination.

public static string EscapeLinkLabel(string label)

Parameters

label string
The label.

Returns

string The escaped label, without the brackets.

Exceptions

ArgumentException
label is blank, or longer than 999 characters once escaped.

Remarks

The parser matches the labels case-insensitively, and collapses consecutive whitespace.

EscapeLinkTitle(string)

MarkdownEscaper.cs

Escapes a link title and encloses it in double quotes, so that it can be used as the title of a link, an image, or a link reference definition: [text](destination "title").

public static string EscapeLinkTitle(string title)

Parameters

title string
The title.

Returns

string The escaped title, enclosed in double quotes.

EscapeTableCell(string)

MarkdownEscaper.cs

Escapes the text of a cell of a pipe table, so that it is displayed as is.

public static string EscapeTableCell(string text)

Parameters

text string
The text of the cell.

Returns

string The escaped text.

Remarks

The text is escaped as with MarkdownEscaper.Escape, which also escapes the pipes. A row of a pipe table cannot span several lines, so the line endings are replaced by spaces. To write a code span in a cell, replace the pipes of the result of MarkdownEscaper.CreateCodeSpan with \|.