.Resources.Get with relative path to current folder returns nil

I’m writing image render hook to process images.

In markdown I use relative path to images like this:

![Photo by Behnam Norouzi on Unsplash](./images/behnam-norouzi-_1ok63FFlM4-unsplash.jpg "Photo by Behnam Norouzi on Unsplash")

The render hook code:

{{ printf "%v" (.Page.Resources.Get .Destination) }}

rendered <nil>

Hugo version

hugo v0.125.4+extended darwin/amd64 BuildDate=2024-04-25T13:27:26Z VendorInfo=brew

Question is: What’s the expected way to refer to resources in markdown? Can .Page.Resources.Get .Destination support such case?

Current work around:

{{- $dest := ( .Destination | safeURL ) -}}
<!-- get actual filename -->
{{- $dest = path.Join (path.Dir $dest) (path.Base $dest) -}}
1 Like

why (./ and not just (/ ?

why the dot?

/ is for root assets folder. Two options: images/behnam-norouzi-_1ok63FFlM4-unsplash.jpg or ./images/behnam-norouzi-_1ok63FFlM4-unsplash.jpg.

I just get used to the ./ version and it’s supported in Markdown rendering

I may be wrong, or your code may be build differently,

But with Hugo and typical markdown approach it’s /images/image.jpg is for reference to root images/image.jpg is referenced to current folder.

Even in pure markdown personally I haven’t saw ./ approach.

Wirhout code a guess for a possible cause.

  • A simple typo (letter case)

… or

  • Your page is not index.md or _index.md:

Page resources are only accessible from page bundles, those directories with index.md or _index.md files at their root. Page resources are only available to the page with which they are bundled.

From Page resources | Hugo

Share from stackoverflow: https://stackoverflow.com/a/52003495/1667395

Acturally it’s quite normal and works fine in vscode markdown preview:

I used path.Base and .GetMatch to find the resource. But the new hugo version seems to prepend a slash according to https://github.com/gohugoio/hugo/issues/12214

I update my theme code and use path.Join (path.Dir $dest) (path.Base $dest) now. (git commit https://github.com/tomowang/hugo-theme-tailwind/commit/a012d538c867799161e589aa040a8ce7d9d735a0)

Anyway it works like a charm. Just wondering whether there are any better solutions. Or I have to remove all the leading ./ in my posts.

Just strip the leading ./ .

.Resources.Get (strings.TrimPrefix "./" .Destination)