Remove FrontMatter of content read with readFile

Since it seems to not be possible to include a Content File (or any other, for what matters) into another natively and I would really like such feature to have some sort of composite content, I’ trying to develop a shortcode for that but I stumbled in one problem.

The contents read with readFile, as expected with a raw data read, also comes with the unparsed FrontMatter. I tried to remove it with replaceRE but it seems that Go/Hugo is too much different or poorly implemented for this matter.

I tried the most simple and generic regex I could think:

{{ ( ( (.Get "file") | readFile ) ) | replaceRE "---.*---" "" }}

Nothing. Then I inspected the HTML parsed and I noticed several blank spaces and newlines added before the three dashes and I modified to this:

{{ ( ( (.Get "file") | readFile ) ) | replaceRE "\s*---.*---\s*" "" }}

And then I received a syntax error. I searched about and apparently, backslashes must be escaped. Alright:

{{ ( ( (.Get "file") | readFile ) ) | replaceRE "\\s*---.*---\\s*" "" }}

Nothing again. Then I tried to use the list counterpart of \s:

{{ ( ( (.Get "file") | readFile ) ) | replaceRE "[ \t\n\r\f\v]*---.*---[ \t\n\r\f\v]*" "" }}

Nothing again.

I also tried to split the readFile part from the replaceRE with a variable, tried to use the “non-piped” way for the function and even escaping the dash thinking it could be some sort of special character (even though it shouldn’t) without success.

What the heck should I do to remove this piece of data before I can apply markdownify on it?

I believe that is exactly what page bundles were created for. You might consider putting the files you want to read into a bundle of text files without front matter, and call the specific resource via shortcode. :slight_smile:

As far as I could understand, I’m looking for headless bundles, but I’ve added the headless: true to the leaf that would be included and tried {{ .Site.GetPage "page" (.Get "file") }} in the include shortcode I made and I received Page("Included Page Title")

I suppose it has something to do with the second argument “page” that wasn’t very clear for a change.

This worked for me in a shortcode:

{{ if .Get 1 }}
            {{ markdownify (readFile (.Get 1)) }}
{{ end }}

Does this function stripes off the FrontMatter, leaving only the content? If so, that would solve the issue, yes, but would cause a smaller one, preventing an idea I had of saving a copy of the plugged Content File’s FrontMatter in a variable.

Maybe I’m missing something, but why not just the following instead…

{{ with .Site.GetPage ""whatever-markdown-file-you-want" }}{{.Content}}{{end}}

Because that would go in a Template. What I was trying to do is a Shortcode to be used within other Content Files

Right, so put it in a shortcode then…

{{ $file := .Get 0 }}
{{ with .Site.GetPage $file }}{{.Content}}{{end}}

Then in your content:

{{< grabpage "posts/" >}}

NOTE: At work and can’t test right now…

[EDIT] Also {{ index .Params 0 }}


if you want the markdown replace .Content with .RawContent

1 Like

Well, I finally had some time to get back to that Shortcode and it worked, that GetPage was precisely what I was looking for. I just had to remove the .Summary from the .Content to make Content Files really plugable.

Thank you for your time

1 Like

I would like to add my findings here for anyone who visits here again.
The accepted solution here is:

{{ $file := .Get 0 }}
{{ with .Site.GetPage $file }}{{.Content}}{{end}}

This does work in case you dont want to do any further processing on the file after it has been read an included. This has issues if you are going to further process the markdown in any way.

My usecase was to do an include of an markdown file and then do a custom _default/_markup/render_image.html on all valid markdown files. When I tried to do that with above my render_image.html generated HTML appeared as “text” rather than an actual image link. I tried a lot of combinations but it didnt seem to work.

What worked finally was having the readfile shortcode do:

{{ $file := .Get 0 }}
{{ $file | readFile | replaceRE "^---[\\s\\S]+?---" "" | safeHTML }}

After this my markdown rendering and image handling both worked out great.
Hope this helps someone.


Thank you @ppipada, this work for me too in order to remove the front matter when using readFile.

1 Like

This shortcode requires entering the full file path, isn’t it?

This adaptation should allow to use relative path instead:

    {{ $file := .Get 0 }}
    {{ (printf "%s%s" .Page.File.Dir $file) | readFile | replaceRE "^---[\\s\\S]+?---" "" | safeHTML }}

Insert with

{{% include "" %}}