3

我正在将博客移至 Jekyll,并且我想提供一个 RSS 提要。我需要从 RSS 内容中排除一些依赖于 javascript 的功能,例如幻灯片。有没有办法告诉帖子(或{% include %}帖子中的)它是插入index.html文件还是feed.xml文件中?

非常简单,这就是我希望它看起来的样子:

索引.html

---
layout: page
title: Index
include_slideshow: true
---
{% for post in paginator.posts %}
  {{ post.content }}
{% endfor %}

提要.xml

---
layout: nil
include_slideshow: false
---
{% for post in site.posts limit:10 %}
  {{ post.content }}
{% endfor %}

_posts/2013-10-01-random-post.html

---
layout: post
title: Random Post    
---
This is a random post.
{% if include_slideshow %}
INSERT SLIDESHOW HERE.
{% else %}
NOTHING TO SEE HERE, MOVE ALONG.
{% endif %}

问题是index.htmlfeed.xmlinclude_slideshow的 YAML 前端的变量对_posts/2013-10-01-random-post.html 不可用。有没有不同的方法来实现这一点?

我通过 GitHub 页面进行部署,所以 Jekyll 扩展不是一个选项。

4

1 回答 1

2

您可以使用一些拆分过滤器。您不需要识别正在呈现的布局,而是在必要时操纵内容本身。

此示例适用于您的内容上的多个幻灯片块,由 html 注释正确标识:

index.html - 过滤幻灯片内容

---
layout: page
title: Index
---
{% for post in paginator.posts %}
  {%capture post_content %}
  {% assign slideshows = post.content | split: "<!-- SLIDESHOW_CONTENT -->" %}
  {% for slide in slideshows %}
      {% if forloop.first %}
          {{ slide }}
      {% else %}
          {% assign after_slide = slide | split: "<!-- END_SLIDESHOW_CONTENT -->" %}
          {{ after_slide[1] }}
       {% endif %}
  {% endfor %}
  {%endcapture%}
  {{ post_content }}
{% endfor %}

feed.xml - 也过滤幻灯片内容

---
layout: nil
---
{% for post in site.posts limit:10 %}
  {%capture post_content %}
  {% assign slideshows = post.content | split: "<!-- SLIDESHOW_CONTENT -->" %}
  {% for slide in slideshows %}
      {% if forloop.first %}
          {{ slide }}
      {% else %}
          {% assign after_slide = slide | split: "<!-- END_SLIDESHOW_CONTENT -->" %}
          {{ after_slide[1] }}
       {% endif %}
  {% endfor %}
  {%endcapture%}
  <content type="html">{{ post_content | xml_escape }}</content>
{% endfor %}

post.html (您的帖子布局) - 请注意您不需要更改您的帖子模板,因为它会显示幻灯片

---
layout: nil
---
<article>
...
<section>{{ content }}</section>
...
</article>

_posts/2013-10-01-random-post.html

---
layout: post
title: Random Post    
---
<!-- SLIDESHOW_CONTENT -->
<p>INSERT SLIDESHOW 0 HERE</p>
<!-- END_SLIDESHOW_CONTENT -->

This is a random post.

Before slideshow 1!

<!-- SLIDESHOW_CONTENT -->
<p>INSERT SLIDESHOW 1 HERE</p>
<!-- END_SLIDESHOW_CONTENT -->

After slideshow 1!


Before slideshow 2!

<!-- SLIDESHOW_CONTENT -->
<p>INSERT SLIDESHOW 2 HERE</p>
<!-- END_SLIDESHOW_CONTENT -->

After slideshow 2!
于 2013-10-04T18:02:35.953 回答