Custom output formats
This page describes how to properly configure your site with the media types and output formats, as well as where to create your templates for your custom outputs.
Media types
A media type (formerly known as a MIME type) is a two-part identifier for file formats and format contents transmitted on the internet.
This is the full set of built-in media types in Hugo:
| Type | suffixes |
|---|---|
| application/json | [json] |
| application/manifest+json | [webmanifest] |
| application/octet-stream | [webmanifest] |
| application/pdf | [pdf] |
| application/rss+xml | [xml rss] |
| application/toml | [toml] |
| application/wasm | [wasm] |
| application/xml | [xml] |
| application/yaml | [yaml yml] |
| font/otf | [otf] |
| font/ttf | [ttf] |
| image/bmp | [bmp] |
| image/gif | [gif] |
| image/jpeg | [jpg jpeg jpe jif jfif] |
| image/png | [png] |
| image/svg+xml | [svg] |
| image/tiff | [tif tiff] |
| image/webp | [webp] |
| text/asciidoc | [adoc asciidoc ad] |
| text/calendar | [ics] |
| text/css | [css] |
| text/csv | [csv] |
| text/html | [html htm] |
| text/javascript | [js jsm mjs] |
| text/jsx | [jsx] |
| text/markdown | [md mdown markdown] |
| text/org | [org] |
| text/pandoc | [pandoc pdc] |
| text/plain | [txt] |
| text/rst | [rst] |
| text/tsx | [tsx] |
| text/typescript | [ts] |
| text/x-sass | [sass] |
| text/x-scss | [scss] |
| video/3gpp | [3gpp 3gp] |
| video/mp4 | [mp4] |
| video/mpeg | [mpg mpeg] |
| video/ogg | [ogv] |
| video/webm | [webm] |
| video/x-msvideo | [avi] |
Note:
- It is possible to add custom media types or change the defaults; e.g., if you want to change the suffix for
text/htmltoasp. Suffixesare the values that will be used for URLs and file names for that media type in Hugo.- The
Typeis the identifier that must be used when defining new/customOutput Formats(see below). - The full set of media types will be registered in Hugo’s built-in development server to make sure they are recognized by the browser.
To add or modify a media type, define it in a mediaTypes section in your site configuration, either for all sites or for a given language.
mediaTypes:
text/enriched:
suffixes:
- enr
text/html:
suffixes:
- asp
[mediaTypes]
[mediaTypes.'text/enriched']
suffixes = ['enr']
[mediaTypes.'text/html']
suffixes = ['asp']
{
"mediaTypes": {
"text/enriched": {
"suffixes": [
"enr"
]
},
"text/html": {
"suffixes": [
"asp"
]
}
}
}
The above example adds one new media type, text/enriched, and changes the suffix for the built-in text/html media type.
Note: these media types are configured for your output formats. If you want to redefine one of Hugo’s default output formats (e.g. HTML), you also need to redefine the media type. So, if you want to change the suffix of the HTML output format from html (default) to htm:
mediaTypes:
text/html:
suffixes:
- htm
outputFormats:
html:
mediaType: text/html
[mediaTypes]
[mediaTypes.'text/html']
suffixes = ['htm']
[outputFormats]
[outputFormats.html]
mediaType = 'text/html'
{
"mediaTypes": {
"text/html": {
"suffixes": [
"htm"
]
}
},
"outputFormats": {
"html": {
"mediaType": "text/html"
}
}
}
Output format definitions
Given a media type and some additional configuration, you get an Output Format.
This is the full set of Hugo’s built-in output formats:
| Type | baseName | isHTML | isPlainText | mediaType | noUgly | path | permalinkable | protocol | rel |
|---|---|---|---|---|---|---|---|---|---|
| amp | index | true | false | text/html | false | amp | true | amphtml | |
| calendar | index | false | true | text/calendar | false | false | webcal:// | alternate | |
| css | styles | false | true | text/css | false | false | stylesheet | ||
| csv | index | false | true | text/csv | false | false | alternate | ||
| html | index | true | false | text/html | false | true | canonical | ||
| json | index | false | true | application/json | false | false | alternate | ||
| markdown | index | false | true | text/markdown | false | false | alternate | ||
| robots | robots | false | true | text/plain | false | false | alternate | ||
| rss | index | false | false | application/rss+xml | true | false | alternate | ||
| sitemap | sitemap | false | false | application/xml | false | false | sitemap | ||
| webappmanifest | manifest | false | true | application/manifest+json | false | false | manifest |
- A page can be output in as many output formats as you want, and you can have an infinite amount of output formats defined as long as they resolve to a unique path on the file system. In the above table, the best example of this is
ampvs.html.amphas the valueampforpathso it doesn’t overwrite thehtmlversion; e.g. we can now have both/index.htmland/amp/index.html. - The
mediaTypemust match a defined media type. - You can define new output formats or redefine built-in output formats; e.g., if you want to put
amppages in a different path.
To add or modify an output format, define it in an outputFormats section in your site’s configuration file, either for all sites or for a given language.
outputFormats:
MyEnrichedFormat:
baseName: myindex
isPlainText: true
mediaType: text/enriched
protocol: bep://
[outputFormats]
[outputFormats.MyEnrichedFormat]
baseName = 'myindex'
isPlainText = true
mediaType = 'text/enriched'
protocol = 'bep://'
{
"outputFormats": {
"MyEnrichedFormat": {
"baseName": "myindex",
"isPlainText": true,
"mediaType": "text/enriched",
"protocol": "bep://"
}
}
}
The above example is fictional, but if used for the home page on a site with baseURL https://example.org, it will produce a plain text home page with the URL bep://example.org/myindex.enr.
Configure output formats
Use these parameters when configuring an output format:
- baseName
- (
string) The base name of the published file. Default isindex. - isHTML
- (
bool) Iftrue, classifies the output format as HTML. Hugo uses this value to determine when to create alias redirects, when to inject the LiveReload script, etc. Default isfalse. - isPlainText
- (
bool) Iftrue, Hugo parses templates for this output format with Go’s text/template package instead of the html/template package. Default isfalse.
- mediaType
- (
string) The media type of the published file. This must match a defined media type, either built-in or custom.
- notAlternative
- (
bool) Iftrue, excludes this output format from the values returned by theAlternativeOutputFormatsmethod on aPageobject. Default isfalse.
- noUgly
- (
bool) Iftrue, disables ugly URLs for this output format whenuglyURLsistruein your site configuration. Default isfalse. - path
- (
string) The path to the directory containing the published files, relative to the root of the publish directory. - permalinkable
- (
bool) Iftrue, thePermalinkandRelPermalinkmethods on aPageobject return the rendering output format rather than main output format (see below). Enabled by default for thehtmlandampoutput formats. Default isfalse.
- protocol
- (
string) The protocol (scheme) of the URL for this output format. For example,https://orwebcal://. Default is the scheme of thebaseURLparameter in your site configuration, typicallyhttps://. - rel
- (
string) If provided, you can assign this value torelattributes inlinkelements when iterating over output formats in your templates. Default isalternate. - root
- (
bool) Iftrue, files will be published to the root of the publish directory. Default isfalse. - ugly
- (
bool) Iftrue, enables uglyURLs for this output format whenuglyURLsisfalsein your site configuration. Default isfalse. - weight
- (
int) When set to a non-zero value, Hugo uses theweightas the first criteria when sorting output formats, falling back to the name of the output format. Lighter items float to the top, while heavier items sink to the bottom. Hugo renders output formats sequentially based on the sort order.
Output formats for pages
A Page in Hugo can be rendered to multiple output formats on the file
system.
Default output formats
Every Page has a Kind attribute, and the default Output
Formats are set based on that.
outputs:
home:
- html
- rss
page:
- html
rss:
- rss
section:
- html
- rss
taxonomy:
- html
- rss
term:
- html
- rss
[outputs]
home = ['html', 'rss']
page = ['html']
rss = ['rss']
section = ['html', 'rss']
taxonomy = ['html', 'rss']
term = ['html', 'rss']
{
"outputs": {
"home": [
"html",
"rss"
],
"page": [
"html"
],
"rss": [
"rss"
],
"section": [
"html",
"rss"
],
"taxonomy": [
"html",
"rss"
],
"term": [
"html",
"rss"
]
}
}
Customizing output formats
This can be changed by defining an outputs list of output formats in either
the Page front matter or in the site configuration (either for all sites or
per language).
Example from site configuration file:
outputs:
home:
- html
- amp
- rss
page:
- html
[outputs]
home = ['html', 'amp', 'rss']
page = ['html']
{
"outputs": {
"home": [
"html",
"amp",
"rss"
],
"page": [
"html"
]
}
}
Note that in the above examples, the output formats for section,
taxonomy and term will stay at their default value ['html','rss'].
- The
outputsdefinition is per pageKind. - The names (e.g.
html,amp) must match thenameof a defined output format, and can be overridden per page in front matter.
The following is an example of front matter in a content file that defines output formats for the rendered Page:
---
outputs:
- html
- amp
- json
title: Example
---+++
outputs = ['html', 'amp', 'json']
title = 'Example'
+++{
"outputs": [
"html",
"amp",
"json"
],
"title": "Example"
}
List output formats
Each Page object has both an OutputFormats method (all formats, including the current) and an AlternativeOutputFormats method, the latter of which is useful for creating a link rel list in your site’s <head>:
{{ range .AlternativeOutputFormats -}}
<link rel="{{ .Rel }}" type="{{ .MediaType.Type }}" href="{{ .Permalink | safeURL }}">
{{ end }}
Link to output formats
The Permalink and RelPermalink methods on a Page object return the first output format defined for that page (usually HTML if nothing else is defined). This is regardless of the template from which they are called.
from single.json.json:
{{ .RelPermalink }} → /that-page/
{{ with .OutputFormats.Get "json" }}
{{ .RelPermalink }} → /that-page/index.json
{{ end }}
In order for them to return the output format of the current template file instead, the given output format should have its permalinkable setting set to true.
Same template file as above with json output format’s permalinkable set to true:
{{ .RelPermalink }} → /that-page/index.json
{{ with .OutputFormats.Get "html" }}
{{ .RelPermalink }} → /that-page/
{{ end }}
From content files, you can use the ref or relref shortcodes:
[Neat]({{< ref "blog/neat.md" "amp" >}})
[Who]({{< relref "about.md#who" "amp" >}})
Templates for your output formats
Each output format requires a corresponding template conforming to the template lookup order. Hugo considers both output format and suffix when selecting a template.
For example, to generate a JSON file for the home page, the template with highest specificity is layouts/index.json.json.
Hugo will now also detect the media type and output format of partials, if possible, and use that information to decide if the partial should be parsed as a plain text template or not.
Hugo will look for the name given, so you can name it whatever you want. But if you want it treated as plain text, you should use the file suffix and, if needed, the name of the Output Format. The pattern is as follows:
[partial name].[OutputFormat].[suffix]
The partial below is a plain text template . The output format is csv, and since this is the only output format with the suffix csv, we don’t need to include the output format name):
{{ partial "mytextpartial.csv" . }}