Is it possible to render markdown with Phoenix 1.7?

Hi everyone :wave:

I’ve been playing with the new 1.7 RCs and the (mostly very welcome) changes have left me scratching my head. Any pointers here would be very welcome!

Imagine the basic, scaffolded application that comes with mix phx.new. I would like to be able to drop markdown files inside controllers/page_html and hook them up to a route etc. so that they are eventually served as HTML. Pretty standard markdown → HTML stuff…

I’ve attempted to make this work but failed a couple of times now. I’m not sure if my mental model of the pipeline is off, or it’s something else. If you could have a read of my thought process below and let me know that would be fantastic.

Features

Here are the main things I’d like to do, in a bit more specific terms:

  1. Place markdown files in the relevant directory to be served
  2. Retain any layouts
  3. Make use of Phoenix Components in the markdown

Pipeline

This is definitely the section that I’m least sure about. Here’s what I’m imagining right now:

  1. Read file from disk
  2. Convert markdown string to HTML string with earmark
  3. Compile HTML with the LiveView engine

I can nearly get this working by sneaking in a call to Earmark.as_html! before the compile stage in Phoenix.LiveView.HTMLEngine.

There seems to be somewhat of a conflicting process in steps two and three where they each depend on a small part of each other. e.g. If I wanted to include EEx in the markdown (say, for a simple sum of two numbers) then this wouldn’t work; if I wanted to include a component in the markdown then this wouldn’t be parsed by earmark (it won’t pick it up dots before HTML tags).

Any thoughts appreciated!

These eex engines are not composable pieces you can easy make work in any arbirary order. As you noticed Earmark cannot compile things with (h)eex in it and heex cannot compile non html’ish content.
With just EEx you could go the direction of EEx first (it doesn’t care for the content around its tags) to compose a final markdown document and then use Earmark to turn that into html. That won’t give you any integration with heex though.

In the end you’d either need a markdown parser, which can turn markdown with embedded heex/eex tags into html with heex/eex tags and pass that into the heex engine or even a single engine doing both.

I’d suggest a much simpler option, which I’m using for my website: Use NimblePublisher for handling the markdown files. Implement a custom parser, which detects some custom comments <!-- [Component] --> and stores that within the parsed data. When rendering the markdown turn that into a live_render(@conn, …).

stream = Stream.cycle([:html, :live])
    parts = :binary.split(assigns.content, ["<!-- [", "] -->"], [:global])
    assigns = assigns |> assign(:parts, Enum.zip([stream, parts]))

    ~H"""
    <%= for p <- @parts do %>
      <%= case p do %>
        <% {:live, live} -> %>
          <%= live_render(@conn, Module.concat([live])) %>
        <% {:html, html} -> %>
          <%= Phoenix.HTML.raw(html) %>
      <% end %>
    <% end %>
    """
1 Like

An other, it’s not exactly what you want is to write a component like this:

  def markdown(assigns) do
    text = if assigns.text == nil, do: "", else: assigns.text

    markdown_html =
      String.trim(text)
      |> Earmark.as_html!(code_class_prefix: "lang- language-")
      |> Phoenix.HTML.raw()

    assigns = assign(assigns, :markdown, markdown_html)

    ~H"""
    <%= @markdown %>
    """
  end

And then use it like: <.markdown text="your markdown text here" />

1 Like