Skip to content

Instantly share code, notes, and snippets.

@cathelijne
Last active May 21, 2022 20:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cathelijne/cc7b3b00725f70605321b138dc16f2a8 to your computer and use it in GitHub Desktop.
Save cathelijne/cc7b3b00725f70605321b138dc16f2a8 to your computer and use it in GitHub Desktop.
As a beginner in Hugo and Golang, some stuff that looks simple takes a surprisingly long time to figure out.
I'm probably not the only one who tried to create a list of tags on a category page, or wondered if it's
possible to simply list everything in . in a simple manner.
I'll add more snippets as I go and learn more, and update the ones that can be done in a better way.
It's a loose collection, but maybe someone else has a use for these.
See also:
- https://github.com/cathelijne/hugo-theme-huguette, Huguette, a boilerplate theme to get you started and nothing more
- https://github.com/cathelijne/hugo-huguette-example, A one-click Netlify install of Hugo with Huguette as a theme
- https://github.com/cathelijne/hugo-dummy-content, Dummy content for Hugo: 100 pages in 5 sections with categories and tags
<!-- Get the tags for the current context -->
{{ $.Scratch.Set "tags" (slice) }}
{{ range .Data.Pages }}
{{ range .Params.tags }}
{{ $.Scratch.Add "tags" . }}
{{end}}
{{end}}
<!-- Make a frequency map of the tags in this context -->
{{ $.Scratch.Set "freq" dict }}
{{ range $.Scratch.Get "tags" }}
{{ $.Scratch.SetInMap "freq" . 0}}
{{ end }}
{{ range $.Scratch.Get "tags" }}
{{ $.Scratch.SetInMap "freq" . (add (index ($.Scratch.Get "freq") .) 1)}}
{{ end }}
<!-- Get all tags and show only if in current context -->
<ul>
{{ range .Site.Taxonomies.tags.ByCount }}
{{ if in ($.Scratch.Get "tags" |uniq) .Name }}
<li>
<a href="{{ .Page.Permalink }}">{{ .Page.Title }}</a> ({{ index ($.Scratch.Get "freq") .Name }}/{{ .Count }})
</li>
{{ end }}
{{ end }}
</ul>
{{ if or ($.Params.debug) ($.Site.Params.debug) }}
{{ $debugInfo := dict }}
{{ $debugSite := (.Site |jsonify|unmarshal) }}
{{ $debugSiteParams := (dict ".Params" .Site.Params) }}
{{ $debugSitePlusSiteParams := (dict ".Site" (merge $debugSite $debugSiteParams)) }}
{{ $debugParams := (dict ".Params" .Params) }}
{{ $debugPage := (dict ".Page" .Page) }}
{{ $debugDot := dict
".Aliases" .Aliases
".Date" .Date
".Description" .Description
".Draft" .Draft
".Keywords" .Keywords
".Kind" .Kind
".Layout" .Layout
".Name" .Name
".OutputFormats" .OutputFormats
".Permalink" .Permalink
".ReadingTime" .ReadingTime
".RelPermalink" .RelPermalink
".Resources" .Resources
".ResourceType" .ResourceType
".Summary" .Summary
".Section" .Section
".Title" .Title
".Type" .Type
".Weight" .Weight
".WordCount" .WordCount
}}
{{ $debugInfo := merge $debugSitePlusSiteParams $debugParams $debugDot}}
<div class="debug">
<details class="card">
<summary>Debug Info</summary>
<details class="card">
<summary>All in one</summary>
<pre><code class="language-json" data-lang="json">{{ $debugInfo |jsonify (dict "indent" " ")}}</code></pre>
</details>
<details class="card">
<summary>.Site</summary>
<pre><code class="language-json" data-lang="json">{{ $debugSite |jsonify (dict "indent" " ")}}</code></pre>
</details>
<details class="card">
<summary>.Site.Params</summary>
<pre><code class="language-json" data-lang="json">{{ $debugSiteParams |jsonify (dict "indent" " ")}}</code></pre>
</details>
<details class="card">
<summary>.Params</summary>
<pre><code class="language-json" data-lang="json">{{ $debugParams |jsonify (dict "indent" " ")}}</code></pre>
</details>
<details class="card">
<summary>.</summary>
<pre><code class="language-json" data-lang="json">{{ $debugDot |jsonify (dict "indent" " ")}}</code></pre>
</details>
<details class="card">
<summary>debug.Dump</summary>
<pre><code class="language-json" data-lang="json">{{ debug.Dump . }}</code></pre>
</details>
</div>
</div>
{{ end }}
#!/bin/bash
# Generate a content markdownfile with some extra frontmatter (introduction, categories, tags)
# Adapt to your liking
# Needs bash, curl, jq and sed
title=$(curl "https://asdfast.beobit.net/api/?type=word" |jq .text|sed 's/\.//')
subtitle=$(curl "https://asdfast.beobit.net/api/?type=word&length=9" |jq .text|sed 's/\.//g')
introduction=$(curl "https://asdfast.beobit.net/api/?type=paragraph&length=1" |jq .text)
clist=("Dolor" "Veritas" "Vino" "Canis" "Terra" "Domus")
tlist=("maecenas" "vitae" "rhoncus" "enim" "donec" "malesuada" "sem" "mollis" "neque" "pharetra" "sed" "cras" "ullamcorper" "semper" "sem" "vitae" "porta" "phasellus" "tincidunt" "tempor" "arcu " "eget" "porttitor" "orci" "vulputate" "amet" "urna" "congue" "volutpat" "sed" "velit" "etiam" "quis" "nibh" "porta" "imperdiet" "risus" "quis" "vehicula" "vivamus" "hendrerit")
tlist=( $(shuf -e "${tlist[@]}") )
body=$(curl "https://jaspervdj.be/lorem-markdownum/markdown.txt?fenced-code-blocks=on&no-wrapping=on")
echo "---"
printf "date: %s\n" "$(date +%Y-%m-%dT%H:%M:%S)"
printf "title: %s\n" "$title"
printf "subtitle: %s\n" "$subtitle"
printf "introduction: %s\n" "$introduction"
printf "categories: [\"%s\"]\n" "$(shuf -e ${clist[@]} -n1)"
printf "tags: [\"%s\",\"%s\",\"%s\",\"%s\"]\n" "${tlist[@]:1:4}"
printf "debug: false\n"
printf "ignore: false\n"
printf "draft: false\n"
echo "---"
printf "%s\n" "$body"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment