Shortcodes in FrontMatter don't get the correct `.Page` variable

Hi, I am using the Wowchemy theme for Hugo, which uses a block based approach to construct the single pages. This means that everything is declared in the page Front Matter, and not in the Content.

I have a problem with the shortcodes in the front matter: they cannot access the correct .Page variable (which for instance I need to construct relative paths).

I did some tests, with this content structures:

content
├── _index.md # homepage
├── post/writing-technical-content/ # post example
│   ├── results.csv
│   └── index.md
└── csv/ # leaf boundle
    ├── index.md
    └── data.csv

Using the shortcode in the csv/index.md, I get:

  • Shortcode in the Front Matter
 = &hugolib.ShortcodeWithPage{Params:[]string(nil), Inner:"", Page:(*hugolib.pageForShortcode)(0xc004c68800), Parent:(*hugolib.ShortcodeWithPage)(nil), Name:"page_info", IsNamedParams:false, Ordinal:0, indentation:"", innerDeindentInit:sync.Once{done:0x0, m:sync.Mutex{state:0, sema:0x0}}, innerDeindent:"", posInit:sync.Once{done:0x0, m:sync.Mutex{state:0, sema:0x0}}, posOffset:45, pos:text.Position{Filename:"", Offset:0, LineNumber:0, ColumnNumber:0}, scratch:(*maps.Scratch)(nil)}
.Page = &{Page(/_index.md) nopPage 0xc0013b8270}
.Page.Parent = <nil>
.Page.BundleType = "branch"
.Page.File.Dir = "/"
.Page.RelPermalink = /
  • Shortcode in the page Content:
. = &hugolib.ShortcodeWithPage{Params:[]string(nil), Inner:"", Page:(*hugolib.pageForShortcode)(0xc0087f7240), Parent:(*hugolib.ShortcodeWithPage)(nil), Name:"page_info", IsNamedParams:false, Ordinal:0, indentation:"", innerDeindentInit:sync.Once{done:0x0, m:sync.Mutex{state:0, sema:0x0}}, innerDeindent:"", posInit:sync.Once{done:0x0, m:sync.Mutex{state:0, sema:0x0}}, posOffset:228, pos:text.Position{Filename:"", Offset:0, LineNumber:0, ColumnNumber:0}, scratch:(*maps.Scratch)(nil)}
.Page = &{Page(/csv/index.md) nopPage 0xc004921770}
.Page.Parent = Page(/_index.md)
.Page.BundleType = "leaf"
.Page.File.Dir = "csv/"
.Page.RelPermalink = /csv/

You can see that in the first case Hugo is not able to determine the correct path (csv/), but instead it returns the root /.

You can find the original issue here in the Wowchemy github, but I think this could actually be an error in Hugo.
You can also clone this branch to get a minimal example (run hugo server and then navigate to /csv/ page).

Thank you for your support!

Shortcodes are for content, there is nothing in the docs that suggests you can use them in frontmatter or in templates.
Converting the shortcode to a partial template that picks up the parameters from the frontmatter should do what you need?

I actually found a solution.

A little more context to better understand.
In my front matter I have:

sections:
  - block: markdown
    content:
      title: CSV test
      text: |-
        Text with sortcodes
  - block: myotherblock
    ... more blocks parameters ....
    content:
      title: Title
      text: |-
        Text with sortcodes
  - ... more blocks ...

Then I loop over those blocks and each one calls the corresponding partial template:

{{ range $index, $block := .Params.sections }}
    {{ partial "functions/parse_block" (dict "page" $ "block" $block) }}
{{ end }}

And then inside the markdown block I have something like:

{{ $text := $block.content.text | emojify | markdownify }}
<div> {{ $text }} </div>

I just found in this issue comment that since version 0.93.0 markdownify has been effectively aliased to site.Home.RenderString. So, the problem is that if I parse $block.content.text with markdownify, I am actually using the Home page as context.

Instead, if I change to
{{ $text := $block.content.text | emojify | $page.RenderString }}
I get the correct .Page variable.

Now, I also see that this is actually mentioned in the markdownify docs:

Note: markdownify now supports Render Hooks just like .Page.RenderString. However, if you use more complicated Render Hooks relying on page context, use .Page.RenderString instead.

but I just understand now its meaning.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.