MarkdownSyntaxTree Class

A parsed Markdown document.

public sealed class MarkdownSyntaxTree : SyntaxTree

Remarks

Parsing never throws: whatever the text says, the tree reproduces it exactly. The characters that no token covers, such as the markers of a block quote on its continuation lines, the indentation and the line endings, are trivia.

Inheritance
SyntaxTree MarkdownSyntaxTree

Properties

NameDescription
FilePath
OptionsGets the options the text was parsed with.
public override string? FilePath { get; }

Property value

string?

No documentation is available.

Gets the options the text was parsed with.

public MarkdownParseOptions Options { get; }

Property value

MarkdownParseOptions

Methods

NameDescription
GetRoot()Gets the document node.
GetText()
InsertBlocksAfter(MarkdownBlockSyntax, string)Inserts blocks after block, in its container.
InsertBlocksBefore(MarkdownBlockSyntax, string)Inserts blocks before block, in its container.
ParseText(SourceText, MarkdownParseOptions?, string?)Parses text.
ParseText(SourceText, string?)Parses text as CommonMark.
ParseText(string, MarkdownParseOptions?, string?)Parses text.
ParseText(string, string?)Parses text as CommonMark.
RemoveBlock(MarkdownBlockSyntax)Removes block and its lines.
ReplaceBlock(MarkdownBlockSyntax, string)Replaces block with other blocks, in its container.
WithGenericAttributes(MarkdownBlockSyntax, MarkdownGenericAttributes?)Replaces the generic attributes of block.

Gets the document node.

public MarkdownDocumentSyntax GetRoot()
public override SourceText GetText()

Returns

SourceText

No documentation is available.

InsertBlocksAfter(MarkdownBlockSyntax, string)

MarkdownSyntaxTree.cs

Inserts blocks after block, in its container.

public MarkdownSyntaxTree InsertBlocksAfter(MarkdownBlockSyntax block, string markdown)

Parameters

block MarkdownBlockSyntax
The block of this tree to insert after.
markdown string
The blocks to insert, as Markdown that is not in a container: the markers of the containers of block are added to each of its lines.

Returns

MarkdownSyntaxTree A tree parsed from the new text.

Exceptions

ArgumentNullException
block or markdown is null.
ArgumentException
block is not a block of this tree.

Remarks

The lines are inserted after the last line of the block. Separate the text from the block with a blank line when the two would otherwise be read as one, such as two paragraphs: a line of text after a paragraph continues it. A fenced block that the end of its container closes, such as a code block without its closing fence, gets a closing fence first.

InsertBlocksBefore(MarkdownBlockSyntax, string)

MarkdownSyntaxTree.cs

Inserts blocks before block, in its container.

public MarkdownSyntaxTree InsertBlocksBefore(MarkdownBlockSyntax block, string markdown)

Parameters

block MarkdownBlockSyntax
The block of this tree to insert before.
markdown string
The blocks to insert, as Markdown that is not in a container: the markers of the containers of block are added to each of its lines.

Returns

MarkdownSyntaxTree A tree parsed from the new text.

Exceptions

ArgumentNullException
block or markdown is null.
ArgumentException
block is not a block of this tree.

Remarks

The text is inserted as it is: separate it from the block with a blank line when the two would otherwise be read as one, such as two paragraphs. In a list, a blank line makes the list loose. To change several blocks, use a MarkdownSyntaxTreeEditor, which parses the text once.

ParseText overloads

ParseText(SourceText, MarkdownParseOptions?, string?)

MarkdownSyntaxTree.cs

Parses text.

public static MarkdownSyntaxTree ParseText(SourceText text, MarkdownParseOptions? options, string? path = null)
Parameters
text SourceText
The Markdown to read.
options MarkdownParseOptions?
How to read it, or null for MarkdownParseOptions.Default.
path string?
Where the text came from.
Returns

MarkdownSyntaxTree

Exceptions
ArgumentNullException
text is null.

ParseText(SourceText, string?)

MarkdownSyntaxTree.cs

Parses text as CommonMark.

public static MarkdownSyntaxTree ParseText(SourceText text, string? path = null)
Parameters
text SourceText
path string?
Returns

MarkdownSyntaxTree

Exceptions
ArgumentNullException
text is null.

