The short code way of including md cause headings missed

I know there is existing topic about how to including anther md file into current md file.

The popular way is to use shortcode, so I create a shortcode ‘include’ like the following

{{ $file := .Get 0  }}

{{ if strings.HasSuffix $file ".html" }}

{{  $file  | readFile  | safeHTML }}

{{ else if strings.HasSuffix $file ".md" }}

{{  $file  | readFile  | markdownify  }}

{{ end }}

and use that shortcode like this

<!--  Requests library introduction -->
{{% include "share/py/requests.md" %}}

The big problem of it is , the headings of included markdown file will NOT be put into the TOC of current md file.

I noticed there is a description in Hugo doc, Shortcodes | Hugo.

In Hugo 0.55 we changed how the % delimiter works. Shortcodes using the % as the outer-most delimiter will now be fully rendered when sent to the content renderer (e.g. Blackfriday for Markdown), meaning they can be part of the generated table of contents, footnotes, etc.

But that just did not work, the headings inside of the included md still missing.

My hugo version is : hugo_extended_0.61.0_Windows-64bit

Is there anying I missed?



I packaged all relevant files in my project into a tiny zip file.

https://github.com/jcyrss/baiyueheiyu/files/4007660/hugo-demo.zip

Just unzip it, run ‘hugo server’, and browse address ''http://localhost:1313/tut/03/" will show the problem.

As the following screenshot shows, headings in included md are missed, while I did use shortcode like {{% include "share/requests.md" %}} in the file content\tut\03.md

OK , I guess i figured out why.

In Hugo 0.55 we changed how the % delimiter works. Shortcodes using the % as the outer-most delimiter will now be fully rendered when sent to the content renderer (e.g. Blackfriday for Markdown), meaning they can be part of the generated table of contents, footnotes, etc.

The above description only applies to those shortcodes with included content directly in related html file.

But for those like having readFile command, the file content to be read is not fully rendered before sent to the markdown processor ( goldmark in my case).

So I have to write a pre-process script in Python to replace include direction with real file content.

Really hope Hugo guys could solve it in better way.

2 Likes

For anyone coming to this now, Hugo has addressed this in v. 0.117 with the .Page.RenderShortcodes method. If you use that, your ToC is processed as expected.

2 Likes

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