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-hedgerulesThe 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:
- Site-wide
HedgerulesRedirectsfromhugo.toml - Hugo
aliasesfrom page frontmatter HedgerulesPathRedirectsfrom 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):
HedgerulesPathHeadersfromhugo.tomlHedgerulesHeadersfrom 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:
| Source | Destination |
|---|---|
| /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:
| Path | Header | Value |
|---|---|---|
| *.xml | content-type | application/xml; charset=utf-8 |
| *.xml | x-content-type-options | nosniff |
| / | x-hedgerules-hello | Nice to meet you |
| / | x-hedgerules-local-development-path | http://localhost:1313{/path} |
| /blog/2024/hello-world/ | cache-control | public, max-age=86400 |
| /blog/2024/hello-world/ | x-blog-post | true |
| /docs/headers/ | cache-control | public, max-age=3600 |
| /docs/headers/ | x-docs-section | headers |
| /docs/headers/ | x-hedgerules-hello-subpath | Hello from this specific subpath (and its children) |
| /docs/headers/per-page-example/ | x-custom-page | per-page-example |
| /favicon.png | x-hedgerules-icon-about | The icon is made of the characters 'h≡', in JetBrains Mono, size 56, color '#14532D' |
| /favicon.svg | x-hedgerules-icon-about | The icon is made of the characters 'h≡', in JetBrains Mono, size 56, color '#14532D' |