Hugo: Add shortcode to link to static files

Created on 24 Jul 2017  路  5Comments  路  Source: gohugoio/hugo

(Examples use a file at /static/path/to/image.jpg)

In order to get links to static files in content files, we need to either

  1. Write a path relative to the current directory, with ../ and the like, so a file at content/section/subsection/page.md would need ../../../path/to/image.jpg
  2. Prefix the path with a leading /, which causes the path to resolve to `example.com/path/to/image.jpg'

(Leaving off the leading / causes the link to resolve relative to the current page, which is undesirable)

The first is a pain to have to do, and the second only works if the baseURL is example.com. example.com/subdir/ will still resolve the link to `example.com/path/to/image.jpg', which won't exist.

I propose having a shortcode similar to the existing ref and relref that takes a path or URL, processes it through absURL or absLangURL, and returns that to the markdown. It would look nicer than the first alternative above and resolve correctly with subdirectories, unlike the second.

What I came up with:
Syntax: {{< static "/path/to/image.jpg" >}}
Code (ignores leading /):

{{- .Scratch.Set "path" (.Get 0) -}}
{{- if hasPrefix (.Scratch.Get "path") "/" -}}
  {{- .Scratch.Set "path" (slicestr (.Scratch.Get "path") 1) -}}
{{- end -}}
{{- .Scratch.Get "path" | absLangURL -}}

Without stripping the leading /, the code would just be:

{{- .Get 0 | absLangURL -}}
Enhancement Stale

Most helpful comment

I arrived at this issue searching for a safe way to link to a static asset from within any page. #3651 was resolved, however, I don't see the way that resolves this issue. Would any project maintainers please comment here if it does?

All 5 comments

This situation will improve once #3651 is out, but it is still a good idea.

We should consider naming it similar to ref and relref: staticRef, staticRelref?

This issue has been automatically marked as stale because it has not had recent activity. The resources of the Hugo team are limited, and so we are asking for your help.
If this is a bug and you can still reproduce this error on the master branch, please reply with all of the information you have about it in order to keep the issue open.
If this is a feature request, and you feel that it is still relevant and valuable, please tell us why.
This issue will automatically be closed in the near future if no further activity occurs. Thank you for all your contributions.

I arrived at this issue searching for a safe way to link to a static asset from within any page. #3651 was resolved, however, I don't see the way that resolves this issue. Would any project maintainers please comment here if it does?

Unfortunately, there still doesn't seem an official one-liner for linking to static assets.

The activity here seems to indicate that page bundles are the official answer. However, that's only a good solution if the asset is used on just one set of pages. If it's used in more than one place on the site then you have to replicate the asset wherever it's needed, which isn't very satisfactory, or come back to the original question, linking to static assets in content.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

vielmetti picture vielmetti  路  3Comments

MunifTanjim picture MunifTanjim  路  3Comments

kaushalmodi picture kaushalmodi  路  3Comments

crash-dive picture crash-dive  路  3Comments

moorereason picture moorereason  路  3Comments