neverstew

neverstew

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!

Showing Posts 1 to 3

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!

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
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
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews