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 MarkdownFormatter

Remarks

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

NameDescription
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.cs

Formats a document with MarkdownFormattingOptions.Default.

public static MarkdownSyntaxTree Format(MarkdownSyntaxTree tree)
Parameters
tree MarkdownSyntaxTree
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
ArgumentNullException
tree is null.

Format(MarkdownSyntaxTree, MarkdownFormattingOptions?)

MarkdownFormatter.cs

Formats a document.

Parameters
tree MarkdownSyntaxTree
The document.
options MarkdownFormattingOptions?
How to write the document, or null for MarkdownFormattingOptions.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
ArgumentNullException
tree is null.
ArgumentException
A character or the line ending of options is not one that it allows.

FormatText(MarkdownSyntaxTree, MarkdownFormattingOptions?)

MarkdownFormatter.cs

Formats a document and returns its text.

public static string FormatText(MarkdownSyntaxTree tree, MarkdownFormattingOptions? options)

Parameters

tree MarkdownSyntaxTree
The document.
options MarkdownFormattingOptions?
How to write the document, or null for MarkdownFormattingOptions.Default.

Returns

string

Exceptions

ArgumentNullException
tree is null.
ArgumentException
A character or the line ending of options is not one that it allows.