gohugoio / hugo

The world’s fastest framework for building websites.
https://gohugo.io
Apache License 2.0
74.73k stars 7.45k forks source link

markdownify resolves page resources to home instead of current page when render hooks are present #9692

Open jmooring opened 2 years ago

jmooring commented 2 years ago

The result is either a missing resource or the wrong resource. This is limited to the markdownify function... .Page.RenderString works as expected.

The problem occurs with v0.93.0 and later.

structure

content/
├── test/
│   ├── a.jpg
│   └── index.md
├── b.jpg
└── _index.md

content/test/index.md

{{< img "![kitten](a.jpg)" >}}

layouts/shortcodes/img.html

{{ .Get 0 | markdownify }}

layouts/_default/_markup/render-image.html

{{ with $i := .Page.Resources.GetMatch .Destination }}
  <img src="{{ .RelPermalink }}">
{{ else }}
  {{ errorf "Unable to get %q as a page resource" .Destination }}
{{ end }}

When you run hugo, the result is:

ERROR 2022/03/18 08:46:47 Unable to get "a.jpg" as a page resource

If you do this...

mv content/b.jpg content/a.jpg

...the site builds but the wrong image is displayed.

Example:

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

Reference: https://discourse.gohugo.io/t/37735

bep commented 2 years ago

This is a known limitation (or at least known to me, but I think I have shouted about it). This was somewhat a catch 22 situation and needed to be able to implement render hooks for code blocks. When I get to finish #9570 I can make this work as expected (then I have global access to "the current page", as it is now we always use the home page for markdownify), but until then I would recommend people with advanced markdownify needs to use .Page.RenderString etc.

jmooring commented 2 years ago

I understand. I just wanted to make sure this was being tracked somewhere.

github-actions[bot] commented 1 year ago

This issue has been automatically marked as stale because it has not had recent activity. The resources of the Hugo team are limited, and so we are asking for your help. If this is a bug and you can still reproduce this error on the master branch, please reply with all of the information you have about it in order to keep the issue open. If this is a feature request, and you feel that it is still relevant and valuable, please tell us why. This issue will automatically be closed in the near future if no further activity occurs. Thank you for all your contributions.

willfaught commented 11 months ago

My use case is a render hook for links that checks whether the current page has a parameter set that makes all external links open in a new tab. It would be great if markdownify worked everywhere.

ytrepidorosonomous commented 8 months ago

Pull request closed https://github.com/gohugoio/hugo/pull/9570