MarkdownSyntaxTree Class
A parsed Markdown document.
public sealed class MarkdownSyntaxTree : SyntaxTreeRemarks
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
SyntaxTreeMarkdownSyntaxTree
Properties
| Name | Description |
|---|---|
| FilePath | |
| Options | Gets the options the text was parsed with. |
FilePath
MarkdownSyntaxTree.csOptions
MarkdownSyntaxTree.csGets the options the text was parsed with.
public MarkdownParseOptions Options { get; }Property value
Methods
| Name | Description |
|---|---|
| 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. |
GetRoot()
MarkdownSyntaxTree.csGetText()
MarkdownSyntaxTree.csInsertBlocksAfter(MarkdownBlockSyntax, string)
MarkdownSyntaxTree.csInserts blocks after block, in its container.
public MarkdownSyntaxTree InsertBlocksAfter(MarkdownBlockSyntax block, string markdown)Parameters
blockMarkdownBlockSyntax- The block of this tree to insert after.
markdownstring- The blocks to insert, as Markdown that is not in a container: the markers of the containers of
blockare added to each of its lines.
Returns
MarkdownSyntaxTree A tree parsed from the new text.
Exceptions
ArgumentNullExceptionblockormarkdownisnull.ArgumentExceptionblockis 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.csInserts blocks before block, in its container.
public MarkdownSyntaxTree InsertBlocksBefore(MarkdownBlockSyntax block, string markdown)Parameters
blockMarkdownBlockSyntax- The block of this tree to insert before.
markdownstring- The blocks to insert, as Markdown that is not in a container: the markers of the containers of
blockare added to each of its lines.
Returns
MarkdownSyntaxTree A tree parsed from the new text.
Exceptions
ArgumentNullExceptionblockormarkdownisnull.ArgumentExceptionblockis 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.csParses text.
public static MarkdownSyntaxTree ParseText(SourceText text, MarkdownParseOptions? options, string? path = null)Parameters
textSourceText- The Markdown to read.
optionsMarkdownParseOptions?- How to read it, or
nullforMarkdownParseOptions.Default. pathstring?- Where the text came from.
Returns
Exceptions
ArgumentNullExceptiontextisnull.
ParseText(SourceText, string?)
MarkdownSyntaxTree.csParses text as CommonMark.
public static MarkdownSyntaxTree ParseText(SourceText text, string? path = null)ParseText(string, MarkdownParseOptions?, string?)
MarkdownSyntaxTree.csParses text.
public static MarkdownSyntaxTree ParseText(string text, MarkdownParseOptions? options, string? path = null)Parameters
textstring- The Markdown to read.
optionsMarkdownParseOptions?- How to read it, or
nullforMarkdownParseOptions.Default. pathstring?- Where the text came from.
Returns
Exceptions
ArgumentNullExceptiontextisnull.
ParseText(string, string?)
MarkdownSyntaxTree.csParses text as CommonMark.
public static MarkdownSyntaxTree ParseText(string text, string? path = null)RemoveBlock(MarkdownBlockSyntax)
MarkdownSyntaxTree.csRemoves block and its lines.
public MarkdownSyntaxTree RemoveBlock(MarkdownBlockSyntax block)Parameters
blockMarkdownBlockSyntax
Returns
MarkdownSyntaxTree A tree parsed from the new text.
Exceptions
ArgumentNullExceptionblockisnull.ArgumentExceptionblockis 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.csReplaces block with other blocks, in its container.
public MarkdownSyntaxTree ReplaceBlock(MarkdownBlockSyntax block, string markdown)Parameters
blockMarkdownBlockSyntax- The block of this tree to replace.
markdownstring- The blocks that replace it, as Markdown that is not in a container: the markers of the containers of
blockare added to each of its lines.
Returns
MarkdownSyntaxTree A tree parsed from the new text.
Exceptions
ArgumentNullExceptionblockormarkdownisnull.ArgumentExceptionblockis 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.csReplaces the generic attributes of block.
public MarkdownSyntaxTree WithGenericAttributes(MarkdownBlockSyntax block, MarkdownGenericAttributes? attributes)Parameters
blockMarkdownBlockSyntax- The block of this tree whose attributes are replaced.
attributesMarkdownGenericAttributes?- The attributes of the block, or
nullto remove them.
Returns
MarkdownSyntaxTree A tree parsed from the new text, where MarkdownBlockSyntax.GetGenericAttributes of the block returns attributes.
Exceptions
ArgumentNullExceptionblockisnull.ArgumentExceptionblockis not a block of this tree, orattributescannot 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.