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 repository 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
# ../../../../.pre-commit-config.yaml

repos:
  - repo: https://github.com/pre-commit/pre-commit-hooks
    rev: v5.0.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.17
    hooks:
      - id: pretty-format-yaml
        args: [--preserve-quotes]

  # Optimize images for size
  - repo: https://github.com/boidolr/pre-commit-images
    rev: v1.8.3
    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.6.1
    hooks:
      - id: strip-exif

  - repo: local
    hooks:
      # default settings in Markdown annoy me too much
      # - id: prettier
      #   name: prettier
      #   entry: prettier --write
      #   language: node
      #   types: [javascript, jsx, ts, tsx, css, scss, json, markdown]
      #   # files: \.(js|jsx|ts|tsx|css|scss|json|md|html|yaml|yml)$
      #   additional_dependencies: ["prettier@^3.0.0"]

      # grammar and style checking for Markdown and text files
      - id: textlint
        name: textlint
        entry: textlint
        args: ["--dry-run", "--fix"]
        # args: ["--debug", "--fix"]
        language: node
        types: [markdown, text]
        additional_dependencies:
          - "textlint@^15.2.2"
          - "textlint-rule-no-start-duplicated-conjunction@^2.0.2"
          - "textlint-rule-no-dead-link@^6.0.1"
          - "textlint-rule-terminology@^5.2.15"
          - "@textlint-rule/textlint-rule-no-unmatched-pair@^2.0.4"
          - "textlint-rule-alex@^5.0.0"
          - "textlint-rule-common-misspellings@^1.0.1"
          - "textlint-rule-apostrophe@^3.0.0"
          - "textlint-rule-write-good@^2.0.0"
          - "@textlint-rule/textlint-rule-preset-google@^0.1.2"

      # allows to easily embed always fresh code snippets
      - id: embedme
        name: embedme
        entry: embedme
        language: node
        types: [markdown]
        additional_dependencies: ["embedme@^1.22.0"]

  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.