【问题标题】:Jekyll code in jekylljekyll 中的 Jekyll 代码
【发布时间】:2014-02-26 14:32:04
【问题描述】:

我正在为 Jekyll 创建一个鸟瞰图教程,托管在 Github 页面上(在我在 Jekyll 上运行的博客上)。所以,我想在那里放一些代码。如果我输入以下内容:

{% for post in site.posts %}
{% if post.categories contains '<categoryname>' %}
    <h2>
      <a href="{{ post.url }}">{{ post.title }}</a>
    </h2>
{% endif %}
{% endfor %}

(制表符后的所有行),它不会呈现为代码,而是执行。如何阻止它执行并将其呈现为代码?

【问题讨论】:

标签: html ruby markdown jekyll github-pages


【解决方案1】:

Jekyll 使用的{%...%} 语法是Liquid templating engine 的一部分。要转义这些标签并按字面意思显示它们,您应该使用raw tag

您可能希望将此与代码块的降价语法结合起来。使用 Redcarpet,您可以使用三重反引号语法。将反引号放在 raw 标记内还是相反都没关系:

{%raw%}
```
{% for post in site.posts %}
{% if post.categories contains '<categoryname>' %}
    <h2>
      <a href="{{ post.url }}">{{ post.title }}</a>
    </h2>
{% endif %}
{% endfor %}
```
{%endraw%}

【讨论】:

    【解决方案2】:

    用反引号括起来你的代码:

    (使用 redcarpet markdown 引擎测试)

    ```
    {% for post in site.posts %}
    {% if post.categories contains '<categoryname>' %}
        <h2>
          <a href="{{ post.url }}">{{ post.title }}</a>
        </h2>
    {% endif %}
    {% endfor %}
    ```
    

    【讨论】:

    • markdown 引擎在你的 _config.yml 中指定。我有这行:markdown: redcarpet
    • 啊!我正在使用rdiscount。那么@zeis 引擎有问题吗?
    • 不同的引擎可能对代码块使用不同的标记。
    • 知道这可能使用哪个标记。选项卡在大多数情况下都有效
    • 我不使用 rdiscount,无论如何试试这个: {% highlight html %} YOUR CODE HERE {% endhighlight %} 这应该适用于启用 pygments,但我还没有测试过。跨度>
    【解决方案3】:

    至少存在三个选项,可用于在 Jekyll 中格式化代码:

    1. highlight - Jekyll 已内置
    {% 高亮 java %} ValidationResult validationResult = NetLicensing.LicenseeService.validate(context, licenseeNumber); {% endhighlight %}

    示例:https://raw.githubusercontent.com/Labs64/netlicensing.io/gh-pages/_drafts/2010-09-16-post-template.md(参见语法高亮部分)

    1. 反引号 - GitHub 风格
    ```java ValidationResult validationResult = NetLicensing.LicenseeService.validate(context, licenseeNumber); ```
    1. HTML 前/代码 - HTML 也可以包含在 Markdown 中

    <pre><code> ValidationResult validationResult = NetLicensing.LicenseeService.validate(context, licenseeNumber); <code/></pre>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多