【问题标题】:How to use Jekyll code in (inline) code highlighting?如何在(内联)代码突出显示中使用 Jekyll 代码?
【发布时间】:2013-12-13 14:02:13
【问题描述】:

如何在代码突出显示中使用例如{{ post.date | date: "%H:%M %p - %-d %b %y" }}

到目前为止,我发现的唯一方法是使用{% raw %}{{ post.date | date: "%H:%M %p - %-d %b %y" }}{% endraw %}。但是,它会将代码 sn-p 显示为未格式化的文本,而不是适当的内联代码突出显示。

如果我使用

`{{ post.date | date: "%H:%M %p - %-d %b %y" }}`

然后代码被渲染而不是显示为代码。

【问题讨论】:

    标签: jekyll liquid


    【解决方案1】:
    {% highlight html %}
    {% raw %}
      {% include google_analytics.html %}
    {% endraw %}
    {% endhighlight %}
    

    【讨论】:

    • 为什么会被接受?这对我来说就像是块突出显示。
    • 这对我来说会引发一个未知标签错误(尽管它仍然有效)。我最终只使用了 {% highlight %}
    【解决方案2】:

    我就是这样做的。

    现场查看: http://www.madhur.co.in/blog/2013/11/05/makingmostdatadirectory.html

    {% highlight html %}
    {% raw %} 
    {% for project in site.data.projects %}
    
        {% if project.publish == true %}
            <strong><a href="/projects/{{ project.project }}.html">{{ project.project }}</a></strong>
    
            <span class="tag-project">{{ project.category }}</span>
            {{ project.description }}
            <hr/>
        {% endif %}
    
    {% endfor %}
    </div>
    {% endraw %} 
    {% endhighlight %}
    

    【讨论】:

    • 这适用于块代码突出显示。但是如何突出显示本来会呈现的 Jekyll 内联代码?
    【解决方案3】:

    您可以通过结合 {% highlight %} 和 {% raw %} 轻松做到这一点 例如

    {% highlight ruby %}
    {% raw %}
            ---
            limit: 100
            ---
    
            {% for post in site.posts limit: page.limit %}
    
                {
                  "title": "{{ post.title }}",
                  "date"     : "{{ post.date | date: "%B %d, %Y" }}",
    
                  "excerpt" : "{{ post.excerpt }}",
                  {% if post.categories %} "categories"  : [
                    {% for category in post.categories %} "{{ category }}"
                    {% if forloop.last %}{% else %},{% endif %}
                    {% endfor %}
                    ],
                  {% endif %}
                  {% if post.categories == nil %} "categories"  : [],  {% endif %}
                  "url": "{{ post.url }}",
                  {% if post.tags %} "tags"  : [
                    {% for tag in post.tags %} "{{ tag }}"
                    {% if forloop.last %}{% else %},{% endif %}
                    {% endfor %}
                    ]
                  {% endif %}
                  {% if post.tags == nil %} "tags"  : []  {% endif %}
    
                }
    
                {% unless forloop.last %},{% endunless %}
    
            {% endfor %}
    {% endraw %}
    {% endhighlight %} 
    

    【讨论】:

      猜你喜欢
      • 2015-09-14
      • 1970-01-01
      • 2012-06-07
      • 1970-01-01
      • 1970-01-01
      • 2017-03-28
      • 1970-01-01
      • 2019-05-21
      相关资源
      最近更新 更多