0
votes

I want to host some API documentation example code snippets, where the snippets live in different files and are included into a Jekyll page. I'm using Jekyll include tag (https://jekyllrb.com/docs/includes/).

e.g. given this input:

The `br` tag is for line breaks. Example:
<pre>
{% include code-snippet.html %}
</pre>

And code-snippet.html might be like:

<!doctype html>
First line<br>
Second line

I get this output, which has the browser interpreting the tags as HTML:

The `<br>` tag is for line breaks. Example:
<pre>
<!doctype html>
First line<br>
Second line
</pre>

But I'd rather get this output, with the browser interpreting the tags as text:

The `<br>` tag is for line breaks. Example:
<pre>
&lt;!doctype html&gt;
First line&lt;br&gt;
Second line
</pre>

Is there a way to achive this in Jekyll? Is there something like {% include code-snippet.html | htmlEscape %}? I imagine I could fix this with a Ruby plugin (after all, the include tag is just Ruby), but I'd like to avoid writing a plugin if possible.

2

2 Answers

0
votes

I found a workaround:

You can read the include into a variable using capture, then include the variable through the xml_escapehttps://jekyllrb.com/docs/includes/ filter:

{% capture code_snippet %}
{% include code-snippet.html %}
{% endcapture %}
<pre>
{{ code_snippet | xml_escape }}
</pre>
0
votes

The highlight tag also does HTML-escaping:

{% highlight html %}
{% include code-snippet.html %}
{% endhighlight %}