A git post-merge hook that turns raw markdown from github into hugo posts

Problem

I was documenting stuff on a private github repo. It is the kind of repo that you can’t make public. In other words, I can’t automatically generate a github page from the docs folder). The documentation was getting nice and detailed… which made it harder to browse and read, so I figured I would make it more friendly by generating a site.

Solution
Of course the solution to all my woes is to use hugo to build a statically generated site. Here is what I needed to do:

Here is how this fancy hooked turned out:

  #/usr/bin/env bash

  changed_files="$(git diff-tree -r --name-only --no-commit-id ORIG_HEAD HEAD)"

  for file in $changed_files;do
          echo "changed files: \n" $file

          starts_with="$(head -n 1 $file)"

          if [ "$starts_with" != "---" ]; then
                  title="$(grep -m 1 '#' $file | tr -d '#')"
                  (echo $'---\ntitle:'$title'\n---\n' && cat $file ) > tmp && mv tmp $file
          fi
  done

This is a lot of ugly one-liners. Be careful when copying, bash scripting is super case sensitive.

Here is what the hook does:

This seemed like a lot of work. My initial solution was to just soldier on and create a weird hugo theme that doesn’t rely on front-matter.

After some tinkering with hugo code, I managed to do that, I generated a homepage and even the navigation menu. But things got un-usable pretty fast. I couldn’t easily do search or navigation between content. I also got lazy. I have tons of written hugo code already. Re-writing it to just ignore the markdown was an over-kill. The hook started to look like a walk in the park.

I know I am meant to loose my post-merge hook. So I am publishing it here for future reference and for whichever poor soul is trying to do the same work.

Here is the hugo template code that enventually ended-up un-used

  {{$path :=  "/content/data-science" }}
  {{$file := "/data-science/README.md"}}

      <div class="container-fluid ml-4 pt-5 mt-5">
        <div class="row pt-5">
          <aside class="col-lg-3">
            <div id="sticky" class="sticky bg-dark p-2 mr-5">
            {{- $dirs := readDir $path -}}
              {{ range $dirs }}
                {{ $type_arr := split .Name "." }}
                {{ $ext := index $type_arr 1 }}
                {{ if and (ne $ext "git") (ne $ext "gitignore")  }}
                    <div class="nav-item">   <a class="nav-link text-white " href="#">{{ .Name }}</a>   </div>
                {{ end }}
              {{ end }}
            </div>
          </aside>
          <article id="content" class="col-lg-6">
              {{- $file  | readFile | markdownify -}}
          </article>
        </div>
      </div>