Search code examples
markdownjekyllgithub-pages

Escape HTML tags in Jekyll


I'm using Jekyll for github pages site. There are several blocks of code to be shown in <pre> block and included in demo page as working code. For this purpose I've extracted these blocks in _includes.

Lets say, we have basic.html in _includes folder.

Now I'm including it on demo page as working code:

{% include demos/basic.html %}

And I need to include this code in <pre> tag.

<pre class="prettyprint lang-html">{% include demos/basic.html %}</pre>

Here I have issue with HTML tags inside <pre>. They should be escaped. I found xml_escape filter in Jekyll but it cannot be applied to includes.

Could anybody advice me how to cope with this?


Solution

  • Finally, found the solution. We can capture the include in variable and then apply our filter.

    <pre class="prettyprint lang-js">
        {% capture my_include %}{% include demos/basic.html %}{% endcapture %}
        {{ my_include | xml_escape }}</pre>