neverstew

neverstew

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!

First 3 of 3 Posts! Switch mode

LostKobrakai

LostKobrakai

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 %>
    """
tcoopman

tcoopman

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" />

10
Post #2
petrus-jvrensburg

petrus-jvrensburg

Thanks for this. Very useful!

Where Next?

Trending in Questions Top

jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
silverdr
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated! To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead. Sta...
New
saveman71
Hello ! We want new/edit form pages to POST/PUT to their own URL rather than the resources REST defaults (post /things, put /things/:id)...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
michallepicki
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New

We're in Beta

About us Mission Statement