Background#
Recently I created a hugo blog and started writing articles (yes, that’s the blog you are reading). Later, I found that dev.to can publish articles (as draft) from a RSS feed. In that way I don’t have to copy-and-paste my articles manually in order to publish the same item on dev.to!
😨 Problem#
Although dev.to’s puslishing from RSS feed service is great, my Hugo site is not that great (with the basic settings). When I was trying to sync my RSS feed items to dev.to, all items can be imported but only the beginning part of the content was fetched.
I know that should be something wrong with the RSS feeds generated by Hugo. The default Hugo RSS template only renders Summary
of an article and my Hugo theme is using that:
As you can see in line 73, the <description>
in my RSS feeds contain article summary only but not the full content. Is there any ways to change the Summary
here into something else? Something else that represents the full content?
😀 Solution#
Locating rss.xml
#
Usually, Hugo templates are placed under {your hugo theme}/layouts/_defualt/
. If such file does not exist, it’s time to create one. You can copy-and-paste the code from Hugo’s github or run the following curl
command under /layouts/_default
to download it:
curl "https://raw.githubusercontent.com/gohugoio/hugo/master/tpl/tplimpl/embedded/templates/_default/rss.xml" > rss.xml
Rendering the full content#
Rendering the full content of is really simple and straight-forward. What you need to do is just replacing this:
<description>{{ .Summary | html }}</description>
With this:
<description>{{- .Content | html -}}</description>
Caution: use {{-
instead of {{
. Read the bonus part if you want to know what is the difference.
Regenerating the site#
Updating the RSS template will not update your generated content automatically. Therefore, you need to re-generate your Hugo site:
hugo
You should see the changes in all RSS xml files (from git). After that, push all the code changes, remove all the fetched posts in dev.to and re-fetch them. That’s it.
Bonus: The difference between {{
and {{-
in Hugo#
You may notice that the template code is changed into {{-
from {{
. As a newbie in Hugo I checked the Hugo’s official documentation about whitespace.
… the ability to trim the whitespace from either side of a Go tag …
This -
notation is introduced in Go 1.6.
The following example (from the documentation):
<div>
{{ .Title }}
</div>
<div>
{{- .Title -}}
</div>
Which will output:
<div>
Hello, World!
</div>
<div>Hello, World!</div>
In other words, that’s the trim()
in JavaScript. That’s the String.Trim()
in C#.
Conclusion#
The default RSS feed in Hugo site shows article abstract only. By defining a custom rss.xml
template and replacing {{ .Summary | html }}
with {{- .Content | html -}}
in the <description>
tag you will get the article’s full content in the re-generated RSS xml files.