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

tudoanh's solution

to Markdown in the Elixir Track

Published at Sep 22 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
  import Enum,
    only: [join: 2, map: 2, map_join: 3]

  import String,
    only: [
      first: 1,
      replace: 4,
      replace_prefix: 3,
      replace_suffix: 3,
      split: 1,
      split: 2,
      trim_leading: 2

  @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(markdown) do
    |> split("\n")
    |> map_join("", &process/1)
    |> wrap_list

  defp process(line) do
    # Refactor string transform with pipeline
    # Simplify if else logic with case
    case line |> first do
      "#" ->
        line |> split |> parse_header_md_level |> enclose_with_header_tag

      "*" ->
        line |> parse_list_md_level

      _ ->
        line |> split |> enclose_with_paragraph_tag

  defp parse_header_md_level([head | tail]) do
      head |> String.length(),
      tail |> join(" ")

  defp make_list(item), do: "<li>#{item}</li>"

  defp parse_list_md_level(list) do
    |> trim_leading("* ")
    |> split()
    |> join_words_with_tags
    |> make_list

  defp enclose_with_header_tag({heading, text}) do

  defp enclose_with_paragraph_tag(paragraph) do

  defp join_words_with_tags(words) do
    |> map(&replace_md_with_tag/1)
    |> join(" ")

  defp replace_md_with_tag(word) do
    |> replace_prefix_and_suffix_md

  defp replace_prefix_and_suffix_md(word) do
    |> replace_prefix("__", "<strong>")
    |> replace_prefix("_", "<em>")
    |> replace_suffix("__", "</strong>")
    |> replace_suffix("_", "</em>")

  defp wrap_list(markdown) do
    |> replace("<li>", "<ul><li>", global: false)
    |> replace_suffix("</li>", "</li></ul>")

Community comments

Find this solution interesting? Ask the author a question to learn more.

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?