matthewsinclair

matthewsinclair

Nimble Publisher: how to hand image refs in markdown and how to control how HTML is emitted?

I have a couple of (relative) newb questions about Nimble Publisher that I can’t quite work out.

I have it (more or less) working but two questions remain:

  1. What is the best way to handle a markdown file with an image ref in it? For example, let’s say that the original markdown file was in a directory called dir/ and in that directory is an image called dir/image.png. If the markdown file is in the same directory (ie dir/markdown.md I could use the standard markdown image reference tag ![image](./image.png)and the markdown viewer or renderer would create an <img src="./image.png">… tag from the markdown. However, because of the way that the routes are set up, that image url ends up being interpreted as an id for a blog post, which of course it can’t find. I can’t be the first person to ever try to do this so there must be an idiomatic way to handle URIs embedded in the markdown file or a way to configure the routes so that markdown is handled by nimble by images are and other content are treated as normal urls.

  2. I want to control the way that the HTML is emitted from the markdown processor so that I can style it. At the moment, a markdown # Heading is being rendered simply as <h1>Heading</h1>. But what I need to do is control the styling of the emitted HTML. Again, I can’t be the first person who has ever needed to do this, but I can’t find from the docs what the best way to handle this would be.

Any ideas or even a link to an example or some docs for either problem would be very helpful. Thanks.

PS: I originally posted this on GitHub to the NimblePublisher repo here, but was asked to cross-post here by @josevalim.

Marked As Solved

billylanchantin

billylanchantin

Here’s my approach for 2 (but with your styles substituted).

I’ve added the following to my post.ex:

  def earmark_options do
    Earmark.Options.make_options!(postprocessor: Earmark.AstTools.node_only_fn(&add_tag_class/1))
  end

  @tag_to_class %{
    "h1" => "text-4xl font-bold tracking-tight text-zinc-800 sm:text-5xl dark:text-zinc-100",
    "h2" => "text-m font-bold text-zinc-800 dark:text-zinc-100",
    "h3" => "text-base font-semibold tracking-tight text-zinc-800 dark:text-zinc-100",
    "h4" => "text-3xl tracking-tight text-zinc-800 sm:text-3xl dark:text-zinc-100",
    "p" => "mt-6 text-base text-zinc-600 dark:text-zinc-400",
    "a" => "text-m font-medium text-teal-500"
  }
  defp add_tag_class({tag, attrs, ignored, meta}) do
    new_attrs = if class = @tag_to_class[tag], do: [{"class", class}], else: []
    {tag, attrs ++ new_attrs, ignored, meta}
  end

Then the following to my use NimblePublisher invocation:

  use NimblePublisher,
    # ...
    earmark_options: Post.earmark_options(),
    # ...

The postprocessor option gives you a hook which lets you work with the AST.

I also had to add this to my tailwind.config.js:

module.exports = {
  content: [
    // ...
    "../lib/path/to/post.ex",
    // ...
  ],

So it would pickup the new classes.

Also Liked

matthewsinclair

matthewsinclair

That’s a neat way to solve this (and more robust than my ropey attempt). Thanks!

adamu

adamu

I don’t know if it’s the best way, but I store the images one directory below the markdown and serve them with Plug.Static.

My markdown is saved in priv/posts and rendered on the /blog/:title route.
I made priv/posts/images to store the images, and then set up a new Plug.Static in the endpoint to serve them on the same /blog scope:

  plug Plug.Static,
    at: "/blog/",
    from: {:my_app, "priv/posts"},
    gzip: false,
    only: ["images"]

Then in your markdown:

![](images/lovely.jpg)

If we place the image in priv/posts/images/lovely.jpg, it should load.

You can also use an <img> tag directly in the markdown if you need to control the width/height:

<img src="images/lovely.jpg" width="100px" height="50px" />

It’s worth mentioning that unfortunately we can’t get the benefits of Phoenix verified routes or mix.digest for the images, because the markdown is injected as raw HTML and not rendered by Phoenix, so unable to get the route information (theoretically it might be possible to do it, because both the markdown and HEEX templates are parsed at compile-time, but I didn’t try it…). This means that we have to directly link to the image and won’t get warnings for invalid image paths and may end up with broken images (like this dashbit post at the time of writing).

Where Next?

Popular in Questions Top

albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
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
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

Other popular topics Top

yurko
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1140 51847 244
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
sergio
I couldn’t find any guides that worked well with Phoenix 1.6.0 and esbuild. I hope this helps people test the waters and eases you into t...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 49522 488
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
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
AstonJ
by Lance Halvorsen Elixir and Phoenix are generating tremendous excitement as an unbeatable platform for building modern web application...
460 27162 124
New
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New

We're in Beta

About us Mission Statement