user128511
user128511

Reputation:

Jekyll Post Excerpt: How to know if there was an auto generated excerpt?

If I understand correctly Jekyll takes the first paragraph as an excerpt unless you use one of the various methods mark or specify one manually.

In my case, I want to be able to distinguish in the templates whether there was no excerpt or not so I can effectively do this

{% if post.excerpt %}

    {{ post.excerpt }}

{% else %}

    {{ post.content }}

{% endif %}

Effectively if there was no excerpt use the entire post. As it is, since Jekyll auto generates excerpts the test will always fail.

I suppose one solution so to go to every post that has no excerpt and add <!-- more --> at the very bottom of the post but that's very error prone as in if I forget I'll get the wrong result. I'd prefer to make the default be if I didn't manually mark an excerpt then the entire post appears on the home page.

To put it another way I'm trying to port from Wordpress to Jekyll. Wordpress's behavior is that no excerpt = insert entire post.

Is that possible in Jekyll? Is there some flag or variable I can check in the templates on whether or not an excerpt was manually specified vs auto generated?

Upvotes: 3

Views: 753

Answers (2)

mabalenk
mabalenk

Reputation: 1043

There is an alternative solution with Liquid. You need to check, if the excerpt separator is present in the post:

{% if post.content contains site.excerpt_separator %}
  {{ post.excerpt }}
  <p><a href="{{ post.url | relative_url }}">Read more</a></p>
{% else %}
  {{ post.content }}
{% endif %}

Upvotes: 4

juzraai
juzraai

Reputation: 5943

I don't know any method to tell if an excerpt is manual or generated. Maybe writing a plugin to analyze the raw file's front-matter can be an option (but that would not work on Github Pages for example).

But I may have a solution for this:

I'd prefer to make the default be if I didn't manually mark an excerpt then the entire post appears on the home page.

According to the documentation, you can set excerpt_separator for every page (you can also set it at once in defaults).

Try setting a value which you know will never appear in your posts. If Jekyll doesn't find the separator, it won't separate, so the generated excerpt will be the entire post.

Example:

---
title: Some title
excerpt_separator: "CANTFINDME!"
---
Post line 1

Post line 2

The generated excerpt will be the entire post:

<p>Post line 1</p>
<p>Post line 2</p>

Upvotes: 2

Related Questions