Convert Markdown to HTML in .NET

11,165

Solution 1

Markdown Sharp is what the site uses and is available on NuGet.

Solution 2

Another implementation that seems to be gaining ground is MarkdownDeep

This is a full implementation for both C# and JavaScript. The MarkdownHelper on Nuget is using MarkdownDeep now instead of MarkdownSharp.

I've used both and MarkdownDeep seems to be more fully functional and having the JavaScript version is great for quick client side setups.

Solution 3

TL;DR: now it's 2021 use markdig

I just came across this questions and the answers are all quite old. It appears that implementations based on commonmark are the suggested way to go now. Implementations for lots of languages (including C#) can be found here

Solution 4

Check out Markdown Sharp. It's the open source library that resulted from the development of Stack Overflow and is much more robust/actively developed than markdown.net.

Solution 5

Markdown Sharp

Markdown Sharp isn't that bad code-wise either as John Leidegren noted, it's just not that easy to comment-out regular expressions or manage complex projects, w in in cleanest-OOP. It is definitely very fast and well-supported. I haven't found a Markup-parser based approach yet. Here is an example:

        pattern = string.Format(@"
            (?:
                (?<=\n\n)           # Starting after a blank line
                |                   # or
                \A\n?               # the beginning of the doc
            )
            (                       # save in $1
                [ ]{{0, {0}}}
                <(hr)               # start tag = $2
                \b                  # word break
                ([^<>])*?           #
                /?>                 # the matching end tag
                [ \t]*
                (?=\n{{2,}}|\Z)     # followed by a blank line or end of document
            )", tabWidth - 1);
        text = Regex.Replace(text, pattern, new MatchEvaluator(HtmlEvaluator), RegexOptions.IgnorePatternWhitespace);
Share:
11,165

Related videos on Youtube

Troj
Author by

Troj

I'm a jack of most trades residing in Sweden and usually involved with full-stack web development technologies. I work for tretton37 as a contractor, my list of clients includes among others Sony and IKEA. I dabble in open source software and have many projects in my Github repository and my Bitbucket repository, among many: RefluxJS - Library for uni-directional data flows, inspired by Facebook's Flux In the little free time that I have, all kinds of stuff happen such as drawing pretty pictures, perform ball juggling, play a guitar, hack on games, and solve a Rubik's cube.

Updated on April 16, 2022

Comments

  • Troj
    Troj about 2 years

    How can I convert markdown into html in .NET?

    var markdown = "Some **bold** text";
    var output = ConvertMarkdownToHtml(markdown)
    // Output: <p>Some <strong>bold</strong> text</p>
    

    I have Markdown text stored in a database that needs to be converted to html when it is displayed.

    I know about StackOverflow's WMD Editor (now PageDown), but that only converts client-side.

    • KyleMit
      KyleMit over 4 years
      Should be re-opened as it's the only question in all of Stack Overflow on converting Markdown to HTML in .NET / C#. Although the problem scope is probably best left to an external library, the question itself no longer seeks one out by default and instead serves as wayfinding to which libs solve that problem and how to implement them. Should remain open to capture changes over time
    • KyleMit
      KyleMit over 4 years
      For a well maintained package in 2019, consider lunet-io / markdig
  • Drew Noakes
    Drew Noakes over 13 years
    SO doesn't use Markdown.NET (at least not any more -- I've no idea if it used to). SO uses Markdown Sharp.
  • Earlz
    Earlz almost 11 years
    Also, MarkdownDeep boasts a significant performance increase compared to MarkdownSharp. This does matter. For instance, I recently discovered that a huge bottleneck in my blog was markdown transforms. It was taking something like 20ms to transform a small blog post, and retrieving the actual post from the database only took 5ms!