contentDir in case of multilingual mode with translation by content directory

Hi there,

i’m running a page in multilingual mode where i am using the translation by content directory.

In my theme i want to add a edit link to the respective file.

When i use:

{{ $projectPath := or (os.Getenv "HUGO_PROJECT_PATH" ) (os.Getenv "PWD") }}
<a href="vscode://file/{{ $projectPath }}/content/{{ .File.Path }}" rel="noopener noreferrer" target="_blank">Edit</a>

this provides me with a local path without the language prefix. Now i can get the language prefix with .File.Lang, e.g. like that:

{{ $projectPath := or (os.Getenv "HUGO_PROJECT_PATH" ) (os.Getenv "PWD") }}
<a href="vscode://file/{{ $projectPath }}/content/{{ .File.Lang}}/{{ .File.Path }}" rel="noopener noreferrer" target="_blank">Edit</a>

but if i use the same theme for a non-translated site i get wrong links. The solution for me would be to access the variable contentDir of the configuration of Hugo instead of hardcoding - is there any way to retrieve that?

Thanks in advance!
Cheers!

Do this:

{{ with .File }}
  <a {{ printf "href=%q" (print "vscode://file" .Filename) | safeHTMLAttr }}>Edit</a>
{{ end }}

.Filename is the absolute path to the file.

You are the best! That worked right away… oh my. I just had to add one additional forward slash after file to it:

{{ with .File }}
  <a {{ printf "href=%q" (print "vscode://file/" .Filename) | safeHTMLAttr }}>Edit</a>
{{ end }}

The solution is great for the local edits but i guess i cannot access the contentDir directly, right? Scenario would be to edit the file in a github repo (instead of a local path via vscode), i have no option currently?

Assumptions:

  1. Your content directory is named content and in the root of your project
  2. Your content is not pulled in via a mount

site config

[params.repo]
url = 'https://github.com/user/project/'
branch = 'main'

template

{{ with .File }}
  {{ $u := printf "%s/edit/%s/content/%s" site.Params.repo.url site.Params.repo.branch (index (split .Filename "content") 1) }}
  <a href="{{ $u }}">Edit</a>
{{ end }}

You’ll end up with some repeated forward slashes[1], but don’t worry about it.


  1. We can easily clean this up if #10897 is accepted/merged. ↩︎

Wow. You really know hugo in and out. Nice!

I modified the template from your version to:

{{ with .File }}
  {{ $u := printf "%s/edit/%s/content%s" site.Params.repo.url site.Params.repo.branch (path.Clean (index (split .Filename "content") 1)) }}
  <a href="{{ $u }}">Edit</a>
{{ end }}

which cleaned the outcome nicely. Thanks so much!

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