Checking for a substring to make sure it's an image

I’m using the render-image filter for this one.

The problem here is that several users edit the content and not everyone links the images properly.

The markdown shows ![alt-text](file.png) and the link breaks because config.toml has uglyurls = false.

To avoid messing too much with the build script I have, I wanted to check if the markdown matches the condition file.ext and if so, change it to ../file.ext.

Looking at the docs, there is an in function, but that doesn’t seem to be what I am looking for.
I also tried with absURL and relURL.

<!-- {{/*  https://gohugo.io/templates/render-hooks/#render-hooks-for-headings-links-and-images  */}} -->

<img src="{{ .Destination | safeURL | relURL }}" alt="{{ .Text }}" {{ with .Title}} title="{{ . }}"{{ end }} />

Any ideas, or suggestions of a new route?

findRE seems more appropriate here. It works with simple go regex.

If it’s just one ext to check you can do {{ if strings.HasSuffix .Destination ".png" }}.

Looking at this again, I didn’t even need to find the ext because the context is already the render-image.html hook.

The current solution is this, but still needs work:

{{ if eq (len (findRE ".*/.*" .Destination) ) 0 }}
<img src="{{ (printf "../%s" .Destination) | safeURL  }}" alt="{{ .Text }}" {{ with .Title}} title="{{ . }}"{{ end }} />
{{ else }}
<img src="{{ .Destination | safeURL  }}" alt="{{ .Text }}" {{ with .Title}} title="{{ . }}"{{ end }} />
{{ end }}

The problem here is that I am trying to fix the url for the user, because in this particular context I can’t force the user to follow a convention.

I was thinking that another approach would be to pick up the markdown image code and add it as a resource. The render-image hook would then just find it by src and output the permalink.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.