<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title>Hugo on sunbro.dev</title><link>https://sunbro.dev/tags/hugo/</link><description>Recent content in Hugo on sunbro.dev</description><generator>Hugo -- 0.131.0</generator><language>en</language><lastBuildDate>Sun, 11 Aug 2024 22:40:38 +0200</lastBuildDate><atom:link href="https://sunbro.dev/tags/hugo/index.xml" rel="self" type="application/rss+xml"/><item><title>Cleaning up the RSS feed</title><link>https://sunbro.dev/posts/2024-08-11-fixing-rss-papermod/</link><pubDate>Sun, 11 Aug 2024 18:00:00 +0100</pubDate><guid>https://sunbro.dev/posts/2024-08-11-fixing-rss-papermod/</guid><description>Updating PaperMod&amp;rsquo;s RSS template to fit my needs</description><content:encoded><![CDATA[<h2 id="motivation">Motivation</h2>
<p>After a 4 year hiatus, I&rsquo;m finally coming back around to this blog and doing some cleaning before writing again.
Part of that cleaning includes making sure the 
<a href="/index.xml/" >RSS feed</a> works as expected.
To do so, it needs to check off the following:</p>
<ul>
<li><input checked="" disabled="" type="checkbox"> Provide valid RSS content</li>
<li><input disabled="" type="checkbox"> Include the posts&rsquo; contents so they can easily be read with RSS readers</li>
<li><input disabled="" type="checkbox"> Exclude pages that are not posts</li>
</ul>
<p>The first box is ticked by default thanks to Hugo&rsquo;s RSS generator</p>
<h2 id="task-1-including-the-posts-contents">Task 1: Including the post&rsquo;s contents</h2>
<p>Looking at PaperMod&rsquo;s RSS layout, I noticed the following code:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-xml" data-lang="xml"><span class="line"><span class="cl">{{- if and site.Params.ShowFullTextinRSS .Content }}
</span></span><span class="line"><span class="cl">    <span class="nt">&lt;content:encoded&gt;</span>{{ (printf &#34;<span class="cp">&lt;![CDATA[%s]]&gt;</span>&#34; .Content) | safeHTML }}<span class="nt">&lt;/content:encoded&gt;</span>
</span></span><span class="line"><span class="cl">{{- end }}
</span></span></code></pre></div><p>This might&rsquo;ve been added some time in the 4 years since I started this blog and seems to solve all of our problems.
One update to our site&rsquo;s config and voilà, the RSS feed&rsquo;s XML includes our posts&rsquo; content in it!</p>
<p>One task down, one to go!</p>
<h2 id="task-2-excluding-specific-pages">Task 2: Excluding specific pages</h2>
<h3 id="the-easy-way">The easy way</h3>
<p>As pointed out on 
<a href="https://discourse.gohugo.io/t/rss-feed-only-for-blog-posts/47558" target="_blank" >Hugo&rsquo;s Discourse</a> there is a straightforward way to generate an RSS feed only for blog posts:</p>
<ol>
<li>Update the <code>output</code> section of your site&rsquo;s config file so it does not include RSS</li>
</ol>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-diff" data-lang="diff"><span class="line"><span class="cl">[...]
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">[output]
</span></span><span class="line"><span class="cl"><span class="gd">- home = [&#34;HTML&#34;, &#34;RSS&#34;, &#34;JSON&#34;]
</span></span></span><span class="line"><span class="cl"><span class="gd"></span><span class="gi">+ home = [&#34;HTML&#34;, &#34;JSON&#34;]
</span></span></span><span class="line"><span class="cl"><span class="gi"></span>
</span></span><span class="line"><span class="cl">[...]
</span></span></code></pre></div><ol start="2">
<li>Add an <code>_index.md</code> file to your <code>/posts</code> subfolder with the following front matter:</li>
</ol>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-yml" data-lang="yml"><span class="line"><span class="cl"><span class="l">+++</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w"></span><span class="l">title = &#34;Posts&#34;</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w"></span><span class="l">outputs = [&#34;HTML&#34;, &#34;RSS&#34;]</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w"></span><span class="l">+++</span><span class="w">
</span></span></span></code></pre></div><ol start="3">
<li>We now have an RSS feed at <code>https://sunbro.dev/posts/index.xml</code>!</li>
</ol>
<p>&hellip; but there&rsquo;s one problem: this breaks the feed for anyone already subscribed to the current feed at <code>https://sunbro.dev/index.xml</code>. That URL now 404s.</p>
<h3 id="the-correct-way">The &ldquo;correct&rdquo; way</h3>
<style type="text/css">
     
    .notice {
        --title-color: #fff;
        --title-background-color: #6be;
        --content-color: #444;
        --content-background-color: #e7f2fa;
    }

    .notice.info {
        --title-background-color: #fb7;
        --content-background-color: #fec;
    }

    .notice.tip {
        --title-background-color: #5a5;
        --content-background-color: #efe;
    }

    .notice.warning {
        --title-background-color: #c33;
        --content-background-color: #fee;
    }

     
    @media (prefers-color-scheme:dark) {
        .notice {
            --title-color: #fff;
            --title-background-color: #069;
            --content-color: #ddd;
            --content-background-color: #023;
        }

        .notice.info {
            --title-background-color: #a50;
            --content-background-color: #420;
        }

        .notice.tip {
            --title-background-color: #363;
            --content-background-color: #121;
        }

        .notice.warning {
            --title-background-color: #800;
            --content-background-color: #400;
        }
    }

    body.dark .notice {
        --title-color: #fff;
        --title-background-color: #069;
        --content-color: #ddd;
        --content-background-color: #023;
    }

    body.dark .notice.info {
        --title-background-color: #a50;
        --content-background-color: #420;
    }

    body.dark .notice.tip {
        --title-background-color: #363;
        --content-background-color: #121;
    }

    body.dark .notice.warning {
        --title-background-color: #800;
        --content-background-color: #400;
    }

     
    .notice {
        padding: 18px;
        line-height: 24px;
        margin-bottom: 24px;
        border-radius: 4px;
        color: var(--content-color);
        background: var(--content-background-color);
    }

    .notice p:last-child {
        margin-bottom: 0
    }

     
    .notice-title {
        margin: -18px -18px 12px;
        padding: 4px 18px;
        border-radius: 4px 4px 0 0;
        font-weight: 700;
        color: var(--title-color);
        background: var(--title-background-color);
    }

     
    .icon-notice {
        display: inline-flex;
        align-self: center;
        margin-right: 8px;
    }

    .icon-notice img,
    .icon-notice svg {
        height: 1em;
        width: 1em;
        fill: currentColor;
    }

    .icon-notice img,
    .icon-notice.baseline svg {
        top: .125em;
        position: relative;
    }
</style><div class="notice info" >
    <p class="notice-title">
        <span class="icon-notice baseline">
            <svg xmlns="http://www.w3.org/2000/svg" viewBox="92 59.5 300 300">
  <path d="M292 303.25V272c0-3.516-2.734-6.25-6.25-6.25H267v-100c0-3.516-2.734-6.25-6.25-6.25h-62.5c-3.516 0-6.25 2.734-6.25 6.25V197c0 3.516 2.734 6.25 6.25 6.25H217v62.5h-18.75c-3.516 0-6.25 2.734-6.25 6.25v31.25c0 3.516 2.734 6.25 6.25 6.25h87.5c3.516 0 6.25-2.734 6.25-6.25Zm-25-175V97c0-3.516-2.734-6.25-6.25-6.25h-37.5c-3.516 0-6.25 2.734-6.25 6.25v31.25c0 3.516 2.734 6.25 6.25 6.25h37.5c3.516 0 6.25-2.734 6.25-6.25Zm125 81.25c0 82.813-67.188 150-150 150-82.813 0-150-67.188-150-150 0-82.813 67.188-150 150-150 82.813 0 150 67.188 150 150Z"/>
</svg>

        </span>Info</p><p>This definitely not the only way to do this and there are certainly more &ldquo;Hugo-ist&rdquo; ways to do it.</p></div>

<p>Now that we know we want to preserve the current <code>/index.xml</code> let&rsquo;s give ourselves a way to exclude certain pages from it.
Let&rsquo;s add a new entry on to our pages&rsquo; config: <code>RSSExclude</code>. You should add it to all the pages you want to exclude from the RSS feed.</p>
<p>For example in my <code>projects.md</code> that I want to exclude I added:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-yml" data-lang="yml"><span class="line"><span class="cl"><span class="l">+++</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w"></span><span class="l">title = &#34;Projects&#34;</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w"></span><span class="p">[</span><span class="l">...]</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w"></span><span class="l">RSSExclude = true</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w"></span><span class="l">+++</span><span class="w">
</span></span></span></code></pre></div><p>Now, let&rsquo;s customise PaperMod&rsquo;s <code>index.xml</code> layout to take it into account.
Copy the theme&rsquo;s file into your <code>/layouts/_default/</code> and edit it as follows:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-diff" data-lang="diff"><span class="line"><span class="cl"><span class="gd">- {{- if and (ne .Layout `search`) (ne .Layout `archives`) }}
</span></span></span><span class="line"><span class="cl"><span class="gd"></span><span class="gi">+ {{- if ne .Params.RssExclude true }}
</span></span></span><span class="line"><span class="cl"><span class="gi"></span>    &lt;item&gt;
</span></span><span class="line"><span class="cl">        [...]
</span></span><span class="line"><span class="cl">    &lt;/item&gt;
</span></span><span class="line"><span class="cl">{{- end }}
</span></span></code></pre></div><p>Instead of only excluding pages with the <code>search</code> or <code>archives</code> layouts we now exclude all pages where <code>RSSExclude</code> is <code>true</code>.</p>
<p>Task two, check!</p>
<h2 id="conclusion">Conclusion</h2>
<p>I&rsquo;m happy to see this was much easier than I thought.
Although I wish PaperMod natively supported excluding pages from the RSS feed, this has only motivated me to tweak the theme&rsquo;s layouts even further in the future.</p>
]]></content:encoded></item><item><title>Adding video support to the Hugo Terminal theme</title><link>https://sunbro.dev/posts/2020-05-20-adding-video-support-to-hugo-terminal/</link><pubDate>Sun, 17 May 2020 22:32:00 +0100</pubDate><guid>https://sunbro.dev/posts/2020-05-20-adding-video-support-to-hugo-terminal/</guid><description>Using Hugo shortcodes to adapt Hugo to my needs</description><content:encoded><![CDATA[<h2 id="problem">Problem</h2>
<p>At the time of writing this post, this website uses 
<a href="https://gohugo.io/" target="_blank" ><strong>Hugo</strong></a> and the

<a href="https://themes.gohugo.io/hugo-theme-terminal/" target="_blank" ><strong>Terminal</strong></a> theme by 
<a href="https://twitter.com/panr" target="_blank" ><strong>panr</strong></a>. I really like
the way this theme handles content and Markdown integration, but there is no
native support for videos. This is probably due to the fact that there currently
is no way to embed videos in Markdown files, unlike images for example.</p>
<p>This problem reared its head when I was writing my first Pokémon Emerald romhack

<a href="/posts/2020-05-08-making-a-simple-romhack/" ><strong>post</strong></a>. At the time, I planned to use GIFs to illustrate the
changes made to the game. However, I quickly realised that GIFs take up a lot
of memory and can take quite long to load for readers, contrary to videos, MP4
in our case, which are up to 10 times smaller !</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">$ du -h static/captures/birch_speech_original.gif
</span></span><span class="line"><span class="cl">3.1M static/captures/birch_speech_original.gif
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">$ du -h static/captures/birch_speech_original.mp4
</span></span><span class="line"><span class="cl">332K static/captures/birch_speech_original.mp4
</span></span></code></pre></div><h2 id="solution">Solution</h2>
<p>The answer to our solution comes in the form of Hugo &ldquo;shortcodes&rdquo;. Shortcodes are
a way to bridge the gap from Markdown to HTML. Therefore, we can use one to handle
integrating videos in our posts&rsquo; Markdown files.</p>
<p>To get started create a <code>shortcodes</code> sub-directory in your <code>layouts</code> folder. Once
that is done create a <code>video.html</code> file in <code>layouts/shortcodes/</code>. The shortcode&rsquo;s
code is as follows:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-html" data-lang="html"><span class="line"><span class="cl"><span class="p">&lt;</span><span class="nt">video</span> <span class="na">autoplay</span> <span class="na">loop</span> <span class="na">muted</span> <span class="na">playsinline</span> <span class="na">aria-label</span><span class="o">=</span><span class="s">&#39;{{ .Get &#34;label&#34;}}&#39;</span> <span class="na">style</span><span class="o">=</span><span class="s">&#34;width: 100%; height: auto;&#34;</span><span class="p">&gt;</span>
</span></span><span class="line"><span class="cl">    {{ with .Get &#34;mp4&#34; }}<span class="p">&lt;</span><span class="nt">source</span> <span class="na">src</span><span class="o">=</span><span class="s">&#34;{{ . }}&#34;</span> <span class="na">type</span><span class="o">=</span><span class="s">&#34;video/mp4&#34;</span><span class="p">&gt;</span>{{ end }}
</span></span><span class="line"><span class="cl">    {{ with .Get &#34;webm&#34; }}<span class="p">&lt;</span><span class="nt">source</span> <span class="na">src</span><span class="o">=</span><span class="s">&#34;{{ . }}&#34;</span> <span class="na">type</span><span class="o">=</span><span class="s">&#34;video/webm&#34;</span><span class="p">&gt;</span>{{ end }}
</span></span><span class="line"><span class="cl">    <span class="p">&lt;</span><span class="nt">p</span><span class="p">&gt;</span> Your browser does not support video. <span class="p">&lt;/</span><span class="nt">p</span><span class="p">&gt;</span>
</span></span><span class="line"><span class="cl"><span class="p">&lt;/</span><span class="nt">video</span><span class="p">&gt;</span>
</span></span></code></pre></div><p>The syntax is explicit enough to get a basic grasp of how it works. Our shortcode
takes three possible parameters: <code>label</code>, <code>mp4</code>, and <code>webm</code>. If <code>label</code> is defined,
its contents become the <code>aria-label</code> of the video HTML element. If <code>mp4</code> or <code>webm</code>
is defined, their contents become the source for the video element.</p>
<p>The rest of the shortcode is plain HTML, with inlined style to make it &ldquo;responsive&rdquo;
and readable on mobile devices.</p>
<p>Using the shortcode in a post&rsquo;s Markdown file goes like this:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-Markdown" data-lang="Markdown"><span class="line"><span class="cl">{{<span class="p">&lt;</span> <span class="nt">video</span> <span class="na">label</span><span class="o">=</span><span class="s">&#34;this is a label&#34;</span> <span class="na">mp4</span><span class="o">=</span><span class="s">&#34;/path/to/video.mp4&#34;</span> <span class="p">&gt;</span>}}
</span></span></code></pre></div><p>I hope this helps you if you encounter the same problem, good luck!</p>
]]></content:encoded></item></channel></rss>