The hugo-theme-hedgerules Hugo theme provides output format layouts, data partials, and table partials for working with Hedgerules headers and redirects.

Installation#

Add the theme to your Hugo site as a module. In your hugo.toml:

[module]
  [module.hugoVersion]
    min = "0.128.0"
  [[module.imports]]
    path = "github.com/mrled/hedgerules/hugo-theme-hedgerules"

# Enable Hedgerules output formats on the home page
[outputs]
  home = ["HTML", "hedgeheaders", "hedgeredirects"]

Initialize Hugo modules if you haven’t already:

hugo mod init github.com/yourname/yoursite
hugo mod get github.com/mrled/hedgerules/hugo-theme-hedgerules

The theme automatically defines the hedgeheaders and hedgeredirects output formats, so you just need to enable them in your [outputs] configuration.

Output format layouts#

These templates generate the files that hedgerules deploy reads.

index.hedgeheaders.json#

Generates _hedge_headers.json, a JSON file mapping paths to HTTP header objects. Enable it via [outputs] in hugo.toml:

[outputs]
  home = ["HTML", "hedgeheaders", "hedgeredirects"]

index.hedgeredirects.txt#

Generates _hedge_redirects.txt, a text file with one source destination pair per line. Enable it alongside hedgeheaders as shown above.

Data partials#

These partials return data and can be called from your own templates or shortcodes.

hedgerules/redirects.html#

Returns a map of all redirects (source path to destination path). Sources are collected in this order:

  1. Site-wide HedgerulesRedirects from hugo.toml
  2. Hugo aliases from page frontmatter
  3. HedgerulesPathRedirects from page frontmatter

The result is cached on hugo.Store so it is only computed once per build.

{{ $redirects := partial "hedgerules/redirects.html" . }}
{{ range $source, $dest := $redirects }}
  {{ $source }} -> {{ $dest }}
{{ end }}

hedgerules/headers.html#

Returns a map of all headers (path to header dict). Sources are merged in this order (later wins):

  1. HedgerulesPathHeaders from hugo.toml
  2. HedgerulesHeaders from page frontmatter

The result is cached on hugo.Store so it is only computed once per build.

{{ $headers := partial "hedgerules/headers.html" . }}
{{ range $path, $headerDict := $headers }}
  {{ $path }}:
  {{ range $name, $value := $headerDict }}
    {{ $name }}: {{ $value }}
  {{ end }}
{{ end }}

Table partials and shortcodes#

These partials render HTML tables and are useful for documentation or debugging pages.

hedgerules/redirectsTable.html#

Renders an HTML table with Source and Destination columns for all redirects.

{{ partial "hedgerules/redirectsTable.html" . }}

This is also available as a shortcode.

{{< hedgerules/redirectsTable >}}

On this site, it renders like this:

SourceDestination
/blog/first-post//blog/2024/hello-world/
/docs/headers/request-tokens//docs/headers/request-path-tokens/
/docs/legacy-redirects//docs/redirects/alias-example/
/docs/redirect-guide//docs/redirects/
/guides/getting-started//docs/guides/getting-started/
/hello//blog/2024/hello-world/
/old-redirect-docs//docs/redirects/alias-example/
/test/redirect-alias-example/docs/redirects/alias-example/
/test/redirect-getting-started/docs/guides/getting-started/

hedgerules/headersTable.html#

Renders an HTML table with Path, Header, and Value columns for all headers.

{{ partial "hedgerules/headersTable.html" . }}

This is also available as a shortcode.

{{< hedgerules/headersTable >}}

On this site, it renders like this:

PathHeaderValue
*.xmlcontent-typeapplication/xml; charset=utf-8
*.xmlx-content-type-optionsnosniff
/x-hedgerules-helloNice to meet you
/x-hedgerules-local-development-pathhttp://localhost:1313{/path}
/blog/2024/hello-world/cache-controlpublic, max-age=86400
/blog/2024/hello-world/x-blog-posttrue
/docs/headers/cache-controlpublic, max-age=3600
/docs/headers/x-docs-sectionheaders
/docs/headers/x-hedgerules-hello-subpathHello from this specific subpath (and its children)
/docs/headers/per-page-example/x-custom-pageper-page-example
/favicon.pngx-hedgerules-icon-aboutThe icon is made of the characters 'h≡', in JetBrains Mono, size 56, color '#14532D'
/favicon.svgx-hedgerules-icon-aboutThe icon is made of the characters 'h≡', in JetBrains Mono, size 56, color '#14532D'