How to reference a static file in page bundle from within a shortcode?

The link docs show that ref is useful for linking from one document to another using a path, e.g.,

{{< ref "/post/topic/slug" >}} 

It doesn’t seem to support other files in a page bundle. For example:

![]({{% ref "/post/topic/slug/summit.jpeg" %}})

This shortcode emits the error:

REF_NOT_FOUND: Ref "/post/topic/slug/summit.jpeg": "/Users/david/dev/blog/content/post/topic/other-post.md:17:5": page not found

Is there something like ref (and urls.Ref for functions) that supports files other than the main document file? Or am I just missing something in my use of ref?

Are you currently using a link or image render hook?

Not to my knowledge, no. I first encounter the issue trying to use urls.Ref in a shortcode, but found the same thing just putting a line like the above directly into a post.

If you are using v0.123.0 or later, try this in your site config:

[markup.goldmark.renderHooks.image]
  enableDefault = true
[markup.goldmark.renderHooks.link]
  enableDefault = true

https://gohugo.io/getting-started/configuration-markup/#renderhooksimageenabledefault
https://gohugo.io/getting-started/configuration-markup/#renderhookslinkenabledefault

And don’t use the relref shortcode anywhere.

Here’s a detailed description of each:

I’m on v0.123.2. Setting those configs made no difference. But those docs refer to files in the assets directory. I’m trying to link to files in a page bundle, which is in the content directory.

Read it again. Carefully. The link and image render hooks first look for page resources, then global resources.

Can you share your repository, even privately? I’d like to understand why it isn’t working in your case.

It just occured to me…

Are you trying to access a page resource associated with PAGE-A from PAGE-B? If yes, don’t go down that path. If it’s used in two or more places make it a global asset.

The render hooks work great for that.

If, for some reason, you don’t want to create it as a global asset, you can combine the relref shortcode with an image path relative to source page.

Here are multiple examples:

git clone --single-branch -b hugo-forum-topic-48656 https://github.com/jmooring/hugo-testing hugo-forum-topic-48656
cd hugo-forum-topic-48656
hugo server

Yes, sorry, I thought the error message made that clear, but I should have been more explicit. I could move the image to a global asset directory. It just seems al little silly, though, since I published PAGE-A last week, and now just wanted to point to the same image in PAGE-A’s directory from a new post today, PAGE-B.

But I think I see now, I can try passing the other page as a a reference to my shortcode. This works:

![]({{% ref "/post/topic/slug" %}}summit.jpeg)

To use urls.Ref I just need to look up the other page bundle (using GetPage).

Here’s why it’s not:

  • I set PAGE-A to expire at a certain date, OR
  • I later set draft = true in PAGE-A’s front matter, OR
  • For whatever reason I decide to delete PAGE-A, OR
  • I delete a page resource from PAGE-A because I’ve forgotten that I used it elsewhere.

Now I have to track down everywhere I’ve used any of the page resources in PAGE-A. This is very fragile.

That’s fair, all about tradeoffs. I don’t make any of these sorts of changes to my posts. Agree it’s safer to use a global asset, and less fussy certainly.

Thanks for the help.