Concatenate Url

Hello,

I’m sure there is the answer somewhere but I keep struggling. I want to generate this Url in my template (an edit link):

https://github.dev/mat/website/blob/main/content/_index.md

After many tries, I’ve came up with that:

{{ path.Join .Site.Params.editUrl .File.Path }}

It works but it removes a slash in .Site.Params.editUrl.
https://github.dev become https:/github.dev

So my url is missing a slash to work properly:

https:/github.dev/mat/website/blob/main/content/_index.md

I probably don’t use the right function, but I always ended up with not the good url whatever I tried.

Thanks for your help !

Replace path.Join with add to make it handle schemas like “https://”.

You need to take care of slash at start and end of the strings when using add. It will not like path.Join handle that for you.

This would improve things a bit:
https://github.com/gohugoio/hugo/issues/9694
https://github.com/gohugoio/hugo/pull/10897

1 Like

Thank you very much both for your answers.

This is my code now and it works:

{{/* .File.Path has a backslash from windows, this transform it into a normal slash */}}
{{ $cleanUrl := path.Join .File.Path }}
{{ $editUrl := add .Site.Params.editUrl $cleanUrl }}
1 Like

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