Including plaintext from Markdown

I am trying to include external source files into my markdown while still retaining syntax highlighting.

I have almost got it working writing a shortcode like this

```{% if lang %}{{lang}}{% endif %}
{% set data = load_data(path=path, format="plain") %}
{{ data | safe  }}
```

Unfortunately the load_data function does not retain empty new lines which are really nice for code readability.

Is it possible to simply load an external file as a string?

1 Like

so the newline characters are being omitted by the load_data function?

Yeah the newlines are being omitted by load_data (but only on empty lines).
If I add any printing character (e.g. #) on the line, it is not discarded.

Unfortunately it seems I also can’t use

{% include path %}

because path has to be a string and can’t be a variable.

Is there a way to do this? I’m trying to avoid GitHub gists.

sounds like a bug in load_data, should probably be reported. Or perhaps it is a bug in the markdown rendering. What happens if you just manually paste the code in that triple quote block, does it still omit the newlines?

I don’t think it’s the Markdown rendering, if I paste my code into the code fences it displays correctly with all the empty lines preserved.

Actually after playing around with it a bit more, the newlines are in fact preserved by load_data, but somehow the Markdown renderer doesn’t pick them up properly.

There are https://github.com/Keats/tera/issues/425 and https://github.com/Keats/tera/issues/320 for somewhat related Tera issues.

I will have to test what the shortcode output is, I only thought of shortcode output as HTML so I wouldn’t be surprised if there are issues with it.