Add .GitInfo.AbbreviatedHash as button on every page

I would like to extend the code snippet from @jmooring Hugo v0.112.0 - New template functions. The idea is to display the last link that was created for a certain page and with a click on the button the corresponding commit should be displayed in GitLab.

hugo.toml

[params.repository]
branch = 'main'
owner = 'example'
repo = 'developer'
service = 'GitLab'
urlPatternCommit = 'https://gitlab.com/%s/%s/-/commits/%s/%s'
urlPatternEdit = 'https://gitlab.com/%s/%s/-/edit/%s/%s'
urlPatternView = 'https://gitlab.com/%s/%s/-/blob/%s/%s'
urlPatternIssues = 'https://gitlab.com/%s/%s/-/issues/new?title=%s/%s'

single.html

{{ define "main" }}
<div class="container">
	<div class="row">
		<div class="col p-4 my-4">
			{{ .Content }}
			{{ $published := .PublishDate | time.Format ":date_medium" }}
			{{ $modified := .Lastmod | time.Format ":date_medium" }}
			{{ if ne $published $modified }}
			<span> Last modified: {{ $modified }} </span>
			{{ if not .Params.disableGitLabEditLink }}
			{{ partial "repository-link.html" (dict "action" "commits" "page" .) }}
			{{ partial "repository-link.html" (dict "action" "edit" "page" .) }}
			{{ partial "repository-link.html" (dict "action" "view" "page" .) }}
			{{ partial "repository-link.html" (dict "action" "issues" "page" .) }}
			{{ end }}
			{{ end }}
		</div>
	</div>
</div>
{{ end }}

repository-link.html

{{- if $.page.File }}
{{- with site.Params.repository }}
{{- $path := strings.TrimPrefix hugo.WorkingDir $.page.File.Filename }}
{{- $href := "" }}
{{- $text := "" }}
{{- if eq $.action "commits" }}
{{- $href = printf .urlPatternCommit .owner .repo .branch $path | urls.JoinPath }}
{{- $text = printf "Last commit: %s" .GitInfo.AbbreviatedHash }}
{{- else if eq $.action "edit" }}
{{- $href = printf .urlPatternEdit .owner .repo .branch $path | urls.JoinPath }}
{{- $text = printf "Edit this page on %s" .service }}
{{- else if eq $.action "view" }}
{{- $href = printf .urlPatternView .owner .repo .branch $path | urls.JoinPath }}
{{- $text = printf "View this page on %s" .service }}
{{- else if eq $.action "issues" }}
{{- $href = printf .urlPatternIssues .owner .repo .branch $path | urls.JoinPath }}
{{- $text = printf "New issue on %s" .service }}
{{- end }}
<div class="repository-link">
	<a href="{{ $href }}" class="btn btn-outline-secondary btn-sm my-1" type="button" rel="external">{{ $text }}</a>
</div>
{{- end }}
{{- end -}}

img

You have a context problem. The current context is not the page.

Do this instead:

$.page.GitInfo.AbbreviatedHash

Also, indented code is much easier to read.

Thank you. This is the one part of the solution.

The link shows:

https://gitlab.com/example/developer/-/commits/main/content/en/handbook.md

but not

https://gitlab.com/example/developer/-/commit/f620a0405b29b6006bbfa85737e1f01cf05a39a7

I see the path name has changed from commits to commit. Please excuse my lack of knowledge

I don’t understand. Is there still a problem?

The first link shows all commits from the initial to the last commit, but does not open the commit that the page received during the push.

This doesn’t sound like a Hugo problem.

Here is the solution for my questions. A special thank you to @jmooring for his help.

hugo.toml

[params.repository]
branch = 'main'
owner = 'example'
repo = 'developer'
service = 'GitLab'
urlPatternCommit = 'https://gitlab.com/%s/%s/commit/%s'

repository-link.html

{{- if eq $.action "commit" }}
{{- $href = printf .urlPatternCommit .owner .repo $.page.GitInfo.Hash | urls.JoinPath }}
{{- $text = printf "Last commit: %s" $.page.GitInfo.AbbreviatedHash }}

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