dwyl / learn-elixir

:droplet: Learn the Elixir programming language to build functional, fast, scalable and maintainable web applications!
1.62k stars 109 forks source link

Which Hex.pm Package for Parsing/Transforming Markdown to HTML? #112

Closed nelsonic closed 5 years ago

nelsonic commented 5 years ago

I need to parse Markdown and transform it to HTML which hex.pm package should I use?

Suggestions Welcome! 💬

Note: I have already done a basic ("spike") of this, I'm opening the issue to capture it in case it's useful to someone else... 💭

nelsonic commented 5 years ago

In Club Soda we are using earmark https://hex.pm/packages/earmark https://github.com/club-soda/club-soda-guide/blob/a7d95752c3144cacbcce800d5420d9bd210e52a8/mix.exs#L53

Searching for "Markdown" on Hex.pm https://hex.pm/packages?search=markdown It is the most popular (by downloads) module for this purpose: image

Sorting for Total downloads the popularity is clear (10x more than edown) https://hex.pm/packages?search=markdown&sort=total_downloads image

Anyone got experience of using anything other than earmark...?

nelsonic commented 5 years ago

Add earmark to the deps section of your mix.exs file:

      { :earmark, "~> 1.3.0" }

example: https://github.com/nelsonic/nelsonic.github.io/blob/47e54c8d7dc93a5cac06795d1152ca0264f09b99/mix.exs#L22-L24

After running mix deps.get you will be able to transform .md into .html:

Code:

  def transform do
    filename = Path.join(File.cwd!, "p/goals.md") # __ENV__.file # "./app.ex"
    # IO.puts filename
    case File.read(filename) do
      {:ok, markdown}      -> # do something with the `body`
        IO.puts markdown
        html_doc = Earmark.as_html!(markdown)
        IO.puts html_doc
        html_path = Path.join(File.cwd!, "p/goals.html")
        File.write!(html_path, html_doc)
      {:error, reason} -> # handle the error caused by `reason`
        IO.puts reason
    end
  end
RobStallion commented 5 years ago

@nelsonic I haven't used anything other than earmark myself. I see that you ended up choosing earmark as well. I am just wondering if you looked into edown or any of the other options?

nelsonic commented 5 years ago

@RobStallion at the moment earmark meets my needs/requirements so I have not looked further. My actual work is to publish a Markdown file as HTML as fast as possible. As soon as I have new or more advanced requirements I will investigate why edown exists ... https://hex.pm/packages/edown

Usually, when there is a "competing" package, it has more or better features. Right now, I don't need anything special, but anyone who has more advanced features should go for it.