Skip to content

Create default link and image render hooks #11933

@jmooring

Description

@jmooring

With the implementation of #11453 and #11904 we no longer duplicate page resources per language1. This is applicable to multilingual, single-host sites.

When resources are not duplicated per language (the default), link and image render hooks are required to properly resolve markdown destinations (links and image).

Per this discussion, the site config would be:

[markup.goldmark.renderhooks.image]
enable = true
multilingualOnly = true

[markup.goldmark.renderhooks.link]
enable = true
multilingualOnly = true

Candidate render hooks:

layouts/_default/_markup/render-image.html
{{- $u := urls.Parse .Destination -}}
{{- $src := $u.String -}}
{{- if not $u.IsAbs -}}
  {{- with or (.Page.Resources.Get $u.Path) (resources.Get $u.Path) -}}
    {{- $src = .RelPermalink -}}
  {{- end -}}
{{- end -}}
{{- $attributes := dict "alt" .Text "src" $src "title" .Title -}}
<img
  {{- range $k, $v := $attributes -}}
    {{- if $v -}}
      {{- printf " %s=%q" $k $v | safeHTMLAttr -}}
    {{- end -}}
  {{- end -}}
>
{{- /**/ -}}
layouts/_default/_markup/render-link.html
{{- $u := urls.Parse .Destination -}}
{{- $href := $u.String -}}
{{- if not $u.IsAbs -}}
  {{- with or
    ($.Page.GetPage $u.Path)
    ($.Page.GetPage (strings.TrimRight "/" $u.Path))
    ($.Page.Resources.Get $u.Path)
    (resources.Get $u.Path)
  -}}
    {{- $href = .RelPermalink -}}
    {{- with $u.RawQuery -}}
      {{- $href = printf "%s?%s" $href . -}}
    {{- end -}}
    {{- with $u.Fragment -}}
      {{- $href = printf "%s#%s" $href . -}}
    {{- end -}}
  {{- end -}}
{{- end -}}
{{- $attributes := dict "href" $href "title" .Title -}}
<a
  {{- range $k, $v := $attributes -}}
    {{- if $v -}}
      {{- printf " %s=%q" $k $v | safeHTMLAttr -}}
    {{- end -}}
  {{- end -}}
>{{ .Text | safeHTML }}</a>
{{- /**/ -}}

Test site:

git clone --single-branch -b hugo-github-issue-11933 https://github.com/jmooring/hugo-testing hugo-github-issue-11933
cd hugo-github-issue-11933
hugo server

Footnotes

  1. This can be disabled by setting markup.goldmark.duplicateResourceFiles = true in site configuration.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions