HUGO

Include an md file

I am converting from Jekyll to Hugo

I have many code snippets in small md files that were included in other pages
Example:

{% include /COM/xap102/ops-change.markdown %}
{% include /COM/xap102/ops-read.markdown %}

How can I accomplish this with Hugo ?

Have a look at the shortcodes:

The inner content of these can be either Markdown or pure HTML (look at the docs). Combine it with the highlight template func and you should have a solid foundation:

And then there is this:

Something in the back of my mind makes me think that the mmark backend has
support for transclusion already - i’m not at my computer atm, so i can’t
confirm it though

sven

Yes, that seems to be correct:

Then you just name your files .mmark and use whatever syntax that project provides.

cool

tks

Hi, bep, I’ve tried with the way of creating a short code like the following

{{$file := .Get 0}}
{{ $file | readFile | markdownify }}

The big problem of this way is , the headings of included markdown file will NOT be put into the TOC. Is there any solution for that?

If you include the shortcode using the “{{%” in the content file, this should work fine (assuming you have a reasonably new Hugo).

I did using “{{%” in the content file, the details are in the following post, is there anything I missed?

Probably some detail WE missed. And that is hard to determine without knowing the details.

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

hugo version : hugo_extended_0.61.0_Windows-64bit

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.

OK,finally solved

{{$file := .Get 0}}
{{ $file | readFile | markdownify }}

should be

{{$file := .Get 0}}
{{ $file | readFile | safeHTML}}

Thank you for this discussion – it taught me how to include files without having to pull their content using python first! However, the headings still don’t show up in the TOC – how can they be made to show up? I am including ~30-40 files and the TOC really is required here. You seem to have come across this problem – maybe you’ve found a solution?