Search code examples
jekyllliquidoctopress

How can I omit content from Octopress/Jekyll's extracted excerpt?


I'm using Octopress 2.0 for blogging, which uses the Jekyll static site generator.

To mark an excerpt of a blog post for the front page, you can insert <!--more--> into the post, and the content up to that point will be used as the excerpt..

For some posts, even though I want an excerpt, I want to exclude certain things from it (maybe a table of contents that only makes sense on the post, or some extra notes that aren't useful for an excerpt).

Is there a way in Octopress/Jekyll/Liquid to use the <!--more--> method to generate an excerpt, but also mark some small amount of content to be left out it?


Here is a brief example. Take this post:

---
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.

I want a way to have the generated excerpt for this post be:

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

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

Solution

  • Edit : I now understand what you're trying to do.

    I assume that you're using the default octopress markdown : rdiscount.

    Let's got the filter way :

    In Gemfile add gem 'nokogiri'

    In your post, the idea is to add a span.secondary to the part that has sometimes to be stripped. content

    ...
    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**
    

    In _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 %}
    

    In _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
    
    ...
    

    Install Nokogiri bundle update

    I hop your rake generate will make you happy.