Skip to content

Commit 10b7c83

Browse files
committed
til: hugo builds human.json
1 parent 344b45c commit 10b7c83

File tree

1 file changed

+93
-0
lines changed

1 file changed

+93
-0
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
---
2+
title: "How to automatically build a human.json file in Hugo"
3+
date: 2026-03-20
4+
tags:
5+
- human.json
6+
- hugo
7+
---
8+
9+
As I mentioned the other day, I recently added a [human.json file](/til/humanjson/) to this site.
10+
11+
However, it became a bit annoying adding new vouches to the json manually.
12+
13+
So today I sat down and made it get created automatically during my page build powered by [Hugo](https://gohugo.io), based on a list of vouches stored in a `vouches.yaml` data file[^1].
14+
15+
For this I first created said data file in `data/vouches.yaml`:
16+
17+
``` yaml
18+
- url: https://food.foosel.net
19+
date: "2026-03-14"
20+
- url: https://chaos.social/@foosel
21+
date: "2026-03-14"
22+
- url: https://octoprint.org
23+
date: "2026-03-14"
24+
# ...
25+
```
26+
27+
Then I created a new output format `humanjson` in my `config.yaml`, making sure to also add it to the home page:
28+
29+
``` yaml
30+
outputs:
31+
home:
32+
- HTML
33+
- JSON
34+
- humanjson
35+
# ...
36+
37+
outputFormats:
38+
# ...
39+
humanjson:
40+
mediaType: application/json
41+
baseName: human
42+
isPlainText: true
43+
notAlternative: true
44+
# ...
45+
```
46+
47+
Finally I created a template for that in `_layout/home.humanjson.json`:
48+
49+
``` go-text-template
50+
{
51+
"version": "0.1.1",
52+
"url": {{ .Site.BaseURL | jsonify }},
53+
"vouches": [
54+
{{- range $index, $vouch := hugo.Data.vouches }}
55+
{{- with $vouch }}
56+
{{- if $index }},{{end}}
57+
{
58+
"url": {{ .url | jsonify }},
59+
"vouched_at": {{ .date | jsonify }}
60+
}
61+
{{- end }}
62+
{{- end }}
63+
]
64+
}
65+
```
66+
67+
I kept the required `<link rel="/human.json">` in my `layouts/partials/extend_head.html` file that gets included on *all* pages by my theme (contrary to how the linking of output formats work, which is why I went with `notAlternative: true` for that).
68+
69+
And to make it even easier to quickly add a new vouch, I also added a new task to my [Taskfile](https://taskfile.dev):
70+
71+
``` yaml
72+
add-vouch:
73+
desc: Adds a new vouch to human.json.
74+
vars:
75+
PAGE: '{{.CLI_ARGS}}'
76+
FILE: 'data/vouches.yaml'
77+
TODAY: '{{now | date "2006-01-02"}}'
78+
cmds:
79+
- |
80+
cat >> {{.FILE}} <<EOF
81+
- url: {{.PAGE}}
82+
date: "{{.TODAY}}"
83+
EOF
84+
```
85+
86+
Now, vouching for a page[^2] is as easy as
87+
88+
```
89+
task add-vouch -- https://example.com
90+
```
91+
92+
[^1]: Targeting Hugo 0.156.0+
93+
[^2]: Even from my phone thanks to [Termux](https://termux.dev/)

0 commit comments

Comments
 (0)