sodapopcan

sodapopcan

Earmark - Parsing HTML inside code blocks

Using Earmark, I’m trying to parse HTML inside markdown code blocks so that they can be syntax-highlighted. I’m having a bit of a rough time with it and, admittedly, I could probably stand to spend a bit more time trying to figure it out myself but I feel I’m maybe on the wrong path as it stands so I thought it couldn’t hurt to ask—also, whenever I do ask for help, I usually figure it out minutes later :sweat_smile:

So, I think understand that the following doesn’t work—the 3rd (content) element of the 4-tuple is usually ignored:

def parse() do
  """
  ```sql
  <span class="k">SELECT</span> * <span class="k">FROM</span> table
  ```
  """
  |> Earmark.as_ast!()
  |> Earmark.map_ast(&parse_html/1)
  |> Earmark.transform()
end

defp parse_html({"code", [{"class", "sql"}], [html], meta} = node, true) do
  {:ok, html} = Floki.parse_fragment(html)

  {:replace, {"code", [{"class", "sql"}], [html], meta}}
end

I say usually since the documentation doesn’t explicitly mention that the content node is ignored when using {:replace, node} yet that seems to be the case (I’m actually not sure .

I there a way to simply replace the content node?

Is there just a better way of doing this (without using highlightjs)?

I’m looking at pre- and post-processors but currently not having much luck.

Thanks for reading!

Most Liked

sodapopcan

sodapopcan

After sleeping on it, I figured it out. It was right there in the documentation, it just didn’t click with me that that is what I needed.

Using map_with_ast we can use the accumulator to conditionally match on a specific text node. The part that the accumulator was used to match in this was is that part that flew over my head when first reading it.

So I’ve ended up with this:

"""
```sql
<span class="k">SELECT</span> * <span class="k">FROM</span> table
```
"""
markdown
|> Earmark.as_ast!()
|> Earmark.Transform.map_ast_with(false, fn
  {"code", [{"class", "sql"}], _, meta}, _ ->
    {{"code", [{"class", "sql"}], nil, meta}, true}

  html, true ->
    {:ok, html} = Floki.parse_fragment(html)

    html =
      Floki.traverse_and_update(html, fn
        {tag, args, children} -> {tag, args, children, %{}}
      end)

    {html, false}

  node, _ ->
    {node, false}
end)
|> Earmark.transform(options())

Which works! The Floki.traverse_and_update/2 call is necessary to convert from Floki’s tuple representation to Earmark’s.

The only thing left is that the spans are put on their own lines which is causing the formatting to be all wonky, though that is expected and will have to figure something else out there.

RobertDober

RobertDober

Great you found it, was just about to try it out …

Thank you for the PR too, very much appreciated

sodapopcan

sodapopcan

Hey @RobertDober, thanks for the reply and thanks for all your work on Earmark—that is very much appreciated!

Yes, compact_output: true did not help since, as you likely well know, spans are not @compact_tags. I was able to fix it by converting the spans to ems (which I don’t mind at all since semantically they are emphasized although I’m doing it programmatically since I want to eventually integrate with makeup or vim’s :TOhtml) but then I was faced with the problem that if two tags are in a row they render without spaces. e.g.: <em class="k">SELECT</em> <em class="k">FROM</em> they render as SELECTFROM. I was actually able to solve this but in a very convoluted way:

    {result, _} =
      Earmark.Transform.map_ast_with(result, nil, fn
        {"em", args, _, meta}, nil ->
          {{"em", args, nil, meta}, :em_first}

        {"em", args, _, meta}, :em_next ->
          {{"em", args, nil, meta}, :em_text}

        {tag, args, _, meta}, _ ->
          {{tag, args, nil, meta}, nil}

        text, :em_first ->
          {text, :em_next}

        text, :em_text ->
          {" #{text}", :em_next}

        node, _ ->
          {node, nil}
      end)

So basically saying "If we see an em for the first time, mark it as such (:em_first), then when we see its text node, do nothing other than that mark it to look out for another em (:em_next). If the next node is indeed an em, mark it that it’s part of a string of ems (still :em_next) and leftPad™ it. Anything else, just reset.

It’s a little convoluted and I haven’t revisited it since I got it working.

For all intents and purposes this solves my problems, but having a another little issue that I was going to open in the repo since it seems more appropriate to discuss there (and I want to look at the source a little more to understand if it’s reasonable or not).

Where Next?

Popular in Questions Top

Tee
can someone please explain to me how Enum.reduce works with maps
New
Kurisu
For example for a current url like http://localhost:4000/cosmetic/products?_utf8=✓&amp;query=perfume&amp;page=2, I would like to get: ...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lists...
New
nobody
How to bind a phoenix app to a specific ip address? could not find anything about that, nowhere, unfortunately, but for me this is quite...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

Other popular topics Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30877 112
New
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

We're in Beta

About us Mission Statement