"Recently Edited" section, with GitHub links for Commit Diff and File History

As an update to this post, which strayed into troubleshooting issues, I am sharing here an update I just did when I found how GitHub lets you link to the History of a file (a subtle difference between commit and commits in the URL).

So to have a Recently Edited section in your site that looks like this

[I know, those icons look misaligned, if somebody has a tip to fix that I’d appreciate it]

You can get all this information from Hugo’s --enableGitInfo option, and corresponding GitInfo variables.

This is the code that drives the loop (from here):

<h2>{{ T "RecentlyEdited"}}</h2>
<h4 class="hd">{{ T "DocumentationPages"}}</h4>
<div class="edit_wrap">
    {{ $byLastMod :=  .Site.RegularPages.ByLastmod  }}
    {{ $recent := ($byLastMod | last 5).Reverse }}
    {{ partial "recently-edited-item" $recent }}

And this is the interesting part writing out each item (from here):

{{ range  . }}
	<h4 class="">
	    <a href="{{ .Permalink }}">{{ .Title }}</a>
            <a href="https://github.com/salesagility/SuiteDocs/commits/{{.GitInfo.Hash}}/content/{{.Path}}" target="_blank" title="File History"><i class="fa fa-2x fa-history"></i></a>
	<p class="">
	   Edited on the {{ .Lastmod.Format "2 January" }} in commit <b>"{{ with .GitInfo }}{{ .Subject }}{{ else }}[Commit description]{{ end }}"</b>  
             <a href="https://github.com/salesagility/SuiteDocs/commit/{{.GitInfo.Hash}}" target="_blank" title="Commit Diff"><i class="fa fa-2x fa-github"></i></a>
{{ end }}

For anyone that uses this in the future, note that the .Path does not work for 2 reasons:

  1. There is some warning in the terminal that you need to do this (I don’t really understand it, but it works now without the depreciation warning. This is the code given in the warning.):
  {{ $path := "" }}
  {{ with .File }}
        {{ $path = .Path }}
  {{ else }}
        {{ $path = .Path }}
  {{ end }}
  1. $Path renders the link as a computer file. I.e. with a backslash instead of forward slash. You therefore have to do the following:
  {{path.Clean $path }}

See this release

1 Like