我想托管一些API文档示例代码片段,这些代码片段位于不同的文件中,并被included到Jekyll页面中。我正在使用Jekyll include tag (https://jekyllrb.com/docs/includes/)。
例如,给定以下输入:
The `br` tag is for line breaks. Example:
<pre>
{% include code-snippet.html %}
</pre>code-snippet.html可能是这样的:
<!doctype html>
First line<br>
Second line我得到以下输出,它让浏览器将标记解释为HTML:
The `<br>` tag is for line breaks. Example:
<pre>
<!doctype html>
First line<br>
Second line
</pre>但我更希望得到这样的输出,浏览器将标签解释为文本:
The `<br>` tag is for line breaks. Example:
<pre>
<!doctype html>
First line<br>
Second line
</pre>在Jekyll中有实现这一点的方法吗?有没有像{% include code-snippet.html | htmlEscape %}这样的东西?我想我可以用Ruby插件(毕竟是the include tag is just Ruby)来解决这个问题,但如果可能的话,我希望避免编写插件。
发布于 2020-07-22 19:06:29
我找到了一种解决方法:
您可以使用capture将include读取到一个变量中,然后通过xml_escapehttps://jekyllrb.com/docs/includes/过滤器包含该变量:
{% capture code_snippet %}
{% include code-snippet.html %}
{% endcapture %}
<pre>
{{ code_snippet | xml_escape }}
</pre>发布于 2020-07-22 19:12:30
highlight标记还可以进行HTML转义:
{% highlight html %}
{% include code-snippet.html %}
{% endhighlight %}https://stackoverflow.com/questions/62993509
复制相似问题