How I Do Microposts on my Hugo Blog
Fri Sep 6, 2019

I thought I'd write a post about how I added microposts to my blog, so here it is.

Hugo seems to have support for microposts, if you have a microposts directory in content it will read it just like posts. However, I wanted my microposts to be displayed in a tweet-like format rather than as a link.

I did this using a simple template change:


  {{ range (where $pages "Section" "!=" "") }}
    <span class="date">{{ .Date.Format "2006-01-02" }}</span>
    {{ if .Section | eq "microposts"}}
    {{ .Content }}
    {{ else }}
    <b>Post: </b><a href="{{ .Permalink }}"> {{ .Title | markdownify }}</a>
    {{ end }}
  {{ end }}

($pages is defined earlier in the template, it's equivalent to .Site.RegularPages on the homepage)

This solved publishing microposts, but writing them was still a process like writing a full post (hugo new, editing in an editor, publishing) and this is too much inconvenience for such small posts.

So I wrote a python script:

#!/usr/bin/env python
import datetime
from subprocess import run

t ="T","seconds") + "+01:00"
fn = f"[path to your site]/content/microposts/{t}.md"

header = \
title: \"\"
date: {t}

with open(fn, "w+") as f:
run(["nvim", fn])

Of course, replace nvim with the editor of your choice. Stick this script somewhere in your PATH and you will be able to just run micropost whenever you have something to say.

I hope this has been informative to anyone looking to add microposts to their hugo blog. Parts of this may need to be revised to run on other platforms/themes/setups but it's an idea.

photos · music · code · posts · notes · about · home