markdig icon indicating copy to clipboard operation
markdig copied to clipboard

Help with parsing nested Blocks

Open BeneHenke opened this issue 1 year ago • 7 comments

Hello,

I'm trying to create an extension that divides my text into sections between every heading. For this I want to find the extend of each section block and then continue to parse the section, appending all the parsed blocks to my section. Is there a block that does something similar already I could look at for guidance? So far I have only had success with manually appending blocks to Containerblocks.

BeneHenke avatar Sep 05 '24 12:09 BeneHenke

You should go over the top-level blocks returned by the MarkdownDocument instance and chunk them from there. You can create your own SectionBlock that derives from ContainerBlock. Otherwise, I'm not sure to understand your issue.

xoofx avatar Sep 07 '24 06:09 xoofx

Thanks for the answer. If i understand your solution right, that is how I do it right now. I was just wondering if I could insert them while parsing the document by putting their renderer first and then telling the parser to parse the span of my section block and inserting everthing to the section instead of the main document.

BeneHenke avatar Sep 07 '24 07:09 BeneHenke

I was just wondering if I could insert them while parsing the document by putting their renderer first and then telling the parser to parse the span of my section block and inserting everthing to the section instead of the main document.

I'm not sure to understand this question as well, sorry 😅

I'm confused by "parsing the document by putting their renderer first", there is no such thing as a rendering involved during the parsing.

If you would like to process the blocks on the fly, this is not possible. You have to parse the document, rework the blocks, and then you can render the updated document (after adding a renderer for the SectionBlock to the render pipeline)

xoofx avatar Sep 07 '24 07:09 xoofx

I'm sorry I meant to write put the section parser first, then find the sections and then parse the sections for other blocks. I basically woke op from the GitHub notification so my brain was not quite there :sweat_smile: . Btw thank you for your amazing library.

BeneHenke avatar Sep 07 '24 07:09 BeneHenke

I'm sorry I meant to write put the section parser first, then find the sections and then parse the sections for other blocks. I basically woke op from the GitHub notification so my brain was not quite there 😅 . Btw thank you for your amazing library.

How is defined a section in markdown in your text? (I thought you were recovering sections from e.g markdown Headings e.g ##)

xoofx avatar Sep 07 '24 07:09 xoofx

I basically made my own heading block as it needed to support the syntax without the space after the # and some other custom syntax. So my thought was to use my current Custom heading block parser to find the beginning of each section and then inserting a heading into my section by default. Then I would search for the next heading of the same or a higher level and end the section there.

BeneHenke avatar Sep 07 '24 07:09 BeneHenke

The problem with this is that I dont know if I can make my parser, parse the content of the section blocks and attach it to the section it is parsing.

BeneHenke avatar Sep 10 '24 07:09 BeneHenke