在 Jekyll 中的 Markdown 代码块内转义双花括号

发布于 2025-01-10 01:33:36 字数 727 浏览 5 评论 0原文

我正在使用 Jekyll 创建一个文档站点,其中我试图记录一些包含类似车把语法的代码。例如{{foo}}。问题是 Jekyll 使用液体标签,无论我做什么,我的双卷发都会被液体处理器撕掉。

顺便说一下,我使用 kramdown 作为 markdown 处理器。

这是我尝试过的:

{% highlight html linenos %}
  Hello, my name is {{name}}.
{% endhighlight %}

这个完全删除了 {{name}} 部分,因为它认为它是对液体变量的引用。

我也尝试过这个:

{% highlight html linenos %}
  Hello, my name is \{\{name\}\}.
{% endhighlight %}

在这种情况下,我试图转义大括号,但结果是斜杠被渲染到页面中。

我什至尝试过这个:

{% highlight html linenos %}
  Hello, my name is <span>{</span>{name}}.
{% endhighlight %}

不可否认,这个非常愚蠢。在本例中,因为我已将语法指定为 html(它需要如此),所以 span 标记将呈现到页面中。

那么我到底该如何解决这个问题呢?

I'm using Jekyll to create a documentation site wherein I am trying to document some code that contains handlebars-like syntax. For example {{foo}}. The problem is that Jekyll uses liquid tags and no matter what I do, my double curlies are getting ripped out by the liquid processor.

By the way, I'm using kramdown as the markdown processor.

Here is something I've tried:

{% highlight html linenos %}
  Hello, my name is {{name}}.
{% endhighlight %}

This one removes the {{name}} section completely because it thinks it's a reference to a liquid variable.

I also tried this:

{% highlight html linenos %}
  Hello, my name is \{\{name\}\}.
{% endhighlight %}

In this case, I'm trying to escape the curly braces but the result is that the slashes get rendered into the page.

I even tried this:

{% highlight html linenos %}
  Hello, my name is <span>{</span>{name}}.
{% endhighlight %}

Admittedly this one was pretty dumb. In this case, because I've specified the syntax as html (which it needs to be), the span tag gets rendered into the page.

So how in the world can I resolve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

半城柳色半声笛 2025-01-17 01:33:36

您正在寻找 {% raw %} 标签。

{% raw %}
Hello, my name is {{name}}.
{% endraw %}

You're looking for the {% raw %} tag.

{% raw %}
Hello, my name is {{name}}.
{% endraw %}
深海少女心 2025-01-17 01:33:36

您可以使用{% raw %}来确保内容未被Jekyll修改:

{% raw %}
This is inserted literally: {{foo}}
{% endraw %}

但是,请注意,这不是代码块。您将需要额外的代码格式以使您的内容呈现为代码:

{% raw %}
    I'm a code block, because I'm indented by 4 spaces
{% endraw %}
{% raw %}
```handlebars
I'm a code block that contains {{handlebars}}
with highlighting.
```
{% endraw %}

You can use {% raw %} to ensure content is unmodified by Jekyll:

{% raw %}
This is inserted literally: {{foo}}
{% endraw %}

However, note that this is not a code block. You will need additional code formatting to make your content render as code:

{% raw %}
    I'm a code block, because I'm indented by 4 spaces
{% endraw %}
{% raw %}
```handlebars
I'm a code block that contains {{handlebars}}
with highlighting.
```
{% endraw %}
倾其所爱 2025-01-17 01:33:36

使用 jekyll 的代码是:

{% highlight html%}
{% raw %}
     <h2> {{ user.name.first | uppercase }}</h2>
     <p> {{ user.email }}</p>
{% endraw %}
{% endhighlight %}

With jekyll the code is:

{% highlight html%}
{% raw %}
     <h2> {{ user.name.first | uppercase }}</h2>
     <p> {{ user.email }}</p>
{% endraw %}
{% endhighlight %}
红ご颜醉 2025-01-17 01:33:36

供将来参考:使用普通的 {% raw %}{% endraw %} 只是第二好的解决方案,因为如果您在普通 github 上查找 Markdown,就会显示这些解决方案.com。

最好的方法是将 {% raw %}{% endraw %} 放在 HTML 注释中:

<!-- {% raw %} -->
something with curlky brackets like { this } and { that }
<!-- {% endraw %} -->

由于 HTML 注释,Github 将其视为注释。在 Github 页面中,原始标签将阻止解析标签之间的大括号。

For future references: using plain {% raw %} and {% endraw %} is only the second best solution since those are shown if you look up the Markdown on normal github.com.

The best way is to put {% raw %} and {% endraw %} in HTML comments:

<!-- {% raw %} -->
something with curlky brackets like { this } and { that }
<!-- {% endraw %} -->

Due to the HTML comments it is seen by Github as a comment. In Github pages the raw tags will prevent the parsing of the curly brackets in between the tags.

絕版丫頭 2025-01-17 01:33:36

这在 jekyll 中有效:

{%raw%}{{thing}}{%endraw%}

This works in jekyll:

{%raw%}{{thing}}{%endraw%}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文