I think I can do this thing, but I’m not sure, so someone shoot this down or give me a pointer, please.
I’m pulling in data via getJSON, to pull in things like commit messages or forum posts (multiple possible sources, hence no .gitinfo). I show them in the single.html fine, but what I’d like to do is sort the content on the list page by the date of the latest message pulled in from the data file.
I’m not grokking how it works.
I guess another way of asking is: can one set the variable for a piece of content that is exposed to the list context, via a data file?
The concrete example is the list at https://interi.org/awesome-lol/, a list of “awesome lists” I’m reading and logging. Some of the lists haven’t been updated in years, so I’d like to sort the lists by when they were last updated, by grabbing the date of the last commit message.
You could getJSON again in the list page, where you {{ range first 200 .Pages }}.
{{ $megalist := slice }}
{{ range first 200 .Pages }}
{{- if isset .Params "allthecodes" -}}
... getJSON stuff ...
{{ $megalist = $megalist | append (dict "page" . "lastcommit" (index $commits 0) )}}
{{ end }}
{{ end }}
<ul>
{{ range $k, $v := sort $megalist ".lastcommit.commit.committer.date" "asc" }}
<li>
{{ $v.page }}
{{ dateFormat "Jan 2 2006" $v.lastcommit.commit.committer.date}}
{{$v.lastcommit.commit.message}}
</li>
{{ end }}
</ul>
… with the assumption that you wanted the pages sorted. If however you wanted all the latest commits sorted (eg last 200 regardless of which page they were associated with) just move the $megalist | append bit into where you do the range first 5 $commits.
Page(/awesome-lol/awesome-cheminformatics.md) Jan 28 2020 add Rcpi
Page(/awesome-lol/awesome-selfhosted.md) Jan 28 2020 Merge pull request #1957 from rodavelino/fix-links
Page(/awesome-lol/awesome-diversity.md) Jan 28 2020 Update README.md
Page(/awesome-lol/awesome-jmeter.md) Jan 27 2020 Add action to run awesome-lint
Page(/awesome-lol/awesome-marvelous-amas.md) Jan 27 2020 Add Ale Muñoz (#229)
Page(/awesome-lol/awesome-mqtt.md) Jan 25 2020 add ESP-BLE2MQTT desc
Page(/awesome-lol/awesome-gravity-forms.md) Jan 24 2020 Clone Gravity Form Entry Add
Page(/awesome-lol/awesome-transit.md) Jan 23 2020 Add onebusaway-vdv-modules
Page(/awesome-lol/awesome-sre.md) Jan 23 2020 Add Resilience Roundup to Blogs section (#63)
I can kinda sorta follow the logic behind {{ $megalist = $megalist | append (dict "page" . "lastcommit" (index $commits 0) )}}. How would you suggest including the title and permalink in this slice? Do I use that slice to sort by the page in another range?
I really outta learn the concepts behind these functions, aside from the examples in the Hugo docs.
If these are the .Page’s values, you could grab them from $v.page ie
{{ $v.page.Permalink }} / {{ $v.page.Title }}
So then you could do
<li>
The last commit for Page <a href="{{$v.page.Permalink}}">{{$v.page.Title}}</a>
was on {{ dateFormat "Monday, Jan 2, 2006" $v.lastcommits.commit.committer.date }}:
<a href="{{ .html_url | safeURL }}">{{ $v.lastcommits.commit.message }}</a>
</li>
Edit:
I just realised that I’m making an assumption here that the $commits API returns a sorted list, and that therefore $commits[0] is the latest. Is that a valid assumption? If not I shall scratch my chin for a bit.
Thanks, doubly so! First, for putting together this list for me (it works!). Second, because that was the part I was missing.
dict "page" . "lastcommit" (index $commits 0)
I didn’t understand the dot usage here, my brain read it was somehow grabbing the lastcommit from the page object… but I know better now. And it makes a lot more sense, thanks!