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 MarkdownEscaperExample
// 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 "hi"\">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
| Name | Description |
|---|---|
| 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.csCreates an inline code span that contains the specified text.
Parameters
contentstring- The content of the code span.
Returns
string The code span, including its backtick delimiters.
Exceptions
ArgumentExceptioncontentis 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.csCreates a fenced code block that contains the specified code.
Parameters
codestring- The code. The lines are written as is.
infoStringstring?- The info string, such as the language of the code (
csharp), ornullfor none.
Returns
string The fenced code block, ending with a line ending.
Exceptions
ArgumentExceptioninfoStringcontains 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.csEscapes every ASCII punctuation character with a backslash, so that the text is displayed as is.
EscapeLinkDestination(string)
MarkdownEscaper.csEscapeLinkLabel(string)
MarkdownEscaper.csEscapes a link label, so that it can be used in a reference link and its link reference definition: [text][label] and [label]: destination.
Parameters
labelstring- The label.
Returns
string The escaped label, without the brackets.
Exceptions
ArgumentExceptionlabelis blank, or longer than 999 characters once escaped.
Remarks
The parser matches the labels case-insensitively, and collapses consecutive whitespace.
EscapeLinkTitle(string)
MarkdownEscaper.csEscapes 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)
MarkdownEscaper.csEscapes the text of a cell of a pipe table, so that it is displayed as is.
Parameters
textstring- 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 \|.