Skip to content

Instantly share code, notes, and snippets.

@bkleinen
Last active July 31, 2023 08:42
Show Gist options
  • Save bkleinen/5818c8685e01f8066e65b100575159e8 to your computer and use it in GitHub Desktop.
Save bkleinen/5818c8685e01f8066e65b100575159e8 to your computer and use it in GitHub Desktop.
Hugo Modules Cheatsheet

Use Hugo Modules / Divide Hugo Page into modules

To divide a hugo page into modules resp. move a part of it into a module, three steps are needed:

  1. make site a module
  2. make part a module
  3. import part via site config.toml

for 1. + 2. : to convert anything into a go module, a go.mod file needs to be created with:

hugo mod init github.com/<owner>/<repo>

for 3. : Import module in config.toml:

[module]
[[module.imports]]
  path = 'github.com/spf13/hyde'

see https://gohugo.io/hugo-modules/use-modules/#use-a-module-for-a-theme

Where to put the module / Update Module

the default is to get the module via github, it is stored in GOPATH or ~/go

hugo mod get -u

gets the latest version/tag (eg. v0.1.0) from github and updates the go.sum file.

To develop a module, use replacements:

[module]
replacements = 'github.com/bkleinen/minimal-site-module -> ../../minimal-module'

The module can even be in a git submodule, see the project-archive integration in showtime-website:

https://github.com/htw-imi-showtime/showtime-website

Module Mounts

The documentation shows an example for site mounts, not for module mounts. To mount paths in a module to a different target, use:

[[module.imports]]
  path = 'github.com/bkleinen/minimal-site-module'
  [[module.imports.mounts]]
  source = "other_path_name"
  target = "content/other"
  [[module.imports.mounts]]
  source = "content"
  target = "content"

Check all mounts with

hugo config mounts

Clear mod cache

go clean -modcache

Examples

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment