Markdownsharp



  1. Golang CloneOptions.FetchOptions - 2 examples found. These are the top rated real world Golang examples of github.com/libgit2/git2go.CloneOptions.FetchOptions extracted from open source projects.
  2. Miscellaneous,.NET tools category.

Update: I changed from using MarkdownSharp to MarkdownDeep.NET and pushed the helper to NuGet to make it easier to use. Sure, it's only a few lines of code, but this way one command will import the MarkdownDeep.NET library and add the namespace to your views Web.config easily. It'll also make updates much simpler if you use it in many places :-)

There are definitely issues with C# to VB comments in RegEx.

One of the goals for my new blogging engine in ASP.NET MVC/Razor is to store all of my articles using Markdown instead of HTML. If you don't know what Markdown is, go read. I'll wait... If you've used StackOverflow much, you're probably already familiar with it, even if you didn't know it by name.

Since I first started working on content management systems many moons ago, there was something I disliked about using HTML to store content. Even with strides towards semantic markup, HTML felt a little clunky as a storage format. I wanted something better.

I tried many things to 'solve' this problem, including creating my own XML markup for content, which I could transform into HTML for displaying on the website (or plain text, if required). This was totally stupid, and I permit for you to laugh at me (just this once, mind). My XML was 95% the same as HTML. Duh. It made more sense to just store HTML, and convert that into other formats if required, but this still didn't feel right.

As years went by, I really learned to hate rich HTML editors. They usually generate crap markup (why are there 50 divs wrapped around my content?!), and often destroyed relative paths in links and images. They also didn't work on mobile versions of most browsers, meaning I couldn't even fix simple typos when away from my computer. I was sure there had to be a better way, but unfortunately at the time of building this blog, I couldn't find one. As a result, all of the articles currently on this blog are hand-typed HTML in a <textarea>! This has an advantage of working on my iPad, but it kinda sucks. It makes me cry a little inside every time I create a post.

Enter, Markdown

Over the past few years, I've been using StackOverflow quite a lot and I've really grown to love the editor they use, which uses Markdown for formatting. The more I use it, the more I think it's what I should be using for my blog. Not only is the markup much simpler than hand-crafting HTML, it's 100% human-readable in text format and also editable on my iPad!

To test out how this could work, I decided to knock up a quick prototype in ASP.NET MVC/Razor. The code used to transform StackOverflow's content server-side was opensourced under the name MarkdownSharp, so I grabbed that. The project includes a lot of files, but these are mostly tests. The file Markdown.cs is all you need to transform text. I didn't expect it to be difficult, but it still turned out to be far simpler and far more elegant than I was expecting!

Creating an HtmlHelper Extension Method

