Greetings,
I can’t tell if what i’m trying to accomplish is a wonton abuse of Page Resources, or if it is a current limitation which could potentially be improved.
Motivation
Hotlinking media files is a constant reality on the web (e.g. YouTube videos). In addition to videos, it often makes sense to (legally) hotlink image files as well (e.g. hosting images on a service like Flickr). Hugo’s Page Resources is designed specifically for files hosted in the site’s content directory. But, being a robust solution for media handling + media metadata, the ability to employ the logic of Page Resources for media files external to the site would be helpful (i think).
Problem
A Page Resource will return empty/nil unless the src specified in the corresponding markdown file targets a file that is actually present in the content directory. It doesn’t matter what other Page Resources metadata may be set in front matter. It’s the src that makes or breaks it. So, for example, the following would return empty/nil:
Front Matter
resources:
- name: "image"
src: ""
title: "Title for the image"
params:
alt: "Description of the image"
caption: "Caption for the image"
Template
{{ $image := .Resources.Match "**image*" }}
{{ printf "%#v" $image }}
Whereas, assuming “image.jpg” is actually located in the same directory as the markdown file, this would return a result:
Front Matter
resources:
- name: "image"
src: "image.jpg"
title: ""
params:
alt: ""
caption: ""
Template
{{ $image := .Resources.Match "**image*" }}
{{ printf "%#v" $image }}
So it doesn’t matter about all that other metadata. As long as the src accurately targets an existing file, there will be a result. But, if the src does not accurately target an existing file—regardless of how much metadata may be set in the front matter—there will be no result.
Proposal
What if Page Resources could return a result in the absence of a src in the content directory? For example:
Front Matter
resources:
- name: "image"
src: ""
title: "Title for the image"
params:
alt: "Description of the image"
caption: "Caption for the image"
hotlink: "//some-other-website.com/image.jpg"
Template
{{ with .Resources.GetMatch "image" }}
{{ if or ( .Params.hotlink ) ( .RelPermalink ) }}
<img
{{ with .Params.alt }}
alt="{{ .Params.alt }}"
{{ end }}
{{ if .Params.hotlink }}
src="{{ .Params.hotlink }}"
{{ else if .RelPermalink }}
src="{{ .RelPermalink }}"
{{ end }}
>
{{ end }}
{{ end }}
In this way, the user could hotlink to an image, but keep/utilize the metadata set in the Page-Resource-specific front matter. In my example, both a hotlink and a content-directory-located src could be specified, with the default behavior favoring the hotlink (but obviously this could be reversed). The point however, is that Page Resources is a robust solution for media files and their corresponding metadata. Getting it to work with media files outside of the site could make it that much more powerful (i think).
Of course i realize there’s a workaround to achieve a similar behavior. For example the following could be done:
Front Matter
hotlink:
src: "//some-other-website.com/image.jpg"
alt: "Description of the image"
title: "Title for the image"
caption: "Caption for the image"
resources:
- name: "image"
src: "image.jpg"
title: "Title for the image"
params:
alt: "Description of the image"
caption: "Caption for the image"
Template
{{ if .Params.hotlink.src }}
<img
{{ with .Params.hotlink.alt }}
alt="{{ .Params.hotlink.alt }}"
{{ end }}
src="{{ .Params.hotlink.src }}"
>
{{ else if .Resources.GetMatch "image" }}
{{ with.Resources.GetMatch "image" }}
<img
{{ with .Params.alt }}
alt="{{ .Params.alt }}"
{{ end }}
src="{{ .RelPermalink }}"
>
{{ end }}
{{ end }}
But my question is motivated by a desire to keep all of this under the umbrella of Page Resources.
Thoughts?