Question about efficient blocks extraction #224

Closed
opened 2026-01-29 14:30:48 +00:00 by claunia · 1 comment
Owner

Originally created by @JustArchi on GitHub (Sep 21, 2018).

Hello. First of all thank you a lot for your time put into this awesome library, I'm loving it so far.

In my current use case I'm trying to extract a changelog from a markdown text. In particular I need this section in markdown so I can render it as HTML and plain text afterwards. Source markdown has simple structure:

### Paragraph1
irrelevant text

---

### Changelog
- I want this
- And this too

---

### OtherParagraph
and some text I don't want

I've successfully written a function capable of extracting changelog from the document above as:

MarkdownDocument markdownDocument = Markdown.Parse(markdownText);
bool insideChangelog = false;

foreach (Block block in markdownDocument.ToList()) {
	if (!insideChangelog) {
		if (block is HeadingBlock headingBlock && (headingBlock.Inline.FirstChild != null) && headingBlock.Inline.FirstChild is LiteralInline literalInline && (literalInline.Content.ToString() == "Changelog")) {
			insideChangelog = true;
		}

		markdownDocument.Remove(block);
		continue;
	}

	if (block is ThematicBreakBlock) {
		insideChangelog = false;
		markdownDocument.Remove(block);
	}
}

return markdownDocument;

While this solution is perfect for me and it works properly, I'm wondering if it's a proper way to achieve what I want, and if there is no better way to go about this in terms of code or performance. I've tried hard to find out any traces of easier blocks extraction but I didn't find any method that would work for me.

Could you please take a look at above C# code and tell me if it's correct usage of your library for what I want to achieve? Perhaps you'd want to suggest some fixes or entirely different approach. Thank you a lot!

Originally created by @JustArchi on GitHub (Sep 21, 2018). Hello. First of all thank you a lot for your time put into this awesome library, I'm loving it so far. In my current use case I'm trying to extract a changelog from a markdown text. In particular I need this section in markdown so I can render it as HTML and plain text afterwards. Source markdown has simple structure: ```markdown ### Paragraph1 irrelevant text --- ### Changelog - I want this - And this too --- ### OtherParagraph and some text I don't want ``` I've **successfully** written a function capable of extracting changelog from the document above as: ```csharp MarkdownDocument markdownDocument = Markdown.Parse(markdownText); bool insideChangelog = false; foreach (Block block in markdownDocument.ToList()) { if (!insideChangelog) { if (block is HeadingBlock headingBlock && (headingBlock.Inline.FirstChild != null) && headingBlock.Inline.FirstChild is LiteralInline literalInline && (literalInline.Content.ToString() == "Changelog")) { insideChangelog = true; } markdownDocument.Remove(block); continue; } if (block is ThematicBreakBlock) { insideChangelog = false; markdownDocument.Remove(block); } } return markdownDocument; ``` While this solution is perfect for me and it works properly, I'm wondering if it's a proper way to achieve what I want, and if there is no better way to go about this in terms of code or performance. I've tried hard to find out any traces of easier blocks extraction but I didn't find any method that would work for me. Could you please take a look at above C# code and tell me if it's correct usage of your library for what I want to achieve? Perhaps you'd want to suggest some fixes or entirely different approach. Thank you a lot!
claunia added the question label 2026-01-29 14:30:48 +00:00
Author
Owner

@xoofx commented on GitHub (Sep 26, 2018):

Your approach is perfectly fine. But depending on your the amount of blocks to remove, it might be better to remove changelog blocks from the document you are processing and move them to a new MarkdownDocument object. It will create less remove/changes on the original markdown document.

@xoofx commented on GitHub (Sep 26, 2018): Your approach is perfectly fine. But depending on your the amount of blocks to remove, it might be better to remove changelog blocks from the document you are processing and move them to a new `MarkdownDocument` object. It will create less remove/changes on the original markdown document.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/markdig#224