In v0.146.3, if a custom shortcode is placed at a lower level in the file tree, it must also be present in the base shortcode directory. Why?

If possible, I’d like to get an explanation of some unexpected behavior (at least to me) that I’m seeing with the new templates system, based on this change:

_shortcodes and _markup directories can be added to any level in the file tree.

Given the following structure (note that I’m only showing relevant files)…

layouts/
├── _markup/
│   └── render-heading.html
├── section1/
│   └── _markup/
│       └── render-link.html
└── section2/

Only links in section1 will use the render-link.html render hook. Great, this works as expected.

Ok, now given the following structure…

layouts/
├── _shortcodes/
│   └── foo.html
├── section1/
│   └── _shortcodes/
│       └── badge.html
└── section2/

I get the follow error:

Error: error building site: process: readAndProcessContent: ".../content/section1/_index.md:9:1": failed to extract shortcode: template for shortcode "badge" not found

If I want the badge shortcode to only be available in section1, it seems like I still need to put an empty badge shortcode in the base shortcode directory to prevent the error.

Why is this? Am I not understanding the intent of this change? Am I getting this error because render hooks have an internal default, and my custom shortcodes do not?

Can you provide a link to where the _shortcode directory is introduced? I missed that somehow and the docs don’t have it (yet). Also the “any level of the file tree” thing is new to me. That would have had to be a bigger announcement I think. Maybe it’s a feature that is still to come?

It’s mentioned in this PR, which was reference in the v0.146.0 release…

1 Like

Based on this I would expect your setup to work. It makes it hard to argue for subfolder _shortcodes to have a base folder version they have to override (or the other way around). This might be a bug and just not yet covered with a test case.

On the other side, there is no explicit comment about shortcodes inside of your section1 only being available inside of section1 - I don’t think that would be an intended feature.

Just my two cent, sorry for wasting your time, but I am interested in how this turns out too :slight_smile:

To answer myself, if you want a shortcode only for section1 you do this:

├── _shortcodes
│   ├── myshortcode.html
│   └── myshortcode.section1.html

Not sure or tested if you need myshortcode.html.

Yes, you are right, this is certainly a leap I made in my mind. I suspect that this is not a bug, but with the new templates, you can only override a base-level shortcode from a lower level.

I’ve had situations where it would be desirable to have a shortcode only available in a certain section, which previously I could handle with template logic, but I was just excited about the possibility of it being built-in.

ah interesting! I’ll try that out.

Maybe you have the shortcode used somewhere outside of that section? Or maybe the shortcode is in a page where your frontmatter changes something about the used template for the page, thus “moving” out of section1?

I have a really basic test…

Also, just using myshortcode.section1.html is interesting in that I can call {{< myshortcode >}} from any section of the site. I find that confusing too, haha.

Oh well, I should probably just wait for the official docs.

1 Like

See https://github.com/gohugoio/hugo/issues/13605.

Thanks!

This was resolved in v0.146.4.

1 Like

Thanks for the update!

1 Like

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