21
votes

I am using Jekyll to create a blog by following this excellent tutorial. I would like to add a post summary on the index page. I tried using:

post.content | truncatewords:50 | strip_html

it works but it displays the entire post until the 50 word count is reached. This includes the heading too. I would like to just summarize the actual content of the post. How can I structure my posts to do this?

4

4 Answers

35
votes

Update 16 Nov, 2015

Now Jekyll support excerpt separator, In template you can do this:

{% if post.excerpt %}
    {{ post.excerpt }}
{% endif %}

and In global config _config.yml you can set:

excerpt_separator: <!--more-->

and the same use with <!--more--> html comment tag.

Old answer

You can try this:

{% if post.content contains '<!--more-->' %}
  {{ post.content | split:'<!--more-->' | first }}
{% else %}
  {{ post.content }}
{% endif %}

and add <!--more--> tag in the article after summary, just like Wordpress.

4
votes

Use YAML front matter and define a separate title per post, like this:

---
title: Efficient smuflet based kwoxel trees
---

Post content goes here.

Then you can use or not use post.title as you please.

Or, if you want to write a separate summary (not just the first n characters) for each post, just add a field for that summary in the front matter as well.

4
votes

From the Jekyll documentation:

Each post automatically takes the first block of text, from the beginning of the content to the first occurrence of excerpt_separator, and sets it as the post.excerpt.

...

Because Jekyll grabs the first paragraph you will not need to wrap the excerpt in p tags, which is already done for you.

See http://jekyllrb.com/docs/posts/#post-excerpts for more info and an example.

3
votes

Use {{ post.excerpt }} in your index.md file to get an excerpt of this post.