I’m trying to render a couple of JSON outputs, given a content folder structure like so:
- content
- posts
- 2023
- post 1
- post 2
- post 3
- 2024
- post 4
- post 5
- post 6
These are the JSON outputs I’m trying to create:
posts/index.json, which includes all posts in the posts section, paginated.
posts/[year]/calendar.json, which includes all the posts in that year folder, no pagination.
I’ve tried a few different approaches, outlined in the demo repo linked below. None of them get it quite right.
I can work around it by creating a separate content folder with files like years/2023.md and years/2024.md, and then modifying the calendar.json template to retrieve posts from the posts/[year] folders as specified in the front matter. But I would love to find a way to do this without an otherwise unnecessary content section.
Instead of coding the JSON by hand, build a data structure then use the jsonify function.
git clone --single-branch -b hugo-forum-topic-52054 https://github.com/jmooring/hugo-testing hugo-forum-topic-52054
cd hugo-forum-topic-52054
hugo server
This is a really smart solution. Thank you. Your example takes advantage of a few key things I didn’t know about. Namely, the .RegularPagesRecursive and CurrentSection methods, which would have been enough to get me to my goal without anything else.
I’ve adapted your example code to my better fit my dataset. I decided to keep a separate output format for my calendar files, as it makes more sense semantically. But you’re right, it is a bit more work to set it up that way.
I also really appreciate the way you include the pagination data in the JSON output. That’s another whole set of data I didn’t realize was available, which is going to help make my frontend implementation much richer.
I am actually already doing this in my projects; I just didn’t bother with it for my demo repo, as it was a very simple example. But it’s definitely worth it as the data structure gets more complex!