Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove Markdown tags from string

I have a string which has Markdown tags embedded inside it. I do not want to encode the Markdown as anything else, I just want to rip out all of the tags.

How can I do this quickly? I need to do this as part of a batch processing job which processes around 5 million pieces of text, so speed is very important.

I looked at MarkdownSharp, and using Transform, but I'm not sure it's the best way of doing this. I just want plaintext output, with no tags inside. I'm even considering a regex removal, but I'm not sure what the most performant option would be.

like image 940
Faraday Avatar asked Jul 07 '26 01:07

Faraday


2 Answers

You could probably use MarkdownSharp or any other similar library (I recommend Strike, since it is surprisingly fast!) to convert the Markdown to Html and then use HtmlAgilityPack to extract the text.

A faster option, but more work for you, would be to modify an existing Markdown parser to produce plain text instead.

like image 89
Rune Grimstad Avatar answered Jul 08 '26 13:07

Rune Grimstad


The solution was a bit hard to get from the comments, but this works for .NET 6:

  1. Install Markdown Deep from NuGet. I needed something for .NET 6 so I used the Core version https://www.nuget.org/packages/MarkdownDeep.NET.Core/
  2. Create a Markdown object:
using MarkdownDeep;
var markdownRemover = new Markdown()
{
    SummaryLength = -1
};
  1. Strip the markdown from the text:
var plainText = markdownRemover.Transform(mdText);
like image 44
Kees C. Bakker Avatar answered Jul 08 '26 15:07

Kees C. Bakker



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!