HUGO

Generate markdown from JSON

Hi!

I’m new in Hugo and don’t know a lot here.
I have the following: 1) JSON file with structured data (project changelog), 2) some text in Markdown file. I wish: append to the Markdown file (after the text) another part of Markdown that generated from structured data.

For example:

content/changelog.md

## Title

Some intro.

## Changes
{{ range $.Site.Data.api.changelog }}
### Changes for {{.version}}
{{ with .messages }}
{{ range . }}
- {{ . }}
{{ end }}
{{ end }}
{{ end }}

data/api/changelog.json

[
  { "version": "1", messages: ["..."] }
]

It demonstrates my intention, but it doesn’t work yet, since Hugo restrict using templating in content files as I know. So, I though I could move the snippet to shortcode as following:

shortcodes/changelog.md

{{ range $.Site.Data.api.changelog }}
### Changes for {{.version}}
{{ with .messages }}
{{ range . }}
- {{ . }}
{{ end }}
{{ end }}
{{ end }}

and content/changelog.md:

## Title

Some intro.

{{< changelog >}}

It produces the following HTML:

...
<h2>Title</h2>
<p>Some intro</p>
"
### Changes for 2021-05-01
... (raw markdown here)
"

So, it isn’t what I need unfortunately. Though, I guess how and why it is happened and I don’t blame the Hugo, looks that it works correctly.

I expected something like this in result HTML:

<h2>Title</h2>
<p>Some intro</p>
<h3>Changes for 2021-05-01</h3>
<p>...</p>

Thanks!

You can call shortcodes two ways:

{{< myshortcode >}}

or

{{% myshortcode %}}

See:

I generally prefer the first method, rendering any markdown to HTML as needed with .Page.RenderString. For example:

markdown

{{< mark >}}
This is **bold** and this is _emphasized_.
{{< /mark >}}

layouts/shortcodes/mark.html

<mark>{{ .Inner | .Page.RenderString }}</mark>

Thanks a lot! @jmooring

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