| 1 |
--- |
| 2 |
title: Blockquote render hooks |
| 3 |
linkTitle: Blockquotes |
| 4 |
description: Create blockquote render hook templates to override the rendering of Markdown blockquotes to HTML. |
| 5 |
categories: [] |
| 6 |
keywords: [] |
| 7 |
--- |
| 8 |
|
| 9 |
## Context |
| 10 |
|
| 11 |
Blockquote _render hook_ templates receive the following [context](g): |
| 12 |
|
| 13 |
`AlertType` |
| 14 |
: (`string`) Applicable when [`Type`](#type) is `alert`, this is the alert type converted to lowercase. See the [alerts](#alerts) section below. |
| 15 |
|
| 16 |
`AlertTitle` |
| 17 |
: {{< new-in 0.134.0 />}} |
| 18 |
: (`template.HTML`) Applicable when [`Type`](#type) is `alert`, this is the alert title. See the [alerts](#alerts) section below. |
| 19 |
|
| 20 |
`AlertSign` |
| 21 |
: {{< new-in 0.134.0 />}} |
| 22 |
: (`string`) Applicable when [`Type`](#type) is `alert`, this is the alert sign. Typically used to indicate whether an alert is graphically foldable, this is one of `+`, `-`, or an empty string. See the [alerts](#alerts) section below. |
| 23 |
|
| 24 |
`Attributes` |
| 25 |
: (`map`) The [Markdown attributes][], available if you configure your site as follows: |
| 26 |
|
| 27 |
{{< code-toggle file=hugo >}} |
| 28 |
[markup.goldmark.parser.attribute] |
| 29 |
block = true |
| 30 |
{{< /code-toggle >}} |
| 31 |
|
| 32 |
`Ordinal` |
| 33 |
: (`int`) The zero-based ordinal of the blockquote on the page. |
| 34 |
|
| 35 |
`Page` |
| 36 |
: (`page`) A reference to the current page. |
| 37 |
|
| 38 |
`PageInner` |
| 39 |
: (`page`) A reference to a page nested via the [`RenderShortcodes`][] method. [See details](#pageinner-details). |
| 40 |
|
| 41 |
`Position` |
| 42 |
: (`string`) The position of the blockquote within the page content. |
| 43 |
|
| 44 |
`Text` |
| 45 |
: (`template.HTML`) The blockquote text, excluding the first line if [`Type`](#type) is `alert`. See the [alerts](#alerts) section below. |
| 46 |
|
| 47 |
`Type` |
| 48 |
: (`string`) The blockquote type. Returns `alert` if the blockquote has an alert designator, else `regular`. See the [alerts](#alerts) section below. |
| 49 |
|
| 50 |
## Examples |
| 51 |
|
| 52 |
In its default configuration, Hugo renders Markdown blockquotes according to the [CommonMark][] specification. To create a render hook that does the same thing: |
| 53 |
|
| 54 |
```go-html-template {file="layouts/_markup/render-blockquote.html" copy=true} |
| 55 |
<blockquote> |
| 56 |
{{ .Text }} |
| 57 |
</blockquote> |
| 58 |
``` |
| 59 |
|
| 60 |
To render a blockquote as an HTML `figure` element with an optional citation and caption: |
| 61 |
|
| 62 |
```go-html-template {file="layouts/_markup/render-blockquote.html" copy=true} |
| 63 |
<figure> |
| 64 |
<blockquote {{ with .Attributes.cite }}cite="{{ . }}"{{ end }}> |
| 65 |
{{ .Text }} |
| 66 |
</blockquote> |
| 67 |
{{ with .Attributes.caption }} |
| 68 |
<figcaption class="blockquote-caption"> |
| 69 |
{{ . | safeHTML }} |
| 70 |
</figcaption> |
| 71 |
{{ end }} |
| 72 |
</figure> |
| 73 |
``` |
| 74 |
|
| 75 |
Then in your markdown: |
| 76 |
|
| 77 |
```md |
| 78 |
> Some text |
| 79 |
{cite="https://gohugo.io" caption="Some caption"} |
| 80 |
``` |
| 81 |
|
| 82 |
## Alerts |
| 83 |
|
| 84 |
Also known as _callouts_ or _admonitions_, alerts are blockquotes used to emphasize critical information. |
| 85 |
|
| 86 |
### Basic syntax |
| 87 |
|
| 88 |
With the basic Markdown syntax, the first line of each alert is an alert designator consisting of an exclamation point followed by the alert type, wrapped within brackets. For example: |
| 89 |
|
| 90 |
```md {file="content/example.md"} |
| 91 |
> [!NOTE] |
| 92 |
> Useful information that users should know, even when skimming content. |
| 93 |
|
| 94 |
> [!TIP] |
| 95 |
> Helpful advice for doing things better or more easily. |
| 96 |
|
| 97 |
> [!IMPORTANT] |
| 98 |
> Key information users need to know to achieve their goal. |
| 99 |
|
| 100 |
> [!WARNING] |
| 101 |
> Urgent info that needs immediate user attention to avoid problems. |
| 102 |
|
| 103 |
> [!CAUTION] |
| 104 |
> Advises about risks or negative outcomes of certain actions. |
| 105 |
``` |
| 106 |
|
| 107 |
The basic syntax is compatible with [GitHub][], [Obsidian][], and [Typora][]. |
| 108 |
|
| 109 |
### Extended syntax |
| 110 |
|
| 111 |
With the extended Markdown syntax, you may optionally include an alert sign and/or an alert title. The alert sign is one of `+` or `-`, typically used to indicate whether an alert is graphically foldable. For example: |
| 112 |
|
| 113 |
```md {file="content/example.md"} |
| 114 |
> [!WARNING]+ Radiation hazard |
| 115 |
> Do not approach or handle without protective gear. |
| 116 |
``` |
| 117 |
|
| 118 |
The extended syntax is compatible with [Obsidian][]. |
| 119 |
|
| 120 |
> [!NOTE] |
| 121 |
> The extended syntax is not compatible with GitHub or Typora. If you include an alert sign or an alert title, these applications render the Markdown as a blockquote. |
| 122 |
|
| 123 |
### Example |
| 124 |
|
| 125 |
This blockquote render hook renders a multilingual alert if an alert designator is present, otherwise it renders a blockquote according to the CommonMark specification. |
| 126 |
|
| 127 |
```go-html-template {file="layouts/_markup/render-blockquote.html" copy=true} |
| 128 |
{{ $emojis := dict |
| 129 |
"caution" ":exclamation:" |
| 130 |
"important" ":information_source:" |
| 131 |
"note" ":information_source:" |
| 132 |
"tip" ":bulb:" |
| 133 |
"warning" ":information_source:" |
| 134 |
}} |
| 135 |
|
| 136 |
{{ if eq .Type "alert" }} |
| 137 |
<blockquote class="alert alert-{{ .AlertType }}"> |
| 138 |
<p class="alert-heading"> |
| 139 |
{{ transform.Emojify (index $emojis .AlertType) }} |
| 140 |
{{ with .AlertTitle }} |
| 141 |
{{ . }} |
| 142 |
{{ else }} |
| 143 |
{{ or (i18n .AlertType) (title .AlertType) }} |
| 144 |
{{ end }} |
| 145 |
</p> |
| 146 |
{{ .Text }} |
| 147 |
</blockquote> |
| 148 |
{{ else }} |
| 149 |
<blockquote> |
| 150 |
{{ .Text }} |
| 151 |
</blockquote> |
| 152 |
{{ end }} |
| 153 |
``` |
| 154 |
|
| 155 |
To override the label, create these entries in your i18n files: |
| 156 |
|
| 157 |
{{< code-toggle file=i18n/en.toml >}} |
| 158 |
caution = 'Caution' |
| 159 |
important = 'Important' |
| 160 |
note = 'Note' |
| 161 |
tip = 'Tip' |
| 162 |
warning = 'Warning' |
| 163 |
{{< /code-toggle >}} |
| 164 |
|
| 165 |
Although you can use one template with conditional logic as shown above, you can also create separate templates for each [`Type`](#type) of blockquote: |
| 166 |
|
| 167 |
```tree |
| 168 |
layouts/ |
| 169 |
└── _markup/ |
| 170 |
├── render-blockquote-alert.html |
| 171 |
└── render-blockquote-regular.html |
| 172 |
``` |
| 173 |
|
| 174 |
{{% include "/_common/render-hooks/pageinner.md" %}} |
| 175 |
|
| 176 |
[CommonMark]: https://spec.commonmark.org/current/ |
| 177 |
[GitHub]: https://docs.github.com/en/get-started/writing-on-github/getting-started-with-writing-and-formatting-on-github/basic-writing-and-formatting-syntax#alerts |
| 178 |
[Markdown attributes]: /content-management/markdown-attributes/ |
| 179 |
[Obsidian]: https://help.obsidian.md/Editing+and+formatting/Callouts |
| 180 |
[Typora]: https://support.typora.io/Markdown-Reference/#callouts--github-style-alerts |
| 181 |
[`RenderShortcodes`]: /methods/page/rendershortcodes/ |