readDir outside of project folder -- possible?

I am trying to create a SCORM manifest XML file, which essentially lists all the files (html + assets) by section. Note that these have to include the actual filename (not permalink).

I managed to set up a custom output format, and it’s generating fine. But I’ve stumbled on the file listing part. Essentially, I need to run readDir on my publishDir so I can list the files as they are published, and not on the folder containing source files.

When I run:

{{ range (readDir "/") }}
                    {{ .Name }}
{{ end }}

I get the project folders (assets, config, content, data, layouts, resources, static). So the contents of my site folder. I’d like to run:

{{ range (readDir "../build/") }}
                    {{ .Name }}
{{ end }}

But that errors out:

execute of template failed: template: _default/section.imsmanifest.xml:47:26: executing “_default/section.imsmanifest.xml” at <readDir “…/build”>: error calling readDir: failed to read directory “…/build”: open …/build: file does not exist

Is what I’m trying to do possible? Or should I suck it up and do it in webpack. (I’m using victor-hugo, so node is already part of the stack).

  • You cannot use readDir/readFile outside of the project folder (due to security reasons; we kind of trust you to do the right thing, but any {{ readFile "/etc/passwd" }} from a random theme would not be too … great.

You can, however, build to a folder below your project (which is a common thing to do). But you will end up with some chicken and egg problems.

1 Like

So that confirms my suspicion. It’s making more and more sense then to use node. (And that will let me compress the files as well. Just kinda frustrating that I’m setting up all my string variables for Hugo, and then need to somehow reuse them in the node env.

Thanks for the confirmation.

You can hard-link directories. I use it to include external directories under /data.

1 Like

So, it’s been 5 years; would you tell us how it worked for you and whether you have any tips for others?

In case you completed your Hugo solution, would it be possible to share it? I’m considering building such a solution myself.

I will close this 5 year old thread with my recommendations:

  • Don’t use readDir because it has many limitations and there are better alternatives.
  • You can configure file mounts in Hugo Configure Hugo modules | Hugo – in your project config this can point outside of project folder.
  • Mount into e.g. /assets and use resources.Get and similar, see resources.Get | Hugo

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.