This may be obvious to most of the people, but I need some clarification as to what implication this will has on the content authoring.
Render Hooks seems to provide functionality for replacing some of the built-in shortcodes, especially the ones related to hyperlink and image processing. My questions are:
Can we replace ref and relref shortcodes with the use of Render Hooks?
Can we replace figure shortcode with the use of Render Hooks?
If so, how can we pass all the parameters to the Render Hooks template?
If not, is the Render Hooks only applicable to the images as page resources (page bundles)?
In most cases the answer to your questions are yes. But you cannot pass custotom parameters to these hooks, so if you need that, then no (it has been discussed in another thread; it may be possible to encode this into the URL, maybe).
That said, for all of my uses of ref/relref/image in Markdown, render hooks should work great.
How can we access the render hooks variables from the outside?
For example I tried to access the .Text variable from a head partial through various contexts (.Page, .Content) but every time I encountered an error in the form of .Text cannot be evaluated in hugolib.Page or html.template
I can get what I need by using findRE in .Content but I was kind of hoping that there was a more straightforward way.
As there would be potentially many links in page, something like .Content.Text would not make sense.
There is no predefined data structure you can access outside of the hook template. So you need to make up your own if you need it and store it with Scratch.
–
Small Note:
A difference between Blackfriday and Goldmark is that with the latter Hugo’s functions work after the Markdown has been processed to HTML whereas with Blackfriday the opposite happened.
–
It is very important to me to minimize the front matter that needs to be entered manually in the Hugo projects that I maintain and I am willing to sacrifice build time to generate the necessary data automatically.
So that is why I need to do the above.
Also content portability is very important and I use Hugo shortcodes only if I do not have another choice.
Again thanks for the Render Hooks. I have already updated a big project and everything works as intended with the bonus of pure Markdown content files.
Thank you for the suggestion. I tried entering it in the render-image.html template however even before trying to access the scratch variable Hugo complains about: at <.Page.Scratch.Set>: wrong number of args for Set: want 2 got 3
I don’t quite understand the error message since there are only 2 arguments and not 3.
In the Render Hook template: render-image.html I entered:
{{ .Page.Scratch.Set "alt" .PlainText }}
And then in the meta description partial I get the Scratch Variable like so:
{{ with .Content }}{{ $.Page.Scratch.Get "alt" }}{{ else }}{{ $.Site.Params.description }}{{ end }}
I have a couple of notes:
I removed your suggested condition from above i.e. {{ if not (.Page.Scratch.Get "alt") }}...{{ end }} because the Scratch Variable was populated in the first page (in alphabetical order) in which .PlainText was encountered but then it was left blank in subsequent pages.
Setting a Scratch Variable stops at the last instance of .PlainText encountered in a page. So for pages with many images the last value is stored.
I do not remember what exactly I did wrong last night -it was after a Christmas party- but anyway I figured it out this morning.