markdig icon indicating copy to clipboard operation
markdig copied to clipboard

Abstract Syntax Tree - Nested objects

Open aviveldan opened this issue 4 years ago • 2 comments

Hi, Would be glad for your help. Not sure if it is a feature request or silly question.

I am trying to work with the AST.

var pipeline = new MarkdownPipelineBuilder().UseAdvancedExtensions().Build();
var result = Markdown.Parse(
@"
# Hello there :)
| Item         | Price     | # In stock |
|--------------|-----------|------------|
| Juicy Apples | 1.99      | *7*        |
| Bananas      | **1.89**  | 5234       |
", pipeline);

Now I want to check if there are nodes within the group of types: bold, italic, header I tried using the Enumerator: var i = result.GetEnumerator(); But the MoveNext method doesn't go through nested nodes, and therefore I need to check the type of the current object: i.Current.GetType() and then use dynamic variable and casting if it is in the list of the objects that can have children objects... I am pretty sure this isn't how I should traverse the tree. Any ideas how to do that efficiently?

Thanks in advance :)

aviveldan avatar Mar 17 '22 17:03 aviveldan

I recommend the document.Descendants() or document.Descendants<SyntaxType>() API.

foreach (EmphasisInline emphasis in document.Descendants<EmphasisInline>())
{
    // ...
}

MihaZupan avatar Mar 17 '22 17:03 MihaZupan

Thanks for the quick response. It works :)

aviveldan avatar Mar 20 '22 08:03 aviveldan