Hugo Templating - Adding enclosure image in RSS feed

This is part of more mods for the Academic theme. In this case, I attempted to change the RSS feed to show the header image. R-Bloggers show the first image in the rss feed as the post image rather than the header image specified in the post. So I changed the rss template in the Academic theme to specify a featured image. The comments are most likely incorrect in some places since I am just learning how to use the Hugo templates but the line works.

I pieced together most of this with help from post1, post2, and post3

The new one-line addition to the rss template is:

{{ with .Params.header.image }}{{ $image_location := printf "static/img/%s" .}}{{$image := readFile $image_location }}{{$len := len $image}}<enclosure url="http://yihanwu.ca/img/{{.}}" length="{{$len}}" type="image/png" />{{end}}

This line is inserted before the and after </>.

Comments on the code: (aka things that threw errors when creating the line above)

Hugo conditional statements such as with and if start as {{ with …}} and have to have a closing {{end}} statement.

Accessing nested front matter params can be done in two ways, by appending successively with dots in between or by adding a text string. .Params.header.image vs .Param "header.image".

The nesting occurs due to the Academic theme front matter:

title: ""
header: 
  caption: ""
  image: ""

Accessing the string value of caption can be done with .header.caption.

Variables starting with the $.Param refer to global while those that are dot only .Param refer to the local scope (aka in the loop). Since this line is inserted with the {{ range first 15 (where .Data.Pages "Type" "!=" "home") }} {{end}}statement, the page becomes the local scope, and then the {{with}} means the code within only applies if the param in the {{with}} statement exists. So when .header.image is not specified, nothing is created.

RSS feeds can use <enclosure/> to include an image. This requires 1) an url, 2) the byte size of the image, and 3) the MIME type. I’m most likely staying with .png for header images so that can be preset.

Getting the img url can be done easily by just concatenating .Params.header.image to where my images should be linked from, http://yihanwu.ca/img/. Do not use https as this invalidates the RSS feed.

Getting the size in bytes of the image itself is slightly trickier. {{ len .}} where . is the image name throws errors or just returns the number of characters in the string. So the image file has to be read into memory to get the size in bytes.

I don’t think it is possible to put the double braces within the double braces, ie {{ {{ }} }}. So we cannot get the image location and its size in one step.

First step is getting the actual location of the image file which is relative to project directory and not public.

{{ $image_location := printf "static/img/%s" .}} creates the image location link, relative to the project directory. This is stored in the image_location variable which is then used in {{$image := readFile $image_location }}. Now the file is actually read into memory. Then size is obtained by len and stored in the $len variable before being put into the length= element as a string.

Next
Previous