Topi Kettunen


Showing Now Playing with Hugo

Posted on 6th of April 2022 | 151 words

So I wanted to add a “Now playing” footer to my posts so I can easily share the music I’m listening to. Maybe some people can find something new and exciting with that, so I implemented a very quick and poor man’s implementation for it! I only play with YouTube’s search_query URL parameter and pass in the song to that from the Hugo post’s front matter. I pass in the current song as a slice in the post’s front matter similar to this:

---
nowPlaying: ["DAF", "Liebe auf den Erste Blick"]
---

Then I just parse that in the template:

{{ if .Params.nowPlaying }}
{{ $artist := index .Params.nowPlaying 0 }}
{{ $song := index .Params.nowPlaying 1 }}
{{ $query := querify "search_query" ( printf "%s %s" $artist $song ) "search_type" "videos" }}
<a href="https://www.youtube.com/results?{{ $query | safeURL }}" target="_blank">
  {{ $artist }} - {{ $song }}
</a>
{{ end }}