Image render hooks
Usage
An image render hook is a Markdown render hook that overrides the conversion of Markdown images to HTML. Use it to change the image markup, such as wrapping standalone images in figure elements, or to resolve image destinations to page resources.
A Markdown image has three components: the image description, the image destination, and an optional image title.

------------ ------------------ ---------
description destination titleHugo passes these components to the template context.
By default, Hugo wraps a standalone image, one without adjacent content, within a paragraph element. To render a standalone image as a block element, such as a figure element, disable this behavior in your project configuration:
markup:
goldmark:
parser:
wrapStandAloneImageWithinParagraph: false
[markup]
[markup.goldmark]
[markup.goldmark.parser]
wrapStandAloneImageWithinParagraph = false
{
"markup": {
"goldmark": {
"parser": {
"wrapStandAloneImageWithinParagraph": false
}
}
}
}
Context
Image render hook templates receive the following context:
Attributes- (
map) The Markdown attributes, available if you configure your project as follows:markup: goldmark: parser: attribute: block: true wrapStandAloneImageWithinParagraph: false[markup] [markup.goldmark] [markup.goldmark.parser] wrapStandAloneImageWithinParagraph = false [markup.goldmark.parser.attribute] block = true{ "markup": { "goldmark": { "parser": { "attribute": { "block": true }, "wrapStandAloneImageWithinParagraph": false } } } } Destination- (
string) The image destination. IsBlock- (
bool) Reports whether a standalone image is not wrapped within a paragraph element. Ordinal- New in v0.160.0
- (
int) The zero-based ordinal of the image on the page. Page- (
page) A reference to the current page. PageInner- (
page) A reference to the nested page when rendering content inserted by theRenderShortcodesmethod. Otherwise, a reference to the current page. PlainText- (
string) The image description as plain text. Position- New in v0.160.0
- (
text.Position) The position of the image within the page content. Calculating the position can be expensive, so call this method only when you log an error or warning. Text- (
template.HTML) The image description. Title- (
string) The image title.
Template selection
Hugo selects an image render hook template from a _markup directory using the template lookup order. Name the template render-image.html, and optionally include page kind, custom layout, output format, and language identifiers in the file name, such as render-image.rss.xml. Place the _markup directory at any level of the layouts directory to limit the template to pages in a given section.
Embedded render hook
Hugo includes an embedded image render hook to resolve Markdown image destinations. The renderHooks.image.useEmbedded setting in your project configuration determines when Hugo uses it. This is the default setting:
markup:
goldmark:
renderHooks:
image:
useEmbedded: fallback
[markup]
[markup.goldmark]
[markup.goldmark.renderHooks]
[markup.goldmark.renderHooks.image]
useEmbedded = 'fallback'
{
"markup": {
"goldmark": {
"renderHooks": {
"image": {
"useEmbedded": "fallback"
}
}
}
}
}
By default, Hugo uses the embedded image render hook unless your project or its modules define custom image render hooks. You can also configure Hugo to always use the embedded image render hook, or to never use it.
The embedded image render hook resolves internal Markdown destinations by looking for a matching page resource, falling back to a matching global resource. It passes remote destinations through unchanged, and does not log an error or warning if it cannot resolve a destination. It does not perform image processing.
Place global resources in the assets directory. If your resources are in the static directory and you cannot move them, mount the static directory to the assets directory by adding both of these entries to your project configuration:
module:
mounts:
- source: assets
target: assets
- source: static
target: assets
[module]
[[module.mounts]]
source = 'assets'
target = 'assets'
[[module.mounts]]
source = 'static'
target = 'assets'
{
"module": {
"mounts": [
{
"source": "assets",
"target": "assets"
},
{
"source": "static",
"target": "assets"
}
]
}
}
To override Hugo’s embedded image render hook, copy the source code to a file with the same name in the layouts/_markup directory. Hugo uses your template instead of the embedded image render hook unless you set useEmbedded to always.
Examples
When IsBlock is false, Hugo renders the image inline, within the surrounding text, so any whitespace that the template outputs appears in the rendered text. For example, a newline after the img element renders as a space between the image and a comma that follows it. The templates below remove this whitespace with the {{- and -}} delimiters, and end with a comment that removes the trailing newline.
In its default configuration, Hugo renders Markdown images according to the CommonMark specification. To create a render hook that does the same thing:
<img src="{{ .Destination | safeURL }}"
{{- with .PlainText }} alt="{{ . }}"{{ end -}}
{{- with .Title }} title="{{ . }}"{{ end -}}
>
{{- /* chomp trailing newline */ -}}To render standalone images within figure elements, set wrapStandAloneImageWithinParagraph to false as described in usage, then create this template:
{{- if .IsBlock -}}
<figure>
<img src="{{ .Destination | safeURL }}"
{{- with .PlainText }} alt="{{ . }}"{{ end -}}
>
{{- with .Title }}<figcaption>{{ . }}</figcaption>{{ end -}}
</figure>
{{- else -}}
<img src="{{ .Destination | safeURL }}"
{{- with .PlainText }} alt="{{ . }}"{{ end -}}
{{- with .Title }} title="{{ . }}"{{ end -}}
>
{{- end -}}