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
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:
data/
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.
can be interesting for you
@betz - Alrighty, a few things:
./scripts/commits.sh content data/commits
baseof.html
template, you could stick the below code in your bottom.html
partial since this partial is used on most pagesfile.md.json
to file.json
, since the data file cannot be read properly with the .md
in theredata/commits/
dir, since hugoβs readDir
function is not recursive.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
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.