aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNicholas Johnson <>2026-09-18 00:00:00 +0000
committerNicholas Johnson <>2026-09-19 16:15:23 -0400
commit424afe0f8fed829758cc4d09722b4cfd98268734f82b799962369478389cf5dc (patch)
treea43292a8f9bdf43393076cd098ef395df4f651a92f6f32fc0bb396a856b9b0c9
parent8963ab91a272a57120273703dc2716aae16669dca18fe2c39b9fb18268a3e275 (diff)
downloadhugo-theme-journal-424afe0f8fed829758cc4d09722b4cfd98268734f82b799962369478389cf5dc.tar.gz
hugo-theme-journal-424afe0f8fed829758cc4d09722b4cfd98268734f82b799962369478389cf5dc.zip
Choose the best audio version of an entry
Both templates took element 0 of the entry's audio resources, so an entry carrying more than one encoding used whichever filename sorted first, with no way to express a preference. An entry with a careful encoding and an mp3 beside it would commonly have offered the mp3. Order the resources by how they sound instead. Public listening tests put Opus ahead of the field at the bitrates spoken word uses, with Vorbis and AAC close behind it and MP3 last; the lossless formats come after the lossy ones rather than before, since a browser that can play FLAC would otherwise fetch several times the bytes for no audible gain. Formats the theme does not rank keep Hugo's own order at the end. The capsule links the first of them, since a Gemini client cannot choose between sources and a reader wants one file to fetch. The website offers them all as the alternative sources that the audio element exists to choose between, best first, so a browser plays the best encoding it understands. Hugo reports .opus, .oga and .ogg alike as audio/ogg, so the ordering keys off the file extension rather than the media type. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
-rw-r--r--layouts/_partials/audio.html32
-rw-r--r--layouts/single.gmi4
-rw-r--r--layouts/single.html6
3 files changed, 37 insertions, 5 deletions
diff --git a/layouts/_partials/audio.html b/layouts/_partials/audio.html
new file mode 100644
index 0000000..a99e9b3
--- /dev/null
+++ b/layouts/_partials/audio.html
@@ -0,0 +1,32 @@
+{{/* Returns the page's audio resources, best sounding first, so that a
+ browser takes the best encoding it can play and the capsule links
+ the best single file. Public listening tests put Opus ahead of the
+ field at the bitrates spoken word uses, with Vorbis and AAC close
+ behind it and MP3 last. Lossless comes after the lossy codecs
+ rather than before, since a browser that can play FLAC would
+ otherwise fetch several times the bytes for no audible gain.
+
+ Hugo reports .opus, .oga and .ogg alike as audio/ogg, so the
+ extension is what tells the codecs apart. Formats not listed here
+ keep Hugo's own order at the end. Returns an empty slice when there
+ is no audio. */ -}}
+
+{{ $preferred := slice ".opus" ".ogg" ".oga" ".m4a" ".aac" ".mp3" ".flac" ".wav" -}}
+{{ $audio := where .Resources "MediaType.MainType" "audio" -}}
+{{ $ordered := slice -}}
+
+{{ range $extension := $preferred -}}
+ {{ range $audio -}}
+ {{ if eq (lower (path.Ext .Name)) $extension -}}
+ {{ $ordered = $ordered | append . -}}
+ {{ end -}}
+ {{ end -}}
+{{ end -}}
+
+{{ range $audio -}}
+ {{ if not (in $preferred (lower (path.Ext .Name))) -}}
+ {{ $ordered = $ordered | append . -}}
+ {{ end -}}
+{{ end -}}
+
+{{ return $ordered }}
diff --git a/layouts/single.gmi b/layouts/single.gmi
index 29df881..2cee33d 100644
--- a/layouts/single.gmi
+++ b/layouts/single.gmi
@@ -8,9 +8,9 @@
{{ partial "pageinfo.gmi" . }}
{{- end -}}
-{{ with index (where .Resources "MediaType.MainType" "audio") 0 }}
+{{ with partial "audio.html" . }}{{ with index . 0 }}
=> {{ .RelPermalink }} {{ emojify ":link:" }} {{ i18n "downloadAudio" $.Title }}
-{{- end }}
+{{- end }}{{ end }}
{{ $content := chomp (.RawContent) -}}
{{ $scratch := newScratch -}}
diff --git a/layouts/single.html b/layouts/single.html
index 9ea21f4..bb9b10e 100644
--- a/layouts/single.html
+++ b/layouts/single.html
@@ -11,10 +11,10 @@
{{- if (.Page.Params.pageinfo | default true) }}
<p class="entry__pageinfo">{{ partial "pageinfo.html" . }}</p>
{{- end -}}
- {{ with index (where .Resources "MediaType.MainType" "audio") 0 }}
+ {{ with partial "audio.html" . }}
<audio class="entry__audio" controls preload="metadata" aria-label="{{ i18n "audioVersion" $.Title }}">
- <source src="{{ .RelPermalink }}" type="{{ .MediaType }}">
- {{ i18n "audioUnsupported" }}
+ {{ range . }}<source src="{{ .RelPermalink }}" type="{{ .MediaType }}">
+ {{ end }}{{ i18n "audioUnsupported" }}
</audio>
{{- end }}
</div>