Debian Codegraph @

docs/content/en/render-hooks/images.md

raw
1 ---
2 title: Image render hooks
3 linkTitle: Images
4 description: Create image render hook templates to override the rendering of Markdown images to HTML.
5 categories: []
6 keywords: []
7 ---
8
9 ## Markdown
10
11 A Markdown image has three components: the image description, the image destination, and optionally the image title.
12
13 ```text
14 ![white kitten](/images/kitten.jpg "A kitten!")
15 ------------ ------------------ ---------
16 description destination title
17 ```
18
19 These components are passed into the render hook [context](g) as shown below.
20
21 ## Context
22
23 Image _render hook_ templates receive the following context:
24
25 `Attributes`
26 : (`map`) The [Markdown attributes][], available if you configure your site as follows:
27
28 {{< code-toggle file=hugo >}}
29 [markup.goldmark.parser]
30 wrapStandAloneImageWithinParagraph = false
31 [markup.goldmark.parser.attribute]
32 block = true
33 {{< /code-toggle >}}
34
35 `Destination`
36 : (`string`) The image destination.
37
38 `IsBlock`
39 : (`bool`) Reports whether a standalone image is not wrapped within a paragraph element.
40
41 `Ordinal`
42 : {{< new-in v0.160.0 />}}
43 : (`int`) The zero-based ordinal of the image on the page.
44
45 `Page`
46 : (`page`) A reference to the current page.
47
48 `PageInner`
49 : (`page`) A reference to a page nested via the [`RenderShortcodes`][] method. [See details](#pageinner-details).
50
51 `PlainText`
52 : (`string`) The image description as plain text.
53
54 `Position`
55 : {{< new-in 0.160.0 />}}
56 : (`string`) The position of the image within the page content.
57
58 `Text`
59 : (`template.HTML`) The image description.
60
61 `Title`
62 : (`string`) The image title.
63
64 ## Examples
65
66 > [!NOTE]
67 > With inline elements such as images and links, remove leading and trailing whitespace using the `{{‑ ‑}}` delimiter notation to prevent whitespace between adjacent inline elements and text.
68
69 In its default configuration, Hugo renders Markdown images according to the [CommonMark][] specification. To create a render hook that does the same thing:
70
71 ```go-html-template {file="layouts/_markup/render-image.html" copy=true}
72 <img src="{{ .Destination | safeURL }}"
73 {{- with .PlainText }} alt="{{ . }}"{{ end -}}
74 {{- with .Title }} title="{{ . }}"{{ end -}}
75 >
76 {{- /* chomp trailing newline */ -}}
77 ```
78
79 To render standalone images within `figure` elements:
80
81 ```go-html-template {file="layouts/_markup/render-image.html" copy=true}
82 {{- if .IsBlock -}}
83 <figure>
84 <img src="{{ .Destination | safeURL }}"
85 {{- with .PlainText }} alt="{{ . }}"{{ end -}}
86 >
87 {{- with .Title }}<figcaption>{{ . }}</figcaption>{{ end -}}
88 </figure>
89 {{- else -}}
90 <img src="{{ .Destination | safeURL }}"
91 {{- with .PlainText }} alt="{{ . }}"{{ end -}}
92 {{- with .Title }} title="{{ . }}"{{ end -}}
93 >
94 {{- end -}}
95 ```
96
97 Note that the above requires the following project configuration:
98
99 {{< code-toggle file=hugo >}}
100 [markup.goldmark.parser]
101 wrapStandAloneImageWithinParagraph = false
102 {{< /code-toggle >}}
103
104 ## Embedded
105
106 Hugo includes an [embedded image render hook][] to resolve Markdown image destinations. You can adjust its behavior in your project configuration. This is the default setting:
107
108 {{< code-toggle file=hugo >}}
109 [markup.goldmark.renderHooks.image]
110 useEmbedded = 'auto'
111 {{< /code-toggle >}}
112
113 When set to `auto` as shown above, Hugo automatically uses the embedded image render hook for multilingual single-host projects, specifically when the [duplication of shared page resources][] feature is disabled. This is the default behavior for such projects. If custom image render hooks are defined by your project, modules, or themes, these will be used instead.
114
115 You can also configure Hugo to `always` use the embedded image render hook, use it only as a `fallback`, or `never` use it. See [details][].
116
117 The embedded image render hook resolves internal Markdown destinations by looking for a matching [page resource](g), falling back to a matching [global resource](g). Remote destinations are passed through, and the render hook will not throw an error or warning if unable to resolve a destination.
118
119 You must place global resources in the `assets` directory. If you have placed your resources in the `static` directory, and you are unable or unwilling to move them, you must mount the `static` directory to the `assets` directory by including both of these entries in your project configuration:
120
121 {{< code-toggle file=hugo >}}
122 [[module.mounts]]
123 source = 'assets'
124 target = 'assets'
125
126 [[module.mounts]]
127 source = 'static'
128 target = 'assets'
129 {{< /code-toggle >}}
130
131 Note that the embedded image render hook does not perform image processing. Its sole purpose is to resolve Markdown image destinations.
132
133 {{% include "/_common/render-hooks/pageinner.md" %}}
134
135 [CommonMark]: https://spec.commonmark.org/current/
136 [Markdown attributes]: /content-management/markdown-attributes/
137 [`RenderShortcodes`]: /methods/page/rendershortcodes/
138 [details]: /configuration/markup/#renderhooksimageuseembedded
139 [duplication of shared page resources]: /configuration/markup/#duplicateresourcefiles
140 [embedded image render hook]: <{{% eturl render-image %}}>