Best practise to reuse content

I have like 20 pieces of Markdown content, each in a different section. Each content file is are almost identical, only title differs. To avoid duplicating the content, I tried migrating the content to a shortcode to parametrize the differing word, but it looks like shortcodes can’t contain Markdown(?).

Does anyone have any idea how I could reuse the content? I could obviously, migrate the content to templates, but it doesn’t feel “right” to have bulk content in HTML.

Thanks,
Jens

Shortcodes can contain markdown with {% %}

But I’m not sure if that’s what you need. Perhaps using partials in your section pages’ templates is more appropriate in your case.

Shortcodes can contain markdown with {% %}

Are you referring to Shortcodes | Hugo? When I tested, I added Markdown to layout/shortcodes/myshortcode.md and then did {{% myshortcode "My parameter" %}} in my content. I expected the markdown to be converted to HTML when looking in my browser, but it was included unaltered. Did I do a mistake? If it should be possible I’ll see if I can put up an example git repository of this.

Markdown shortcodes inside {% %} are processed by Blackfriday. More here: https://gohugo.io/extras/shortcodes#shortcodes-with-markdown

Also there is the safeHTML function to render HTML with Hugo. See here for more: https://gohugo.io/templates/functions#safehtml

Thanks for quick response.

Please see my POC tests here. The first commit tests {{% %}} which documentation says I should use. The second commit tested embedding {% my markdown %} in the shortcode (I thought I misunderstood you). The third commit tests {% %} which is what you proposed. None of those converts the markdown to HTML in the public directory.

Let me know if I’ve misunderstood something!

No, You don’t use Hugo shortcodes like this {{% **This should be bold** %}}

In your content file you call a shortcode like so: e.g. {{% bold %}}**HELLO**{{%/ bold %}}

And inside bold.md you may have something like this
{{ .Inner }}

Maybe the not-just-yet-official Docs will be more helpful to you https://gohugo.io/content-management/shortcodes/#readout

EDIT
But again, if you have content files that you want to reuse in different sections (or list pages) you should be using partials.

Only if you want to reuse this content in other content (or posts) go for shortcodes.

Something like this, perhaps? Define a shortcode named include.html:

{{ readFile (.Get 0) | markdownify }}

Call it from a content file with:

{{% include "content/foo.txt" %}}

-j

3 Likes

What @jgreely posted is also a smart way to go about it.

EDIT
You can also use the safeHTML function that I mentioned above, if you want to render this file as html.

The question I would raise is: why do you have 20 identical content files?

I don’t think I consider that “content”. Maybe I am wrong, but that sounds like “data”. Read about it at: data files.

Regards,

Bas

1 Like

This is what I want. And I want my reused content to be written in Markdown in layout/shortcodes/myshortcode.md (with the occasional {{ .Get 0 }} for a parameter). I do not wan’t to use inlining. Can this be done? Looking at the examples in Create your own shortcodes | Hugo, all shortcodes have the .html suffix (and not .md) so maybe it’s not possible to have a Markdown shortcode template?

Sure, that’s a good question! Each content is an news announcement going into a new market. They are all identical in text except the name of the country. Any alternative approach would be appreciated!

You can use a .md file as a shortcode. Although that begs the question why does it have to be a .md?

But perhaps you should post an example of what you’re trying to do. Like what you’re trying to include in your shortcode.

Without giving us more information we can’t really help you.

1 Like

I think you should put a md file in your data folder, like data/news/announcement.md.

In the content files *.md frontmatter define: country = "country_name".

In the template rendering the html you can access them both by:

.Site.Data.news.announcement
.Params.country

Regards,

Bas

1 Like

Sounds like an excellent approach. Thanks!

To build on @jgreely’s example, I wrote a readFile shortcode for the new docs site:

However, I use a named parameter instead of a positional one and pass markdownify as an argument…

2 Likes