I love blogging with Hugoexternal link and I have two blogs already that use it. The good thing about static sites is that you have all the data in the files. You can optimize them locally, batch process, amend, etc. Powerful templating engine allows to quickly pre fill documents in the format I like.

I have some steps in the Makefile for things like image optimization, but I often don’t remember to run them 😃

That’s where pre-commit1 comes to help, triggering linters, file syntax checks, optimizers.

  1. Install hooks in the git repo by calling:
Install pre-commit hooks
pre-commit install

  1. Create config file called .pre-commit-config.yaml.

  2. Fill it with hooks2 that will make you life easier:

.pre-commit-config.yaml
repos:
  - repo: https://github.com/pre-commit/pre-commit-hooks
    rev: v4.5.0
    hooks:
      - id: check-merge-conflict
      - id: check-yaml
      - id: check-json
      - id: check-toml
      - id: end-of-file-fixer
      - id: mixed-line-ending
        args: [--fix=auto]
      - id: check-added-large-files
      - id: trailing-whitespace
        args: [--markdown-linebreak-ext=md]
      - id: pretty-format-json
        args: [--autofix, --indent=4, --no-sort-keys]

  - repo: https://github.com/boidolr/pre-commit-text
    rev: v1.2.13
    hooks:
      - id: pretty-format-yaml
        args: [--preserve-quotes]

  # Optimize images for size
  - repo: https://github.com/boidolr/pre-commit-images
    rev: v1.5.1
    hooks:
      - id: optimize-avif
      - id: optimize-jpg
      - id: optimize-png
      - id: optimize-svg
      - id: optimize-webp

  # Strip EXIF data from images
  - repo: https://github.com/stefmolin/exif-stripper
    rev: 0.1.2
    hooks:
      - id: strip-exif

  1. (Optional) Run pre-commit manually on all files to update “old” files in one shot:
Run pre-commit
pre-commit run -a

With this config, every time I commit, my files will be cleaned and optimized.