首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Jekyll:如何在html转义代码-snippet.html时{% include code-snippet.html%}

Jekyll:如何在html转义代码-snippet.html时{% include code-snippet.html%}
EN

Stack Overflow用户
提问于 2020-07-20 18:33:46
回答 2查看 77关注 0票数 0

我想托管一些API文档示例代码片段,这些代码片段位于不同的文件中,并被included到Jekyll页面中。我正在使用Jekyll include tag (https://jekyllrb.com/docs/includes/)。

例如,给定以下输入:

代码语言:javascript
复制
The `br` tag is for line breaks. Example:
<pre>
{% include code-snippet.html %}
</pre>

code-snippet.html可能是这样的:

代码语言:javascript
复制
<!doctype html>
First line<br>
Second line

我得到以下输出,它让浏览器将标记解释为HTML:

代码语言:javascript
复制
The `<br>` tag is for line breaks. Example:
<pre>
<!doctype html>
First line<br>
Second line
</pre>

但我更希望得到这样的输出,浏览器将标签解释为文本:

代码语言:javascript
复制
The `<br>` tag is for line breaks. Example:
<pre>
&lt;!doctype html&gt;
First line&lt;br&gt;
Second line
</pre>

在Jekyll中有实现这一点的方法吗?有没有像{% include code-snippet.html | htmlEscape %}这样的东西?我想我可以用Ruby插件(毕竟是the include tag is just Ruby)来解决这个问题,但如果可能的话,我希望避免编写插件。

EN

回答 2

Stack Overflow用户

发布于 2020-07-22 19:06:29

我找到了一种解决方法:

您可以使用captureinclude读取到一个变量中,然后通过xml_escapehttps://jekyllrb.com/docs/includes/过滤器包含该变量:

代码语言:javascript
复制
{% capture code_snippet %}
{% include code-snippet.html %}
{% endcapture %}
<pre>
{{ code_snippet | xml_escape }}
</pre>
票数 0
EN

Stack Overflow用户

发布于 2020-07-22 19:12:30

highlight标记还可以进行HTML转义:

代码语言:javascript
复制
{% highlight html %}
{% include code-snippet.html %}
{% endhighlight %}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62993509

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档