如何省略Octopress / Jekyll提取的摘录中的内容?

时间:2014-09-11 05:16:24

标签: jekyll liquid octopress

我使用Octopress 2.0进行博客,该博客使用的是Jekyll静态网站生成器。

要标记首页博客文章的摘录,您可以在帖子中插入<!--more-->,直到该点的内容将用作摘录..

对于某些帖子,即使我想要一个摘录,我想要从中排除某些内容(也许是一个只对帖子有意义的目录,或者对摘录有用的一些额外的注释) )。

在Octopress / Jekyll / Liquid中是否有办法使用<!--more-->方法生成摘录,但是标记了一些少量内容要被遗漏?


这是一个简短的例子。接受这篇文章:

---
layout: post
title: "Example Post"
---

This is the first paragraph. It will be included in the excerpt.

[Jump to third paragraph](#para3). This paragraph should **not** be in the excerpt.

This is the second paragraph. It will be included in the excerpt.

<!--more-->

<a name="para3"></a>This is the third paragraph. It won't be in the excerpt.

我想要一种方法让这篇文章的摘录为:

This is the first paragraph. It will be included in the excerpt.

This is the second paragraph. It will be included in the excerpt.

1 个答案:

答案 0 :(得分:2)

修改:我现在明白你要做的是什么。

我假设你使用默认的章鱼降价:rdiscount

让我们得到过滤方式:

Gemfile 中添加gem 'nokogiri'

在你的帖子中,我们的想法是将span.secondary添加到有时被剥离的部分。含量

...
remove_secondary_content_from_excerpt : true
---
This is the first paragraph. It will be included in the excerpt.

[[Jump to third paragraph](#para3). This paragraph should **not** 
be in the excerpt.](class:secondary)

This is the second paragraph. It will be included in the excerpt.
<!--more-->
### This is the TEXT title
This is **the text**

在_includes / article.html

...
  </header>
{% endunless %}
{% if index %}
    <div class="entry-content">
        {% if post.remove_secondary_content_from_excerpt == true %}
        {% capture secondary_content %}{{ post.excerpt | excerpt_get_secondary_content }}{% endcapture %}
        {{ post.excerpt | remove: secondary_content }}
        {% else %}
        {{ post.excerpt }}
        {% endif %}
    </div>
  {% capture excerpted %}{{ content | has_excerpt }}{% endcapture %}
  {% if excerpted == 'true' %}
    <footer>
      <a rel="full-article" href="{{ root_url }}{{ post.url }}">{{ site.excerpt_link }}</a>
    </footer>
  {% endif %}
{% else %}
<div class="entry-content">
    <!-- example on how to use it in post page -->
    {% if page.remove_secondary_content_from_excerpt == true %}
        {% capture secondary_content %}{{ page.excerpt | excerpt_get_secondary_content }}{% endcapture %}
        {{ page.excerpt | remove: secondary_content }}
        {{ secondary_content }}
        {{ page.content | markdownify | remove: page.excerpt }}
    {% else %}
        {{ content }}
    {% endif %}
</div>
{% endif %}

在_plugins / octopress_filters.rb

...
module OctopressLiquidFilters

  def excerpt_get_secondary_content(input)
    require 'nokogiri'
    doc = Nokogiri::HTML(input)
    # as excerpt can surrounded by one <p> (when no double newline in it)
    # or with multiple <p> when a double newline is found
    multiparagraph = doc.css("p").length > 1

    if multiparagraph
      # look for parent <p>
      xpathString = "span.secondary/.."
    end
      # look only for the span element
      xpathString = "span.secondary"
    else

    secondary = doc.css(xpathString)
    secondary.to_s
  end

...

安装Nokogiri bundle update

我跳你的rake generate会让你开心。