Episode completed

Next epsiode starting in 3 seconds

Published at

Script

A simple, yet effective way to add formatting to text is markdown. Although it is possible, we don’t need to write our own markdown parser. Instead, we can use an existing package like mmark. To add the package to our project, we need to edit the default.nix file in your project folder. MMark is a Haskell Package, so we need to add it to our haskell dependencies. After we have done that, we need to stop our devserver and update our local environment. We do this by remaking the .envrc file. This will download and install the mmark package. After that, we can start the devserver again. To use the package, we need to import it in our ShowView. Next, we need to create a function, which takes the post content and parses it with mmark. First we need to parse the body with the MMark.parse function. The empty string we pass to the parse function is usually the filename of the markdown file, but we don’t need that in this case. This statement returns Either a markdown object or an error, if the parse fails. To check that, we can wrap this in a case like this. In case there is an error, we can output an error message. If everything works, we can render the parsed markdown. Since the render function returns a String, we can convert it to text with tshow. This is needed, because now we have raw html as a text. To parse that, we can use the preEscapedToHtml function from IHP to display the Html correctly. Now all that's left to do is adding that function to our body. We also should change the body input field in the new view to a text area field, so that we can input multiline strings. If we now return to our browser, we can now create a blogpost with markdown. When we now open the post, the body is nicely formatted.

Discussion