Shortcode processing order

Hi,
I needed to include the content of one markdown content file in another and it was suggested to me that the best way to do it would be with a custom shortcode. So I created a simple shortcode as follows
{{ $file := .Get “file” | readFile }}
{{ (print “\n” $file “\n”) | markdownify }}

This does work as expected in that i’m able to include the content of one file into another. However this seems to be screwing up my toc. I was hoping to have the toc be generated based on the whole content, i.e the content of the main file as well as the included content. However, because the partial template that is responsible for rendering the toc in my theme gets called first, it ends up creating a toc only based on the content in the main file but not the included content.

Is there a way to have my shortcode be called/processed before the page html is rendered so that the toc is generated based on the “final content” ?

I believe this has open discussions in the forums and/or GitHub issues. I don’t recall there being a way due to how BlackFriday, the markdown processor, generates the document tree, or something. I don’t fully recall, but search for it, you’ll find the discussions. :slight_smile:

Thanks for the reply. I did do a search in the forums but didn’t find any specific discussion. I’ll check GitHub issues but from what you are mentioning, it looks like this wouldn’t be possible :worried:
That seems a bit limiting. Are there any other alternative ways to achieve the same result ?

I was hoping that page resources would help you. But just tested, and the ToC only picks up headers from the original content file, not any of the page resource files.

There is an open issue about fixing this, and we will eventually get there.

Thanks for the update. Could you please point me to the issue. I would like to track it and if possible, contribute.

A post was split to a new topic: Create a TOC with Javascript

@darshakthakore my response to this post was moved to a separate tips-n-tricks post — Create a custom page TOC ("mini TOC") with Javascript (which I’ve now edited to match an independent tip) — so I’m not sure whether you had a chance to see it. As I had previously written, we’re in the process of implementing an include-file shortcode, and I found that when using our site’s custom JS-based page-TOC (“mini TOC”) implementation (now shared in the tips post), the headings in included files appears in the correct relative location in the in the TOC of the including page. I hope this helps.

Note that you can simplify your readFile include shortcode like this:

{{- printf "<br/>%s<br/>" (readFile (.Get "file")) | markdownify -}}

See my separate Include-file shortcode for including files with shortcode calls and and Include-file shortcode context issues posts for more include-file shortcode observations.

@sharonl thanks a lot for the response. I did check it and am trying it out now. Will report back on it but I really appreciate you providing the info.

cheers.

Also note that I’m working on simplifying the “page and content etc.” part of Hugo’s source code to prepare for “Pages from data sources”, and I have this and other related issues in the back of my head while doing so.

3 Likes