A git post-merge hook to turn vanilla github markdown files into hugo posts
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.
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:
- start a new private repo with hugo as a site generator.
- add a submodule in the content directory. The sub-module points to a specific branch in the private git repo where all my nice writing is.
- pull new content, at first manually
- later on automate with a hook.
- add a git hook that does all the processing for me.
Here is how this fancy hooked turned out:
This is a lot of ugly one-liners. Be careful when copying, bash scripting is super case sensitive.
Here is what the hook does:
- detects changed files
- loops through the files
- checks if changed files have front-matter by looking for “—” at the beginning of the file.
- If no front-matter is detected:
- find the title of the file by looking for “#”
- drop the # from the title
- create front-matter
- insert it into a temp file
- replace the pulled file
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 by reading the markdown as data. I generated a homepage and even the navigation menu.
Here is what the code looked like:
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.