How to inject markdown resources into ``index.md``

I have the following structure:

content/posts/post-1/
├── index.md
├── part-1.md

in index.md I want to inject the content of part-1.md inside it
I have tried to create shortcode from the suggestion in this post but it didn’t work:

execute of template failed at <readFile>: error calling readFile: file does not exist 

The documentation about Page Resources seems also too complex for me. Is the anyway to make this happen?

layouts/shortcodes/inject.html

{{ with .Get 0 }}
  {{ with $.Page.Resources.Get . }}
    {{ .Content }}
  {{ else }}
    {{ errorf "The %s shortcode was unable to get %s. See %s" $.Name . $.Position }}
  {{ end }}
{{ else }}
  {{ errorf "The %s shortcode requires one positional parameter. See %s" .Name .Position }}
{{ end }}

markdown

{{< inject "part-1.md" >}}
2 Likes

Thank you so much. It works. I have tried .Page.Resources.Get without $, what is the difference between them?
Also, it seems like with the injected resource, I cannot get the correct TOC. Is there any solution for this?

References:

We need to fully render the shortcode before Goldmark (the markdown renderer) renders the page.

You need to make two changes.

1) Call the shortcode with the {{% foo %}} notation instead of {{< foo >}}

2) In the third line of the shortcode change this:

{{ .Content }}

to this:

{{ .RawContent }}
1 Like

I forgot something. You cannot indent the {{ .RawContent }} line in the shortcode by 4 or more spaces. If you do, the first line of the content is treated as a code block. So let’s trim the leading whitespace with the {{- action delimiter.

{{ with .Get 0 }}
  {{ with $.Page.Resources.Get . }}
    {{- .RawContent }}
  {{ else }}
    {{ errorf "The %s shortcode was unable to get %s. See %s" $.Name . $.Position }}
  {{ end }}
{{ else }}
  {{ errorf "The %s shortcode requires one positional parameter. See %s" .Name .Position }}
{{ end }}
1 Like

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