Hugo and Git modules for documentation

Why

Documentation always gets neglected. To help with that a bit, I initially had a Hugo site in the repository where I’d stored the whole project. It was a good idea in theory, but it actually involves a lot of overhead.

Why a Git submodule? You want to generate documentation automatically from within the ‘project’. Examples include:

  • circuit diagrams
  • GLB files so you can view things in a nice 3D format.

Why a Hugo module? Here, you’re semi-separate from the ‘project’, so if the project has large files that aren’t essential for the documentation, you don’t have to include them – in short, it’s all about speed.

You could have used a Hugo-themed site to host your documentation, but if you have multiple projects, you’d end up with too many Hugo sites. Now it’s neatly integrated into your main site.

An added bonus is that you’re more likely to be tempted to keep the documentation up to date.

And whilst you can keep the blog posts with your project, they do appear neatly in your own ‘normal’ blogs, so you could attract more traffic to your site, if that’s something you’re interested in.

Hugo

Before you start

Before starting this trip make sure you can run your Hugo site local. This helps speeding up the documentation. For Docsy it is:

npm run hugo -- server

Next add your module to the hugo.yaml:

    - path: <your repo as in your remote git repo without the .git>
      mounts:
        - source: content/en/docs
          target: content/docs/<map in this Docsy site>
        - source: content/en/blog
          target: content/blog
        - source: assets
          target: assets
        - source: static
          target: static

Only add content/blog, assets and static if you have them.

Do hugo mod get -u <your repo as in hugo.yaml>@<git branch>. It should exist but can be empty.

Check if there require <your repo as in hugo.yaml> in go.mod.

Adding local content

To speed up development, it is smart to refer to the local directory. This is not hard, you only need to do a few things, don’t ask me how I know.

First

export HUGO_THEMESDIR="$(pwd)/../.."

this should be the directory where your go module base directory is located.

You can do two things, add a replace in your go.mod but this one will be added to your git repository. To get around this, you can start Hugo the following way:

HUGO_MODULE_REPLACEMENTS="<your repo as in hugo.yaml> -> <your local directory>" npm run hugo -- server

Committing the docs and view in “the Docsy site”

Now the “tricky” part.

To make sure “this” Docsy repo knows that there are updates to the “remote” documentation. Commit the remote changed repository:

cd <your local directory>
git add <changed content>
git commit -m"<some git commit comment>

Go back to “this” Docsy repository:

hugo mod get -u <your repo as in hugo.yaml>@<git branch>
git add go.mod go.sum
git commit -m"<some meaningfull commit comment"

If you have actions or a CI/CD you should probably do:

git push

And check if you CI/CD did its work.

Git

Now that we have the git module, we can add it to the ‘code’ repository quite easily.

git submodule add ssh://git@<your repo as in hugo.yaml>.git docs

To get the content of that repo:

git submodule update --init --recursive

To make sure you are working in the right repo version:

cd docs
git checkout <the right branch>
cd ..

Now store all:

git add .gitmodules
git add docs
git commit -m"Added docs submodule"

The do you favorite stuff to test it all.