ParseText(string, MarkdownParseOptions?, string?)

MarkdownSyntaxTree.cs

Parses text.

public static MarkdownSyntaxTree ParseText(string text, MarkdownParseOptions? options, string? path = null)
Parameters
text string
The Markdown to read.
options MarkdownParseOptions?
How to read it, or null for MarkdownParseOptions.Default.
path string?
Where the text came from.
Returns

MarkdownSyntaxTree

Exceptions
ArgumentNullException
text is null.

ParseText(string, string?)

MarkdownSyntaxTree.cs

Parses text as CommonMark.

public static MarkdownSyntaxTree ParseText(string text, string? path = null)
Parameters
text string
path string?
Returns

MarkdownSyntaxTree

Exceptions
ArgumentNullException
text is null.

RemoveBlock(MarkdownBlockSyntax)

MarkdownSyntaxTree.cs

Removes block and its lines.

public MarkdownSyntaxTree RemoveBlock(MarkdownBlockSyntax block)

Parameters

block MarkdownBlockSyntax

Returns

MarkdownSyntaxTree A tree parsed from the new text.

Exceptions

ArgumentNullException
block is null.
ArgumentException
block is not a block of this tree.

Remarks

The lines of the block are removed, with the markers of the containers on them. A blank line, with the markers of the containers, replaces them when the lines around them are not blank, so that the blocks around the removed one are not read as one, such as two paragraphs, or a paragraph and a thematic break that would make it a setext heading.

When the block starts on the line of the marker of its list item, the marker stays, and the next block of the item moves to this line: removing the only block of an item leaves the item empty.

ReplaceBlock(MarkdownBlockSyntax, string)

MarkdownSyntaxTree.cs

Replaces block with other blocks, in its container.

public MarkdownSyntaxTree ReplaceBlock(MarkdownBlockSyntax block, string markdown)

Parameters

block MarkdownBlockSyntax
The block of this tree to replace.
markdown string
The blocks that replace it, as Markdown that is not in a container: the markers of the containers of block are added to each of its lines.

Returns

MarkdownSyntaxTree A tree parsed from the new text.

Exceptions

ArgumentNullException
block or markdown is null.
ArgumentException
block is not a block of this tree.

Remarks

The text replaces the lines of the block, from where it starts, after the markers of its line, to the end of its last line: the indentation of an indented code block is replaced too. The line ending after the block stays, and a line ending at the end of markdown is ignored. Separate the text from the blocks around it with a blank line when they would otherwise be read as one, such as two paragraphs.

WithGenericAttributes(MarkdownBlockSyntax, MarkdownGenericAttributes?)

MarkdownSyntaxTree.cs

Replaces the generic attributes of block.

public MarkdownSyntaxTree WithGenericAttributes(MarkdownBlockSyntax block, MarkdownGenericAttributes? attributes)

Parameters

block MarkdownBlockSyntax
The block of this tree whose attributes are replaced.
attributes MarkdownGenericAttributes?
The attributes of the block, or null to remove them.

Returns

MarkdownSyntaxTree A tree parsed from the new text, where MarkdownBlockSyntax.GetGenericAttributes of the block returns attributes.

Exceptions

ArgumentNullException
block is null.
ArgumentException
block is not a block of this tree, or attributes cannot be written, such as a class with whitespace or an attribute name that starts with a digit.
InvalidOperationException
The tree is parsed without MarkdownExtensions.GenericAttributes, or the block cannot have these attributes, such as an HTML block, a list item or a footnote definition, which Markdig gives no attributes to.

Remarks

Every {...} that is for the block is removed, and the attributes are written as Markdig reads them: at the end of the first line of a heading, of a fenced code block, of a math block and of a custom container, at the end of the last line of a paragraph, and on a line before the other blocks, such as a list, a block quote or a table, with the markers of their containers. A blank line separates this line from the paragraph before it, or from a thematic break that would make it a setext heading.

The text is parsed again, and checked to give the attributes to the block and to keep its text. To change the attributes of several blocks, use MarkdownSyntaxTreeEditor.SetGenericAttributes, which parses the text once, or, in a MarkdownSyntaxRewriter, MarkdownBlockSyntax.WithGenericAttributes, which changes a block whose attributes are written in it without parsing the document again.