Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

jekyll

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?


2 Answers

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 %}
like image 52
mabalenk Avatar answered Oct 19 '25 14:10

mabalenk


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>
like image 31
juzraai Avatar answered Oct 19 '25 12:10

juzraai