The best way to use Markdown in an MVC view would be by creating an extension method on the Html property (an instance of HtmlHelper of a view. This will allow us to transform text very easily, like this:

To do this, we need to create an extension method on HtmlHelper. We do this by using the class we'd like to extend as the first argument, but prefixing it with the keyword 'this':

It's important to note that you need to wrap the resulting string in an MvcHtmlString (or other IHtmlString) to stop the framework from HtmlEncoding the output, since in this case the result is deliberately HTML that should be rendered by the browser.

In order to be able to use this method in our views without having to import namespaces, we can add the namespace to the web.config file, like this:

Now in our view, we'll see our Markdown method in the intellisense once we've typed 'Html.', allowing us to pass in the string we'd like to transform.

In addition to transforming data in your model, you could also pass a string in directly if you wish to keep your markup a little clearer:

Unfortunately if you add indenting you'll get strange issues (since indenting means something in Markdown), but for a simple hard-coded list of links/etc. this might be more elegant than ahrd-coding UL/Hyperlinks!

Introduction

Long time ago I had posted a benchmarks comparing CppCMS based blog and PHP based one.

I wanted to compare real life applications with each other. For a long time I had been searching for similar applications in several technologies doing very similar jobs in leading technologies: PHP, Asp.Net and Java/JSP. The last two were particularly important as they use static type system and 'compiled' languages as C# and Java that are known to be faster then other dynamic typed languages like PHP, Python, Ruby and Perl popular in web development.

Setup

Unfortunately I had failed to find such application, so finally I decided to write something representative and small on my own an application with following requirements:

  1. Uses simple time-out based page caching
  2. Uses MySQL and the database and keeps open connections in pool.
  3. For each request access to database (if page is not cached), fetches the page content and comments for 'sample article' in blog.
  4. Converts text to HTML using a markdown filter and displays it on page.

I used following technologies:

  • CppCMS:

    Version: 0.99.3
    MySQL Connection: dbixx/libdbi library using libmysqlclient
    Markdown library: discount
    Connection: internal HTTP server

  • PHP

    Version: 5.2.6
    MySQL Connection: internal driver
    Markdown library: PHP-Makrdown
    Connection: Lighttpd 1.4.19 + FastCGI
    Bytecode Cache: XCache

  • Asp.Net/Mono

    Version: 2.6.7
    MySQL Connection: Connector/Net
    Markdown library: MarkdownSharp
    Connection: internal HTTP server XSP (found to be much faster than fastcgi server)

  • JSP/Tomcat

    Version - Tomcat: 6.0.18
    Version - Java: Sun Java 1.6.0_12
    MySQL Connection: Connector/J
    Markdown library: jmd-0.8.1
    Caching: oscache 2.4.1
    Connection: HTTP

I tested following parameters:

  • Pages per seconds generation for different cache hit/miss ratio: stating from 0% miss ratio up to 100% miss ratio.
  • Memory usage

For each test the application was 'warmed up' with 100 requests to fill the cache, and then 1000 request with max concurrency of 5 request are done, while certain percent of them is new pages and the other are taken from 'warmed up' once.

Github Markdown Table

Notes:

I used the fastest Markdown implementation I had found.

C# implementation is the same one that http://stackoverflow.com uses - it is actually heavily optimized implementation based on older C# implementation

Stack Overflow Markdown

The Java implementation is based on the above C# and the fastest one I had found.

Discount is the fastest C implementation of markdown that I had found.

Markdownsharp

Results:

Summary

Markdownsharp
  1. C#, Java and PHP implementation behave very similarly and without significant differences.
  2. The memory usage of Java/Tomcat and Mono/Asp.Net was significantly higher - up one or two orders of magnitude in comparison to CppCMS and PHP
  3. Surprisingly PHP behaves very well, in comparison to 'compiled' languages like Java and C#.

Revisiting

After doing some profiling it was clear that C implementation of Markdown was significantly faster then all other implementations. So I decided tocreate my own mini-markdown that make some basic handing of titles, lists, paragraphs and quotes at one level only. That is very simple syntax but implemented similarly in all 4 languages using same algorithm.

The results were following:

The difference between CppCMS and other implementations was still significant but still much smaller then the difference between real markdown implementation. So the performance difference was less dramatic.

2nd Revision

And in the last revision I decided not to use any text filters by fetchready HTML formatted content from DB and display it on the web as is.

Such comparison actually profile the most basic stuff:

  1. Caching
  2. SQL Connection
  3. Request/Response handling

And would ignore hundreds lines of code used in any web applications responsiblefor the actual business logic.

Conclusions

  1. Using C++ with CppCMS provides significant performance gains in developing web applications even in very basic case.
  2. The performance is effected not only by the framework itself but also by many other libraries that are in use. Using highly optimized C and C++ librariesmay give significant performance gain in many cases.
  3. Such called 'jit-compiled' languages as C# and Java and the frameworks based on the use significant amount of memory and still provide much lower performance then the one that can be achieved using real compiled languages like C++.
  4. It is good to remember that these benchmarks are still quite synthetic ones and in real life the actual performance depend on many factors - but using high quality and high performance libraries available for C++ have significant impact on performance.

Results Data

System and Hardware

  • OS: Linux, Debian Lenny, 64 bit
  • Hardware: AMD Athlon XP 3000, 64 bit, 1GB memory

Related:

Code

The Code can be downloaded from there. note, to run it you will need to have some libraries installed and configure some hardcoded paths to make it run.