Customizing Hugo / Blogdown RSS Templates

December 12, 2018
By

[This article was first published on Yongfu's Blog, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Blogdown makes it easy to create Hugo blogs or personal websites, and it is becoming more and more popular in the R community. Once the blog is created, people might want to submit their blogs’ RSS feeds to R-bloggers. But before that can happen, one must modify the RSS template to meet the requirements of RSS submission.

Due to my successful experience in creating a new Jekyll RSS template for my blog, I thought it would be easy to customize the RSS template of Hugo blogs to make it suitable for R-bloggers. I was WRONG. Hugo has stricter rules for modifying RSS templates1, and it took me a while to figure out how to modify the category/tag RSS templates.

By default, Hugo generates different RSS feeds for each section and taxonomy2. I will write about how to modify a subset of them to create category- or tag-specific RSS feeds for R-bloggers, i.e, making the field of the RSS feed display full content, rather than an excerpt, of a post.

Overwriting the default RSS Template

You won’t find any RSS template files shipped with Hugo themes (at least Hugo Acadimic theme doesn’t). RSS files (index.xml) will be generated (in public/) according to the default RSS template.

To override the default RSS template without touching the theme template,

  1. Copy the default RSS template

  2. Change {{ .Summary | html }} to {{ .Content | html }}

  3. Create subdirectories in layouts/ and save the RSS template files (name them rss.xml) in them (one for each subdirectory).

Putting RSS templates in different subdirectories will have effects on different kind of RSS feeds. The file name (e.g. rss.xml) of the template also matters and can’t be arbitrary. Read Hugo’s documentation on Lookup Order for RSS Templates for details3.

An Example: Hugo Acadimic Theme

I use Hugo Acadimic theme as an example to set two kinds of RSS templates:

  1. A template that causes RSS feeds under every category to display full content of the posts
  2. A template with fields to capture all post tags

Directory Tree

Below is the directory structure (simplified) after adding RSS templates to Hugo Acadimic theme. I added and modified rss.xml in /layouts/post/ and /layouts/categories/. Then /docs/post/index.xml and /docs/categories/r/index.xml get generated according to /layouts/post/rss.xml and /layouts/categories/rss.xml, respectively. Note that /docs is /public by default, when publishDir = "docs" is not set in /config.toml. Using /docs allows me to publish Hugo blog on GitHub Pages.

/
├── index.Rmd
├── Hugo-RSS.Rproj
├── config.toml
├── content/
│   ├── home/
│   └── post/
│       ├── 2015-07-23-r-rmarkdown.html
│       └── 2015-07-23-r-rmarkdown.Rmd

├── layouts/
│   ├── post/
│   │   └── rss.xml          # Add/Modify RSS template here
│   └── categories/
│       └── rss.xml          # Add/Modify RSS template here

├── docs/                    # change 'public' to 'docs' in /config.toml
│   ├── index.html
│   ├── index.xml
│   ├── home/
│   ├── post/
│   │   ├── index.xml        # set in '/layouts/post/rss.xml' 
│   │   ├── index.html
│   │   ├── 2015-07-23-r-rmarkdown/
│   │   └── 2015-07-23-r-rmarkdown_files/
│   └── categories/
│       └── r/
│           ├── index.xml    # set in '/layouts/categories/rss.xml'      
│           └── index.html
└── themes/
    └── hugo-academic/

Source Code

You can check out the complete directory at GitHub and the web page generated from this repo.

  1. For Jekyll blogs, creating a new RSS feed is just like creating a new page in the blog, and one can even use custom file names for RSS template files (such as feed.rbloggers.xml). 

  2. See https://gohugo.io/templates/rss/ for more information. 

  3. I have to admit that I don’t completely understand Hugo’s RSS template look up order. I can’t predict the behaviors of Hugo precisely. 

To leave a comment for the author, please follow the link and comment on their blog: Yongfu's Blog.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers

Sponsors

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)