Hugo

Run command for every page

Hi there!

I am looking for a way to have a command run for every page hugo generates.
Is this possible? Hook into the page building process.
That would be awesome.

Cheers,
Tom

What in particular are you wanting to do?

goal would be to run a curl command requesting all commits of that .md file on the Gitlab commits API. To generate a changelog for each page independently.

If you only needed the most recent commit, you could use hugo’s git info variables.

But if you need all commits, then yeah you’d need a build script, which could do something like:

  1. The curl statement to get all commits
  2. Put the output in a CSV/JSON/TOML/YAML file somewhere under data/
  3. Have logic in your template that parses the data file and displays all commits
  4. Build your site as usual

yeah, thats what i am doing at https://hsbxl.be/changelog
But i want to run this on certain pages.
So a hook on every page build would be swhat i need.

I see. Well, not sure how to do a hook on every page (or if that is possible with hugo).

Another idea: use a bash script to loop through each content page. Within the loop, make your curl call for each page name. Then in your single page template, check the .File.LogicalName against the page name in the data/ folder, and if they match, then show changelog.

I made some good progress on this.
I generate a .json file with git log data in the data directory for each markdown file in content.

content
β”œβ”€β”€ changelog.md
β”œβ”€β”€ contact.md
β”œβ”€β”€ events
β”‚ β”œβ”€β”€ byteweek
β”‚ β”‚ β”œβ”€β”€ 2019
β”‚ β”‚ β”‚ β”œβ”€β”€ blockstack.md

I get something like this generated:

data/commits
β”œβ”€β”€ changelog.md.json
β”œβ”€β”€ contact.md.json
β”œβ”€β”€ events
β”‚ β”œβ”€β”€ byteweek
β”‚ β”‚ β”œβ”€β”€ 2019
β”‚ β”‚ β”‚ β”œβ”€β”€ blockstack.md.json

now, I need to get a range from that json file of the current .md Tfile.
The path is something like β€œdata/commits/{{ .File.Path }}.json”
I already did play with a range from data, something like β€œ{{ range $.Site.Data.changelog }}”, but how would I get this dynamic path in that range?

BTW, this is the bash script i wrote to generate the json files. I run it before my build process in gitlab-ci.

1 Like

can be interesting for you

@betz - Alrighty, a few things:

  • Update the script usage to be: ./scripts/commits.sh content data/commits
  • Since you’re not using a baseof.html template, you could stick the below code in your bottom.html partial since this partial is used on most pages
  • Update the script to rename JSON files from file.md.json to file.json, since the data file cannot be read properly with the .md in there
  • Update the script to put all JSON files in data/commits/ dir, since hugo’s readDir function is not recursive
  • Use .date instead of .created_at, .commit instead of .id, and .author instead of .author_name, since these fields were different than those of allcommits.json

In your bottom.html partial (or somewhere where all single pages will inherit this):

{{ $currentPage := . }}
{{ $jsonFile := "" }}
{{ $files := readDir "data/commits/" }}

{{ range $files }}
  {{ if in .Name $currentPage.File.BaseFileName }}
    {{ $jsonFile = $currentPage.File.BaseFileName }}
  {{ end }}
{{ end }}

{{ if ne $jsonFile "" }}
  {{ $data := index $.Site.Data.commits $jsonFile }}
  <ul>
  {{ range $data }}
    <li>
      {{ .date }}: <a href="https://gitlab.com/hsbxl/site/commit/{{ .commit }}">
      <strong>{{ trim .message "\n" }}</strong>
      </a> ({{ .author }})
    </li>
  {{ end }}
  </ul>
{{ end }}

To use your contact.md page as an example, it will map to a data file at data/commits/contact.json

oh thanks looks good.
I will try to find a solution that works for identical filenames.
Lot of .md files are in fact _index.md
Maybe md5 the path

For the record, this is the updated script that generates json files of each page.
it is in production now, and works perfect.

Cool – I see you updated your bottom partial too :slightly_smiling_face:

yeah; works smooth now with gitlab-ci.
An example page, on the bottom you have the changelog accordion:
https://hsbxl.be/projects/jailed_ci_user

I would like to run bash script - for minifying, zipping and uploading to AWS S3 (with setting headers) updated files.