I have the following setup:
- a repo/directory (website-generator) for the hugo generator containing theme, layouts and all core site building assets.
- a repo/directory (documentation) for the site content - markdowns, media, git history etc.
- a repo/directory (website) for the build output
https://github.com/gardener see repos website-generator, documentation, website
(because I am a new user and I can post up to 2 links !??)
The site build is triggered by a shell script. It copies the content from documentation into hugo as content folder.
Next, there is a pre-build step in which a nodejs pulls the commits for each markdown and serializes them as json side-by-side with the corresponding markdowns.
The site-building then involves git-related partials that make use of .GetJSON
to read the jsons and present git-related information on the relevant pages.
Another use of resources that I have (still not published in the repos above) is .readDir
which I use to read a set of images form a provided directory and create carousel out of them dynamically.
All this worked, until I decided to skip the copy content step and instead use the --contentDir
flag and point hugo directly to documentation. As I read the documentation and the discussions here, both .GetJSON
and .readDir
are not intended to be used outside hugo’s work directory:
-
.GetJSON
docs: “the source files must reside within Hugo’s working directory” -
.readDir
docs: “Gets a directory listing from a directory relative to the current working directory.”
I can understand if .GetJSON
is dedicated to non-content data files that need not be published and therefore not necessarily part of content directory itself. Although, as you see it can have its uses too.
I’m not sure about the rationale behind restricting .readDir
though, but I assume it’s the same.
There was a proposal to use
{{ with .Page.Resources.GetMatch $jsonfile }}
{{ .Content | transform.Unmarshal }}
{{ end }}
, which seems to be able to take it over for .GetJSON
for non-hugo resident content directory. I want to confirm first with you if that is the case?
But even if it is, I am not sure how to construct the path for $jsonfile
here. In my case it’s not going to be starting with content and I don’t want to hardcode paths.
For the use case with loading image files I guess I can get away with the same approach and using .Match
with wildcards (**.jpg
)?( Provided that there is a way to resolve the path construction issues above.)
If Hugo is in no position to provide functions to read files from content directory regardless of its location, then I suppose I need to create the commits jsons in hugo’s data directory, maintain the same directory structure and map them to their markdown counterparts based on it. Unless there is a better way.
For the carousel use case using .readDir
however, I have no good ideas because the images are content and need to stay with the content directory.
I am aware that I can create a symlink from hugo’s work directory to the documentation (content) directory and solve the issue with copying content and preserve current behavior. I am asking for an advise how to approach these problems without creating additional resources to circumvent limitations?