← All series

Series contents

Log in to track your progress

  1. 1. Welcome to Alchemist Camp Episode
  2. 2. Lesson 1: The guessing game Episode
  3. 3. Solution 1: The Greeter Episode
  4. 4. Lesson 2: Word Count Episode
  5. 5. Word Count ULTIMATE EDITION! Episode
  6. 6. Minimal Todo List Part 1 Episode
  7. 7. Elixir lists vs Elixir tuples Article
  8. 8. The Image Snatcher Episode
  9. 9. Minimal Todo List Part 2 Episode
  10. 10. Elixir maps made effortless Article
  11. 11. Fibonacci with tail recursion Episode
  12. 12. Mini Markdown Episode
  13. 13. Extending Mini Markdown Episode
  14. 14. Alchemy Markdown Episode
  15. 15. Using mix to modularize and add tests Episode
  16. 16. Command line applications with escript Episode
  17. 17. Caboose, a simple Unix tail clone Episode
  18. 18. Game board (Tictac Part 1) Episode
  19. 19. Structs: Maps with checks and default values Article
  20. 20. Deck generation, Pythagorean Triplets and more Episode
  21. 21. Whiteboarding the Tictac state machine Episode
  22. 22. Game state machine (Tictac Part 2) Episode
  23. 23. Modelling the memory game Episode
  24. 24. Finishing Tictac and making a CLI Episode
  25. 25. Threatened by rooks, bishops and knights Episode
  26. 26. A worker to log stats from a YouTube’s API Episode
  27. 27. Constructing bitmap files with Elixir Episode
  28. 28. Making bitmaps with color palettes Episode
  29. 29. Flattening lists recursively in Elixir Episode

Mini Markdown

In this episode, we implement a minimal subset of Markdown in Elixir by chaining together regex replace functions into a processing pipeline. If you've ever written a static page on Github or a comment on Reddit, then you've used Markdown. Here's a quick overview in case you're not familiar with it.

Markdown is a terse, human-readable document format that is easily converted to HTML. Anything that appears between a pair of asterisks is *italicized*, while text between a pair of asterisks is **bolded**. A line starting with # creates an H1 tag while starting with ## creates an H2 tag, etc, etc. Markdown is pretty handy when you just want to write a document quickly.

The plan

  • Create a new module with a to_html function that converts Markdown to HTML by calling a pipeline of other functions
  • Create an italics function that will take a text input and use Regex.replace to globally replace *text* with <em>text</em> and return the new text.
  • Create a similar bold function that will globally replace **text** with <strong>text</strong>
  • Create a p function that wraps lines of text with <p> tags
  • Create a test string that uses various features and lets us easily test our module as we add to it.
  • Try writing an output.html file from a simple markdown file.

This module isn't that complex, but it's a great way to show off the power of Elixir's function pipelines! We'll go into a more standard way of doing unit tests once we start working with mix in a few lessons.

Challenge 5

Add two more HTML tags to markdown—"big" and "small". Decide for yourself what the syntax should be to generate them.

(solution here)

Open standalone page ↗

3 Comments

I found that if p function put in first pipeline chain will cause error when test_str is poem, and italics, bold are used for end words.

- manhtranlinh · 6 years ago

Interesting. I haven't encountered that error. What's the full input string you're trying to convert?

- alchemist · 6 years ago

I wonder why you catch on the empty lines in the regex instead of just that : Regex.replace(~r/[\r\n|\n|\r|^]+([^\r\n]+)([\r\n|\n|\r]+$)?/, txt, "<p>\1</p>")

- jtoulouse · 5 years ago