How do I use markdownify in Jekyll to show an exce

2019-03-22 17:36发布

问题:

I'm looking to show a short excerpt of text from a longer post or page on the index page. I was going to use a custom variable in the Front Matter and grab that, but then I saw the filter for .excerpt

I see in the Jekyll docs there's something called {{ page.excerpt | markdownify }} How would I markup the markdown on a page or post in order to use that filter?

edit: Or does markdownify take the entire .md document?

回答1:

In the post markdown files you need to first set your excerpt, here is an example from one of my posts

layout: post
title: A developers toolkit
date: Friday 14 December, 2012
excerpt: What text editor to use? Sass or plain old CSS? What on earth is Compass? Command    line? I'm not touching that. Sound like you? Welcome, I was once like you and this is the guide I wish someone had given me.

Then on the index page call the tag

{{ post.excerpt }}

This should then output what you have wrote in the markdown file. Nice and simple and why I love Jekyll.



回答2:

Jekyll has an option excerpt_separator, which is suitable for you. Things go like this:

In _config.yml:

excerpt_separator: <!--more-->  # you can specify your own separator, of course.

In you post:

---
layout: post
title: Foo
---

This appears in your `index.html`

This appears, too.

<!--more-->

This doesn't appear. It is separated.

Note you must type exactly <!--more-->, not <!--More--> or <!-- more -->.

In your index.html:

<!-- Loop in you posts -->
{% for post in site.posts %}
  <!-- Here's the header -->
  <header>
    <h2 class="title"><a href="{{ post.url }}">{{ post.title }}</a></h2>
  </header>

  <!-- Your post's summary goes here -->
  <article>{{ post.excerpt }}</article> 
{% endfor %}

The output is like this:

<header>
  <h2 class="title"><a href="Your post URL">Foo</a></h2>
</header>

<article>

This appears in your `index.html`

This appears, too.

</article>


回答3:

doesnt work for mu, or collections, jekyll panics when hitting the except parsing liquid. I dont know why this is, it should work as you suggest.

there is an alternative:

post.content or as my case is: blogX.content and smash it through some text filters that limit the content size.

ie: {{ blog.content | strip_html | truncatewords: 100 }}



回答4:

As of ref 84cfc1cef the github version of jekyll supports the per-post excerpt_separator so you have to add the reference to Gemfile:

gem 'jekyll', github: 'jekyll/jekyll', ref: '84cfc1ceff0474fd3eb3beb193ae59ae43694863'

and create a post with the following YAML:

---
title:  Post Excerpt Separator
excerpt_separator: "\n---\n"
---