<?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>Shortcodes on sunbro.dev</title><link>https://sunbro.dev/categories/shortcodes/</link><description>Recent content in Shortcodes on sunbro.dev</description><generator>Hugo -- 0.131.0</generator><language>en</language><lastBuildDate>Thu, 09 Mar 2023 22:24:07 +0000</lastBuildDate><atom:link href="https://sunbro.dev/categories/shortcodes/index.xml" rel="self" type="application/rss+xml"/><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>