MarkdownSharp & GitHub syntax for C# code
Asked Answered
T

3

38

Is there a way to get MarkdownSharp (I'm using the NuGet package) to handle 'GitHub flavored Markdown (GFM)' and especially syntax highlighting of c# code, which (in GFM) is written like this:

```c#
//my code.....
```

So, if I pass Markdown formatted content to MarkDownSharp, containg a C# code block (as above) I want it to generate syntax highlighted html for that c# code. Any ideas? I know I can use the supported 4 spaces to indicate a code block, but again, I'm seeking a solution for getting it to support GitHub flavored Markdown.

Thackeray answered 21/11, 2011 at 11:22 Comment(1)
I'm trying to solve this same problem, but I specifically need the github table support. I haven't found any existing library that seems to do it. same problem as discussed hereColored
T
11

Here's the result: https://github.com/danielwertheim/kiwi/wiki/Use-with-Asp.Net-MVC

//D

Thackeray answered 3/12, 2011 at 1:27 Comment(0)
C
38

I have made some light modifications to MarkdownSharp that will transform github flavored fenced code blocks

https://github.com/KyleGobel/MarkdownSharp-GithubCodeBlocks

```cs
Console.WriteLine("Fenced code blocks ftw!");
```

Would become

<pre><code class='language-cs'>
Console.WriteLine("Fenced code blocks ftw!");
</code></pre>

It handles the cases I needed to use, there are probably lots of edge cases though, feel free to fork/change/modify/pull request. Markdown sharp has plenty of comments and is all only one file, so it's not too bad to modify.

Collinsia answered 11/5, 2014 at 17:45 Comment(0)
T
11

Here's the result: https://github.com/danielwertheim/kiwi/wiki/Use-with-Asp.Net-MVC

//D

Thackeray answered 3/12, 2011 at 1:27 Comment(0)
B
4

As one can read in this post, GitHub relies on RedCarpet to render Markdown syntax.

However, Vicent Marti (Sundown (ex-Upskirt) and RedCarpet maintainer) states that the syntax highlighting is specifically handled by Pygments, a python library.

Back to your concern, I can think of several options to benefit from syntax highlighting from C#:

  • Try and build a compiled managed version of Pygments source code thanks to IronPython ("IronPython’s Hosting APIs can be used to compile Python scripts into DLLs, console executables, or Windows executables.")
  • Port Pygment to C#
  • Use a different syntax highlighting product (for instance, ColorCode which is used by Codeplex...)

Then either:

  • Fork MarkDownSharp to make it accept plug-ins
  • Similarly to what GitHub does, use the managed syntax highlighting product and post process the Html generated by MarkDownSharp

By the way, as a MarkDown alternative, you might want to consider Moonshine, a managed wrapper on top of Sundown which is said to be "at least 20x faster than MarkdownSharp when run against MarkdownSharp's own benchmark app."

Buckbuckaroo answered 2/12, 2011 at 13:49 Comment(3)
I actually have done it both. Made a spike with Pygments using IronPython and C# but that route brings to many dependencies. The solution was Pre and Post processing with MarkdownSharp and ColorCode. I'm putting it out on GitHub and NuGet.Thackeray
Some of these links are now broken, @nulltoken.Burglarious
@AdamKDean The sundown link is now fixed.Buckbuckaroo

© 2022 - 2024 — McMap. All rights reserved.