Featured image of post Full-Content RSS in Hugo

Full-Content RSS in Hugo

TL;DR: Use *.rss.xml to override Render Hook and Shortcode in rss.

Long time ago, I found out that most of my blog RSS generated by Hugo only has a summary, unlike other static page generators that support full-content RSS which can be read directly in an RSS reader.
So I searched for Hugo’s documentation and other people’s solutions on the Internet, and most of them mentioned the {{ .Summary }} part of the custom RSS template.

patch
--- a/rss.xml
+++ b/rss.xml
@@ -32,7 +32,7 @@
       <pubDate>{{ .Date.Format "Mon, 02 Jan 2006 15:04:05 -0700" | safeHTML }}</pubDate>
       {{ with .Site.Author.email }}<author>{{.}}{{ with $.Site.Author.name }} ({{.}}){{end}}</author>{{end}}
       <guid>{{ .Permalink }}</guid>
-      <description>{{ .Summary | html }}</description>
+      <description>{{ .Content | html }}</description>
     </item>
     {{ end }}
   </channel>

At the same time, some themes are very “friendly”, they provided the rssFullContent configuration parameter directly, and implemented a template that supports full-content RSS. The theme I’m using now provides this feature.

However, if you try a little bit or read Hugo’s issue 1, you will find that the full-content RSS obtained by directly using {{ .Content }} is shit.

Simply using {{ .Content }} will output the content to RSS as if generating an HTML page, which will result in a lot of redundant content in RSS. The most obvious is the line numbers in the code area (in hexo, this feature is im), which behaves very badly in some RSS readers and telegrah.

The main reason for this is because of the Render Hook and Shortcode generate a lot of HTML-only friendly elements in HTML generation. In RSS output, other reader-friendly formats should be used.

e.g: For code, use a Hook render-codeblock.rss.xml to override all codeblock settings made in HTML:

html
<pre><code>{{ .Inner }}</code></pre>

Tip

Shortcode can also rewrite RSS output. For example, <blockquote> is used in RSS to replace the <div class="foo"> with css in the page.

In addition, pictures imported using relative paths in markdown need to be modified to absolute paths, otherwise the reader cannot find the pictures. A simple Hook render-image.rss.xml is required to transform the links.

html
{{- $url := urls.Parse .Destination -}}
{{- if not (or (eq $url.Scheme "http") (eq $url.Scheme "https")) -}}
    {{- $result := .Page.Resources.GetMatch (printf "%s" (.Destination | safeURL)) -}}
    {{- with $result -}}
        <img src="{{ .RelPermalink | absURL }}" alt="{{ $.Text }}" {{ with $.Title}} title="{{ . }}"{{ end }} />
    {{- end -}}
{{- end -}}
Licensed under CC BY-NC-SA 4.0
Last updated on May 06, 2023 12:22 +0800

Note

Comments are not available in this language. Please switch to 简体中文 to view or leave comments.