Alchemy Markdown

It's time to learn how to use mix! Mix is the Elixir equivalent of npm/yarn, pip or gem and it plays a fundamental role in dependency management, automated testing, OTP application management and scripting.

Last episode we worked on a minimal (and incomplete) implementation of Markdown. Now we're going to use mix to pull in Earmark, a Markdown implementation. Interestingly, its author is Dave Thomas, who wrote the excellent guide Programming Elixir.

Here's a breakdown of this episode:

  • Choosing Earmark - (1:44)
  • Getting started with mix - (3:10)
  • A first look at testing - (5:37)
  • The mix file and adding deps - (8:47)
  • Using Earmark and adding functionality & unit tests - (12:00)
  • Reviewing unit tests and migrating integration test from last time - (31:07)
  • Discovering Earmark is a dash-eater (plus workaround) - (37:30)

The markdown features implemented are:

  • All the standard ones implemented by Earmark (thank you Dave and thank you Mix!)
  • <big></big> tags, by placing ++ before and after a string of text.
  • <small></small> tags by placing -- before and after a string of text.
  • <hr/> tags by making a line start with three or more - or * characters with any number of spaces between them.

All of these features as well as those from MiniMarkdown are unit tested using the standard mix test.

Level up your Elixir skills

Request a free email course

Back to index

3 Comments

If you use three ` in a row, you can do GFM-style comments here!

- alchemist · 5 years ago

My -- turns into ΓÇô on windows powershell. I tried <<45>> with no luck. So my tests fail with left: "<p>\nSome ΓÇôsmallΓÇô words!</p>\n" right: "<small>small</small> words"

When I use git bash I get: left: "<p>\nSome –small– words!</p>\n" right: "<small>small</small> words"

- jifem · 5 years ago

Another way to regex for hrs :

def hrs(text) do
    Regex.replace(~r/(\r\n|\r|\n|^)([*-])\s*(\2\s*){2,}/, text, "\\1<hr />")
end
- jtoulouse · 5 years ago