Merge commit '5be51ac3db225d5df501ed1fa1499c41d97dbf65'

This commit is contained in:
Bjørn Erik Pedersen
2025-04-10 13:04:51 +02:00
987 changed files with 12379 additions and 14083 deletions

View File

@@ -2,14 +2,8 @@
title: Link render hooks
linkTitle: Links
description: Create a link render hook to override the rendering of Markdown links to HTML.
categories: [render hooks]
categories: []
keywords: []
menu:
docs:
parent: render-hooks
weight: 70
weight: 70
toc: true
---
## Markdown
@@ -28,56 +22,44 @@ These components are passed into the render hook [context](g) as shown below.
Link render hook templates receive the following context:
###### Destination
Destination
: (`string`) The link destination.
(`string`) The link destination.
Page
: (`page`) A reference to the current page.
###### Page
PageInner
: {{< new-in 0.125.0 />}}
: (`page`) A reference to a page nested via the [`RenderShortcodes`] method. [See details](#pageinner-details).
(`page`) A reference to the current page.
PlainText
: (`string`) The link description as plain text.
###### PageInner
Text
: (`template.HTML`) The link description.
{{< new-in 0.125.0 />}}
(`page`) A reference to a page nested via the [`RenderShortcodes`] method. [See details](#pageinner-details).
[`RenderShortcodes`]: /methods/page/rendershortcodes
###### PlainText
(`string`) The link description as plain text.
###### Text
(`template.HTML`) The link description.
###### Title
(`string`) The link title.
Title
: (`string`) The link title.
## Examples
{{% note %}}
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.
{{% /note %}}
> [!note]
> 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.
In its default configuration, Hugo renders Markdown links according to the [CommonMark specification]. To create a render hook that does the same thing:
[CommonMark specification]: https://spec.commonmark.org/current/
{{< code file=layouts/_default/_markup/render-link.html copy=true >}}
```go-html-template {file="layouts/_default/_markup/render-link.html" copy=true}
<a href="{{ .Destination | safeURL }}"
{{- with .Title }} title="{{ . }}"{{ end -}}
>
{{- with .Text }}{{ . }}{{ end -}}
</a>
{{- /* chomp trailing newline */ -}}
{{< /code >}}
```
To include a `rel` attribute set to `external` for external links:
{{< code file=layouts/_default/_markup/render-link.html copy=true >}}
```go-html-template {file="layouts/_default/_markup/render-link.html" copy=true}
{{- $u := urls.Parse .Destination -}}
<a href="{{ .Destination | safeURL }}"
{{- with .Title }} title="{{ . }}"{{ end -}}
@@ -86,7 +68,7 @@ To include a `rel` attribute set to `external` for external links:
{{- with .Text }}{{ . }}{{ end -}}
</a>
{{- /* chomp trailing newline */ -}}
{{< /code >}}
```
## Default
@@ -94,8 +76,6 @@ To include a `rel` attribute set to `external` for external links:
Hugo includes an [embedded link render hook] to resolve Markdown link destinations. Disabled by default, you can enable it in your site configuration:
[embedded link render hook]: {{% eturl render-link %}}
{{< code-toggle file=hugo >}}
[markup.goldmark.renderHooks.link]
enableDefault = true
@@ -103,11 +83,8 @@ enableDefault = true
A custom render hook, even when provided by a theme or module, will override the embedded render hook regardless of the configuration setting above.
{{% note %}}
The embedded link render hook is automatically enabled for multilingual single-host sites if [duplication of shared page resources] is disabled. This is the default configuration for multilingual single-host sites.
[duplication of shared page resources]: /getting-started/configuration-markup/#duplicateresourcefiles
{{% /note %}}
> [!note]
> The embedded link render hook is automatically enabled for multilingual single-host sites if [duplication of shared page resources] is disabled. This is the default configuration for multilingual single-host sites.
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.
@@ -123,4 +100,9 @@ source = 'static'
target = 'assets'
{{< /code-toggle >}}
{{% include "/render-hooks/_common/pageinner.md" %}}
{{% include "/_common/render-hooks/pageinner.md" %}}
[`RenderShortcodes`]: /methods/page/rendershortcodes
[CommonMark specification]: https://spec.commonmark.org/current/
[duplication of shared page resources]: /configuration/markup/#duplicateresourcefiles
[embedded link render hook]: {{% eturl render-link %}}