Button/link to edit a page on GitHub

Hi fellow Hugo-ites!

I have a Hugo theme that (among other things) lets users click a button to edit the relevant site page in Github. I build the edit URL in my template using the site’s provided repo name, the page Path, etc.

At the moment I check if there’s a language-specific subdirectory like this:

{{ else if .Site.Language.Lang }}
{{ $editURL = printf “%s/edit/master/content/%s/%s” $gh_repo ($.Site.Language.Lang) .Path }}

However, this is creating the wrong URL for one of my theme users, as their site is not Multilingual, but they do have a specified language (English) for content that is not in an en subdir:

defaultContentLanguage = “en”
defaultContentLanguageInSubdir = false

If I switch the check to “is this site Multilingual”, however:

{{ else if .Site.IsMultiLingual }}
{{ $editURL = printf “%s/edit/master/content/%s/%s” $gh_repo ($.Site.Language.Lang) .Path }}

…I create the wrong URL for one of my own sites, because it is not Multilingual but does have the content in an en subdir.

defaultContentLanguage = “en”
defaultContentLanguageInSubdir = true

Is there a nice Hugo way of checking in a template whether defaultContentLanguageInSubdir is true, given that it isn’t a parameter?

Thanks for any help!

1 Like

I think you can check .Site.Params for defaultContentLanguageInSubdir.

1 Like

Hmm, I thought you could but it’s not actually getting a value, I think it’s because it’s not in the [params] section of my config. I’ll poke at it some more.

Also looking more closely it seems like what I need to check is contentDir but that has the same problem.

The solution would have been to use site.LanguagePrefix to build URLs that work regardless of the value of defaultContentLanguageInSubdir.

From its doc:

this can be used to prefix URLs to point to the correct language. It will even work when there is only one defined language. See also the functions absLangURL and relLangURL.

1 Like

Ah, I don’t think this existed back when I originally had this issue! Many many thanks, I had a slightly hacky workaround but this looks like a proper fix.

https://discourse.gohugo.io/t/hugo-v0-112-0-new-template-functions/44512

{{ strings.TrimPrefix hugo.WorkingDir .Page.File.Filename }}
3 Likes

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