HUGO

Less than character in code blocks in a shortcode

I’ve noticed that less than symbols < inside of code blocks that are in shortcodes are getting converted into &lt.

For example, my_shortcode.md has a code block with a less than character:

This is the text of my shortcode, it has a code example:
``` python
if x <= 100:
```

The code block is converted into:

if x &lt;= 100:

Although I noticed that html elements are just fine.

Should I drop an issue in the Goldmark github repo or is this an issue with Hugo?

I guess using the htmlUnescape function on your shortcode’s output might solve your problem.

For a more accurate advice it would be useful to see the actual code of your shortcode (including the {{ }} brackets).

I don’t think the htmlUnescape function is going to be any help.

I did create a repo that shows the problem.

I don’t think this is the intended use of shortcodes, Hugo is probably expecting to only find .html files in the shortcodes folder so this might be what’s causing the encoding/htmlEscaping problem, not sure though. Shortcodes work as pieces of code/template that you can call from your content files to help you process something. What you have in your “shortcode” now is clear content, there’s no processing, so you don’t need to store this in a shortcode and try solving this problem.

Instead you could:

  1. store your markdown content in content/reusables/pieceofcontent.md with front matter telling Hugo not to publish/render it anywhere:
---
_build:
  list: false
  render: false
---

One line ...

```python
if x <= 100:
```
Another line ...
  1. Create a shortcode shortcodes/reusable.html able to get you the content files from that directory:
{{ $path := print "/reusables/" ( .Get "name" ) }}
{{ with .Site.GetPage $path }}
        {{ .Content }}
{{ end }}
  1. and call it from your content file (like from content/somepage.md) like this:

{{< reusable name="pieceofcontent.md" >}}

That seems a bit complicated.
First, shortcodes should be able to handle md content.

But an easier method:

In a content page:

{{< readFile_shortcode file="layouts/shortcodes/shortcode_test.md" >}}

In readFile_shortcode.html

{{ readFile (.Get "file") | markdownify }}

That works, there’s no &lt; in the output. But it seems unnecessarily complicated and also there aren’t any other symbols that are converted to their html value.

The doc you linked is not talking about storing markdown content in a shortcode file the way you do but about processing markdown content in the “.Inner” part of a shortcode call, see the example at the top of the page meaning:

{{% mdshortcode %}}Some markdown stuff to process by the mdshortcode in the *center*.{{% /mdshortcode %}}

Anyway good you found a way that works for you.