MarkdownFormatter Class
Writes a Markdown document in a consistent style, without changing its HTML: one blank line between the blocks, the markers of the containers on every line, the same marker for the bullet lists, ATX headings, fenced code blocks and aligned tables, as MarkdownFormattingOptions configures.
public static class MarkdownFormatterRemarks
The text of the paragraphs, the headings and the cells, the code, and the HTML are kept: only the blocks around them change. The blank lines of the lists are kept, as they make the difference between a tight and a loose list, and the indentation of a line of a paragraph is removed, unless the line would start a block without it. The formatter checks that the formatted text renders as the same HTML, and returns the text as it is when it does not, which can happen with rare combinations of the extensions of Markdig, such as lazy lines that change how a pipe table is read.
Example
var tree = MarkdownSyntaxTree.ParseText("""
Title
=====
* one
* two
3) a
7) b
Code:
var x = 1;
> quote
continued
| a | b |
|-|:-:|
| long cell | x |
""", MarkdownParseOptions.GitHubFlavored);
var formatted = MarkdownFormatter.Format(tree);
Assert.Equal("""
# Title
- one
- two
3) a
4) b
Code:
```
var x = 1;
```
> quote
> continued
| a | b |
| --------- | :-: |
| long cell | x |
""", formatted.GetRoot().ToFullString());
Assert.Equal(MarkdownHtmlRenderer.ToHtml(tree), MarkdownHtmlRenderer.ToHtml(formatted));Methods
| Name | Description |
|---|---|
| Format(MarkdownSyntaxTree) | Formats a document with MarkdownFormattingOptions.Default. |
| Format(MarkdownSyntaxTree, MarkdownFormattingOptions?) | Formats a document. |
| FormatText(MarkdownSyntaxTree, MarkdownFormattingOptions?) | Formats a document and returns its text. |
Format overloads
Format(MarkdownSyntaxTree)
MarkdownFormatter.csFormats a document with MarkdownFormattingOptions.Default.
public static MarkdownSyntaxTree Format(MarkdownSyntaxTree tree)Parameters
treeMarkdownSyntaxTree
Returns
MarkdownSyntaxTree The tree of the formatted text, parsed with the options of tree, or tree when its text is already formatted or when the formatted text would not render as the same HTML.
Exceptions
ArgumentNullExceptiontreeisnull.
Format(MarkdownSyntaxTree, MarkdownFormattingOptions?)
MarkdownFormatter.csFormats a document.
public static MarkdownSyntaxTree Format(MarkdownSyntaxTree tree, MarkdownFormattingOptions? options)Parameters
treeMarkdownSyntaxTree- The document.
optionsMarkdownFormattingOptions?- How to write the document, or
nullforMarkdownFormattingOptions.Default.
Returns
MarkdownSyntaxTree The tree of the formatted text, parsed with the options of tree, or tree when its text is already formatted or when the formatted text would not render as the same HTML.
Exceptions
ArgumentNullExceptiontreeisnull.ArgumentException- A character or the line ending of
optionsis not one that it allows.
FormatText(MarkdownSyntaxTree, MarkdownFormattingOptions?)
MarkdownFormatter.csFormats a document and returns its text.
public static string FormatText(MarkdownSyntaxTree tree, MarkdownFormattingOptions? options)Parameters
treeMarkdownSyntaxTree- The document.
optionsMarkdownFormattingOptions?- How to write the document, or
nullforMarkdownFormattingOptions.Default.
Returns
Exceptions
ArgumentNullExceptiontreeisnull.ArgumentException- A character or the line ending of
optionsis not one that it allows.