Path to content file from project root

With this content file:

/home/user/project/content/en/about.md

{{ .File.Filename }} returns:

/home/user/project/content/en/about.md

But what I really really want is:

/content/en/about.md

If I add PWD to security.funcs.getenv I can do this:

{{ strings.TrimPrefix (os.Getenv "PWD") .File.Filename }}

But this approach is somewhat fragile (e.g., executing hugo from another dir with hugo -s something).

Running hugo config (with or without a -s flag) shows:

workingdir =  "/home/user/project"

So we have access to the value to trim, somewhere.

Justification: avoid hardcoding contentDir when interacting with GitHub, GitLab, etc. For example:

3 Likes

Hi there,

I’m not familiar with this part of Hugo, but I played in the past with filenames and path to provide a shortcode to embed a file into another, maybe you can find interesting bits out there?
I’m thinking of .Page.File.Dir, see also the references and untested methods

Sorry if I’m off-topic, and good luck

1 Like

This is resolved in the next release, presumably v0.112.0, with https://github.com/gohugoio/hugo/pull/10971.

Thank you bep.

3 Likes

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