| 1 |
--- |
| 2 |
title: Link render hooks |
| 3 |
linkTitle: Links |
| 4 |
description: Create a link render hook to override the rendering of Markdown links to HTML. |
| 5 |
categories: [] |
| 6 |
keywords: [] |
| 7 |
--- |
| 8 |
|
| 9 |
## Markdown |
| 10 |
|
| 11 |
A Markdown link has three components: the link text, the link destination, and optionally the link title. |
| 12 |
|
| 13 |
```text |
| 14 |
[Post 1](/posts/post-1 "My first post") |
| 15 |
------ ------------- ------------- |
| 16 |
text destination title |
| 17 |
``` |
| 18 |
|
| 19 |
These components are passed into the render hook [context](g) as shown below. |
| 20 |
|
| 21 |
## Context |
| 22 |
|
| 23 |
Link _render hook_ templates receive the following context: |
| 24 |
|
| 25 |
`Destination` |
| 26 |
: (`string`) The link destination. |
| 27 |
|
| 28 |
`Ordinal` |
| 29 |
: {{< new-in v0.160.0 />}} |
| 30 |
: (`int`) The zero-based ordinal of the link on the page. |
| 31 |
|
| 32 |
`Page` |
| 33 |
: (`page`) A reference to the current page. |
| 34 |
|
| 35 |
`PageInner` |
| 36 |
: (`page`) A reference to a page nested via the [`RenderShortcodes`][] method. [See details](#pageinner-details). |
| 37 |
|
| 38 |
`PlainText` |
| 39 |
: (`string`) The link description as plain text. |
| 40 |
|
| 41 |
`Position` |
| 42 |
: {{< new-in 0.160.0 />}} |
| 43 |
: (`string`) The position of the link within the page content. |
| 44 |
|
| 45 |
`Text` |
| 46 |
: (`template.HTML`) The link description. |
| 47 |
|
| 48 |
`Title` |
| 49 |
: (`string`) The link title. |
| 50 |
|
| 51 |
## Examples |
| 52 |
|
| 53 |
> [!NOTE] |
| 54 |
> 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. |
| 55 |
|
| 56 |
In its default configuration, Hugo renders Markdown links according to the [CommonMark][] specification. To create a render hook that does the same thing: |
| 57 |
|
| 58 |
```go-html-template {file="layouts/_markup/render-link.html" copy=true} |
| 59 |
<a href="{{ .Destination | safeURL }}" |
| 60 |
{{- with .Title }} title="{{ . }}"{{ end -}} |
| 61 |
> |
| 62 |
{{- with .Text }}{{ . }}{{ end -}} |
| 63 |
</a> |
| 64 |
{{- /* chomp trailing newline */ -}} |
| 65 |
``` |
| 66 |
|
| 67 |
To include a `rel` attribute set to `external` for external links: |
| 68 |
|
| 69 |
```go-html-template {file="layouts/_markup/render-link.html" copy=true} |
| 70 |
{{- $u := urls.Parse .Destination -}} |
| 71 |
<a href="{{ .Destination | safeURL }}" |
| 72 |
{{- with .Title }} title="{{ . }}"{{ end -}} |
| 73 |
{{- if $u.IsAbs }} rel="external"{{ end -}} |
| 74 |
> |
| 75 |
{{- with .Text }}{{ . }}{{ end -}} |
| 76 |
</a> |
| 77 |
{{- /* chomp trailing newline */ -}} |
| 78 |
``` |
| 79 |
|
| 80 |
## Embedded |
| 81 |
|
| 82 |
Hugo includes an [embedded link render hook][] to resolve Markdown link destinations. You can adjust its behavior in your project configuration. This is the default setting: |
| 83 |
|
| 84 |
{{< code-toggle file=hugo >}} |
| 85 |
[markup.goldmark.renderHooks.link] |
| 86 |
useEmbedded = 'auto' |
| 87 |
{{< /code-toggle >}} |
| 88 |
|
| 89 |
When set to `auto` as shown above, Hugo automatically uses the embedded link 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 link render hooks are defined by your project, modules, or themes, these will be used instead. |
| 90 |
|
| 91 |
You can also configure Hugo to `always` use the embedded link render hook, use it only as a `fallback`, or `never` use it. See [details][]. |
| 92 |
|
| 93 |
The embedded link render hook resolves internal Markdown destinations by looking for a matching page, falling back to a matching [page resource](g), then 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. |
| 94 |
|
| 95 |
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: |
| 96 |
|
| 97 |
{{< code-toggle file=hugo >}} |
| 98 |
[[module.mounts]] |
| 99 |
source = 'assets' |
| 100 |
target = 'assets' |
| 101 |
|
| 102 |
[[module.mounts]] |
| 103 |
source = 'static' |
| 104 |
target = 'assets' |
| 105 |
{{< /code-toggle >}} |
| 106 |
|
| 107 |
{{% include "/_common/render-hooks/pageinner.md" %}} |
| 108 |
|
| 109 |
[CommonMark]: https://spec.commonmark.org/current/ |
| 110 |
[`RenderShortcodes`]: /methods/page/rendershortcodes/ |
| 111 |
[details]: /configuration/markup/#renderhookslinkuseembedded |
| 112 |
[duplication of shared page resources]: /configuration/markup/#duplicateresourcefiles |
| 113 |
[embedded link render hook]: <{{% eturl render-link %}}> |