Notes on Hugo 0.60

config.toml

To ensure I can use raw HTML, for example when embedding a YouTube video, I had to set unsafe to true for the goldmark renderer:

1
2
[markup.goldmark.renderer]
  unsafe = true

I wanted my posts URLs to be at the root of the domain, and not at posts/xxxx, this can be achieved in config:

1
2
[permalinks]
  posts = "/:filename/"

I can dynmanically add the current year to the copyright notice by defining my copyright in the config:

1
copyright = "Copyright 2015-{year} - Largely a reference for me, you might find it useful. Sherry?"

Then in my footer.html partial:

1
<small>&copy; {{ replace .Site.Copyright "{year}" now.Year }}</small>

Archetypes

Archetypes pre-fill the frontmatter for a given content type. For example, I have a posts folder in my content folder, and I have a corresponding posts.md in my archetypes folder. The posts.md file looks like this:

1
2
3
4
5
6
7
8
---
title: "{{ replace .Name "-" " " | title }}"
description: ""
date: {{ .Date }}
tags: []
draft: false
---

I have added description and tags to make sure they appear in every new post file, and I’ve set draft to false as I want to explicit set it to true in the rare instance I create a draft and commit it.

Data

As a bit of fun I keep a page with lists of things I like. These are stored as toml under data/xxx.toml, defined in the following way:

1
2
3
4
5
6
7
list = [
  "Blade Runner (1982)",
  "Amelie",
  "Goodbye Lenin (2003)",
  "Bob La Flambeur (1956)",
  "2001 A Space Odyssey"
]

Then I have a shortcode in my <theme>/layouts/shortcodes folder called list.html:

1
2
3
4
5
6
7
8
9
{{ $data := index .Site.Data (.Get 0) }}
<section>
  <h2>{{ .Get 1 }}</h2>
  <ul class='list'>
  {{ range sort $data.list }}
    <li>{{ .  | markdownify }}</li>
  {{ end }}
  </ul>
</section>

The first line above is looking for the first argument passed to the shortcode which will be the name of the data file.

Line 5 grabs and sorts the list, by making sure every list toml file defines the array as list=, this will work for any data file I throw at it:

1
2
3
4
  {{< list "films" "Films" >}}
  {{< list "music" "Music" >}}
  {{< list "books" "Books" >}}
  {{< list "places" "Places I've been" >}}

Pagination

First create a paginator using your where selector and Paginate. Here I am getting all my ‘posts’ and sorting by date, newest first:

1
{{ $paginator := .Paginate (where .Site.RegularPages "Section" "posts").ByDate.Reverse }}

Now in my range I can use the paginator:

1
2
3
{{ range $paginator.Pages }}
...
{{ end }}

The Paginator exposes lots of fun things for us to use. For next and previous links (which I find ambiguous, preferring newer and older), we can test if there is a next or a previous and render some markup accordingly:

1
2
3
4
5
6
{{ if $paginator.HasPrev }}
  <a href="{{ $paginator.Prev.URL }}">&laquo; Newer</a>
{{ end }}
{{ if $paginator.HasNext }}
  <a href="{{ $paginator.Next.URL }}">Older &raquo;</a>
{{ end }}

The number of items per page can be set in the config.toml:

1
paginate = 5