Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Proposal] Add a variable to describe site's content directory #10969

Closed
razonyang opened this issue May 20, 2023 · 10 comments · Fixed by #10971
Closed

[Proposal] Add a variable to describe site's content directory #10969

razonyang opened this issue May 20, 2023 · 10 comments · Fixed by #10971

Comments

@razonyang
Copy link
Contributor

razonyang commented May 20, 2023

Hi, I use the Language.ContentDir to get the relative path of pages to the site root, which is useful for generating the links to view/edit the pages. But the internal use variable was removed on this commit, is there a workaround to get the relative path of the pages? Or is it possible to expose a function or variable to achieve this?

Thanks.

@bep
Copy link
Member

bep commented May 20, 2023

I suggest you do something ala:

{{ with .File }}
{{ $relativeToContentDir := .Path }}
{{ end }}

Which isn't the same as what you do today as you would need to know that it's inside content, but the thing is, with a composite file system (multiple content dirs), there's no golden way of doing this without knowing more ...

Note that if you're talking about links to the files on disk (and not GitHub), you can do:

{{ with .File }}
{{ $absoluteFilename := .Filename }}
{{ end }}

@bep
Copy link
Member

bep commented May 20, 2023

We can certainly add "something" to simplify the above, but I don't see what.

@razonyang
Copy link
Contributor Author

razonyang commented May 20, 2023

Thanks for your suggestions, I want to generate links about the view/edit URLs of GitHub/GitLab, but I didn't figure out a solution.
The File.Path isn't enough to describe the path to the site root when ContentDir is set.
Is that possible to add a variable on File that return the path that includes the content or custom contentDir folder?
Or can I access the site root path so that I can manually transform the File.Filename and get the path I want.

@jmooring
Copy link
Member

@razonyang This forum topic might be helpful, specifically:

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

For this to work, Hugo must be run within the project directory (i.e., cannot use hugo -s somedir).

@razonyang
Copy link
Contributor Author

razonyang commented May 20, 2023

@jmooring Thanks, I thought about this, as you mentioned, $PWD isn't always reliable.

@jmooring
Copy link
Member

This is what I really really want:

{{ strings.TrimPrefix hugo.WorkingDir .File.Filename }}

@razonyang
Copy link
Contributor Author

@jmooring Thanks a lot, will try later.

@jmooring
Copy link
Member

hugo.WorkingDir is not implemented---it is what I want.

bep added a commit to bep/hugo that referenced this issue May 20, 2023
@bep
Copy link
Member

bep commented May 20, 2023

Sure, we can do that, but note that

{{ strings.TrimPrefix hugo.WorkingDir .File.Filename }}

Will not work for files in themes/modules.

@bep bep added this to the v0.112.0 milestone May 20, 2023
bep added a commit to bep/hugo that referenced this issue May 20, 2023
bep added a commit to bep/hugo that referenced this issue May 20, 2023
bep added a commit that referenced this issue May 20, 2023
@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 11, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants