Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create default link and image render hooks #11933

Closed
jmooring opened this issue Jan 28, 2024 · 6 comments · Fixed by #11945
Closed

Create default link and image render hooks #11933

jmooring opened this issue Jan 28, 2024 · 6 comments · Fixed by #11945
Assignees
Milestone

Comments

@jmooring
Copy link
Member

jmooring commented Jan 28, 2024

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.

@bep
Copy link
Member

bep commented Jan 28, 2024

Just to be clear, when I do

[markup.goldmark.renderhooks.link]
enable = false

That toggle is just for the "default templates" not the "user provided" ... or both?

@jmooring
Copy link
Member Author

That toggle is just for...

Yeah, I get it. There's a couple of cases I didn't consider. Let me think about this for a bit.

@jmooring
Copy link
Member Author

jmooring commented Jan 28, 2024

Here's my thought process on this...

1) With a multilingual single-host site where markup.goldmark.duplicateResourceFiles = false, do you always need link and image render hooks? Yes. So let's hardcode that bit, always enabling the default link and image render hooks under the following conditions:

  • Site is multilingual, and
  • Site is single-host, and
  • markup.goldmark.duplicateResourceFiles = false

Note the default hooks will always be overridden by user-provided hooks if present (in the same way that any of the built-in templates are overridden, including the built-in GoAT hook).

2) Is there ever a time, with any type of site, that you would want the default render hooks to override user-provided hooks? No.

3) Do we need or want a flag to disable both default and user-provided hooks? No. We don't have a flag like that for heading or code block hooks.

So, what about?

[markup.goldmark.renderhooks.image]
enableDefault = false   <-- hardcoded to true per criteria above

[markup.goldmark.renderhooks.link]
enableDefault = false   <-- hardcoded to true per criteria above

We wouldn't need the multilingualOnly because it would be hardcoded (see item 1).

@bep
Copy link
Member

bep commented Jan 29, 2024

Since detecting a false that's explicitly set is harder than the other way around, I suggest we do:

[markup.goldmark.renderhooks.image]
disableDefault = true   <-- hardcoded to false per criteria above.

@bep bep modified the milestones: v0.123.0, v0.124.0 Jan 29, 2024
@jmooring
Copy link
Member Author

Sounds good to me.

@bep bep self-assigned this Jan 29, 2024
bep added a commit that referenced this issue Jan 30, 2024
bep added a commit that referenced this issue Jan 30, 2024
bep added a commit that referenced this issue Jan 30, 2024
bep added a commit that referenced this issue Jan 30, 2024
bep added a commit that referenced this issue Jan 30, 2024
bep added a commit that referenced this issue Jan 30, 2024
@bep bep closed this as completed in 5b7cb25 Jan 30, 2024
jmooring added a commit to jmooring/hugo-testing that referenced this issue Jan 30, 2024
These are no longer necessary due to the built-in link and image render
hooks that are automatically enabled when:

- Site is multilingual, and
- Site is single-host, and
- markup.goldmark.duplicateResourceFiles = false

See:

- <gohugoio/hugo#11933>
- <gohugoio/hugo#11945>
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 21, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants