Markdown Preprocessor

March 31, 2014 // Tagged in: software, c#, web

I recently added a feature to my application to serve arbitrary markdown documents from a directory. This is designed to allow authors to populate a folder full of help documents, and be able to view them without any code changes. However, because this is an MVC application, I needed a few extra features. Straight to it, here's my controller:

In short, it allows passing a file name (without ".md") called "id" (poor name is my fault for lazy routing), assigns a default document if a file name was not passed, sets a ViewBag link to the homepage if we're not on the default document, maps the file name to a markdown document in the directory, replaces some preprocessed keywords, and returns the view with the contents of the document.

My view renders the homepage link, if applicable, then the document using MarkdownSharp. I haven't seen my idea of "preprocessed keywords" in markdown before, so I was wandering what others think. It's really useful for resolving links in documents, without worrying about them while writing. It allows document writers to write:

To view your user page [click here]({{AppRoot}}/User/Me) and here's an image: ![image]({{DocumentationImages}}/UserPageImage.png)

Which is transformed to this, on localhost, without the author worrying about the actual path to the application, or the content directory:

To view your user page [click here](http://localhost/MyApp/User/Me) and here's an image: ![image](http://localhost/MyApp/Content/Documentation/images/UserPageImage.png)

I also added one for the support link, so I could pull in that setting from ConfigurationManager.AppSettings.

What do you think?