🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of bryanchun

bryanchun's solution

to Markdown in the Elixir Track

Published at Aug 21 2020 · 0 comments
Test suite

Refactor a Markdown parser.

The markdown exercise is a refactoring exercise. There is code that parses a given string with Markdown syntax and returns the associated HTML for that string. Even though this code is confusingly written and hard to follow, somehow it works and all the tests are passing! Your challenge is to re-write this code to make it easier to read and maintain while still making sure that all the tests keep passing.

It would be helpful if you made notes of what you did in your refactoring in comments so reviewers can see that, but it isn't strictly necessary. The most important thing is to make the code better!

Running tests

Execute the tests with:

$ mix test

Pending tests

In the test suites, all but the first test have been skipped.

Once you get a test passing, you can unskip the next one by commenting out the relevant @tag :pending with a # symbol.

For example:

# @tag :pending
test "shouting" do
  assert Bob.hey("WATCH OUT!") == "Whoa, chill out!"

Or, you can enable all the tests by commenting out the ExUnit.configure line in the test suite.

# ExUnit.configure exclude: :pending, trace: true

If you're stuck on something, it may help to look at some of the available resources out there where answers might be found.

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.


defmodule MarkdownTest do
  use ExUnit.Case

  # @tag :pending
  test "parses normal text as a paragraph" do
    input = "This will be a paragraph"
    expected = "<p>This will be a paragraph</p>"
    assert Markdown.parse(input) == expected

  # @tag :pending
  test "parsing italics" do
    input = "_This will be italic_"
    expected = "<p><em>This will be italic</em></p>"
    assert Markdown.parse(input) == expected

  # @tag :pending
  test "parsing bold text" do
    input = "__This will be bold__"
    expected = "<p><strong>This will be bold</strong></p>"
    assert Markdown.parse(input) == expected

  # @tag :pending
  test "mixed normal, italics and bold text" do
    input = "This will _be_ __mixed__"
    expected = "<p>This will <em>be</em> <strong>mixed</strong></p>"
    assert Markdown.parse(input) == expected

  # @tag :pending
  test "with h1 header level" do
    input = "# This will be an h1"
    expected = "<h1>This will be an h1</h1>"
    assert Markdown.parse(input) == expected

  # @tag :pending
  test "with h2 header level" do
    input = "## This will be an h2"
    expected = "<h2>This will be an h2</h2>"
    assert Markdown.parse(input) == expected

  # @tag :pending
  test "with h6 header level" do
    input = "###### This will be an h6"
    expected = "<h6>This will be an h6</h6>"
    assert Markdown.parse(input) == expected

  # @tag :pending
  test "unordered lists" do
    input = "* Item 1\n* Item 2"
    expected = "<ul><li>Item 1</li><li>Item 2</li></ul>"
    assert Markdown.parse(input) == expected

  # @tag :pending
  test "with a little bit of everything" do
    input = "# Header!\n* __Bold Item__\n* _Italic Item_"

    expected =
      "<h1>Header!</h1><ul><li><strong>Bold Item</strong></li><li><em>Italic Item</em></li></ul>"

    assert Markdown.parse(input) == expected


ExUnit.configure(exclude: :pending, trace: true)
defmodule Markdown do
  @doc """
    Parses a given string with Markdown syntax and returns the associated HTML for that string.

    ## Examples

    iex> Markdown.parse("This is a paragraph")
    "<p>This is a paragraph</p>"

    iex> Markdown.parse("#Header!\n* __Bold Item__\n* _Italic Item_")
    "<h1>Header!</h1><ul><li><em>Bold Item</em></li><li><i>Italic Item</i></li></ul>"
  @spec parse(String.t()) :: String.t()
  def parse(m) do
    |> String.split("\n")
    |> Enum.map_join(&process/1)
    |> enclose_with_list_tag()

  # Handle a line of markdown
  defp process("#" <> _ = heading),   do: heading |> parse_header_md_level() |> enclose_with_header_tag()
  defp process("*" <> _ = list_item), do: list_item |> parse_list_md_level()
  defp process(line),                 do: line |> enclose_with_paragraph_tag()

  # Handle headings
  @spec parse_header_md_level(String.t()) :: {integer, String.t()}
  defp parse_header_md_level(hwt) do
    [hashes | title] = hwt |> String.split(" ", parts: 2)
    { String.length(hashes), title }

  defp enclose_with_header_tag({level, title}), do: "<h#{level}>#{title}</h#{level}>"

  # Handle lists in the line level
  defp parse_list_md_level(l) do
    md = l
    |> String.trim_leading("* ")
    |> String.split()
    |> join_words_with_tags()
  # Handle normal text  
  defp enclose_with_paragraph_tag(l) do
    md = l
    |> String.split()
    |> join_words_with_tags()  

  # Common utilities
  defp join_words_with_tags(t), do: t |> Enum.map_join(" ", &replace_inline_with_tag/1)

  defp replace_inline_with_tag("__" <> rest), do: "<strong>" <> replace_inline_with_tag(rest)
  defp replace_inline_with_tag("_" <> rest), do: "<em>" <> replace_inline_with_tag(rest)
  defp replace_inline_with_tag(w) do
    bold_size = lead_size(w, "__")    # in order to use binary pattern matching, sizes need to be precomputed
    italic_size = lead_size(w, "_")
    case w do
      <<lead::binary-size(bold_size), "__">> -> replace_inline_with_tag(lead) <> "</strong>"
      <<lead::binary-size(italic_size), "_">> -> replace_inline_with_tag(lead) <> "</em>"
      _ -> w
  defp lead_size(w, token), do: byte_size(w) - byte_size(token)

  # Handle list to enclose li's with an ul across lines
  defp enclose_with_list_tag(l) do
    |> String.replace("<li>", "<ul><li>", global: false)  # replace first occurrence
    |> String.replace_suffix("</li>", "</li></ul>")       # replace last occurrence

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?