如何在Jekyll中使用markdownify来在索引页显示摘要

24

我想在索引页面显示一篇较长文章或页面的简短摘录。我本来打算在Front Matter中使用自定义变量并获取它,但后来看到了.excerpt过滤器。

Jekyll文档中,我看到有一个叫做{{ page.excerpt | markdownify }}的东西。如何标记页面或文章上的Markdown以便使用该过滤器?

编辑:还是markdownify需要整个.md文档吗?

4个回答

79

Jekyll有一个选项excerpt_separator,适合您使用。步骤如下:

_config.yml中:

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

在您的文章中:

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

This appears in your `index.html`

This appears, too.

<!--more-->

This doesn't appear. It is separated.

请注意,必须精确地输入<!--more-->,而不是<!--More--><!-- more -->
在您的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 %}

输出结果如下:
<header>
  <h2 class="title"><a href="Your post URL">Foo</a></h2>
</header>

<article>

This appears in your `index.html`

This appears, too.

</article>

@kaplan 这是更合适的答案。它应该被接受。 - kleinfreund
@kleinfreund 实际上,我在第一个答案被接受几个月后回答了这个问题。 - Hgtcl
我知道。这就是为什么我评论的原因。它是更合适的答案。 - kleinfreund
@MaxfanZone:有没有一种方法可以在所需文本之前和之后指定专家分隔符?例如,我希望文章显示仅文本摘录,而不拉出任何图像/嵌入式视频? - dshgna

16

在发布的 Markdown 文件中,您需要首先设置摘要,以下是我其中一篇文章的示例:

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.

然后在首页调用标签

{{ post.excerpt }}

然后,这将输出您在Markdown文件中编写的内容。简单易懂,这就是我喜欢Jekyll的原因。


3

对于mu或collections,当遇到解析liquid时,jekyll会出现错误。我不知道为什么会这样,按照你的建议应该可以工作。

有一个替代方案:

使用post.content或者像我的情况一样:blogX.content,并通过一些文本过滤器来限制内容大小。

例如: {{ blog.content | strip_html | truncatewords: 100 }}


1
截至参考文献84cfc1cef的Github版本支持每篇文章的excerpt_separator,因此您需要将引用添加到Gemfile中。
gem 'jekyll', github: 'jekyll/jekyll', ref: '84cfc1ceff0474fd3eb3beb193ae59ae43694863'

创建一篇带有以下YAML的文章:
---
title:  Post Excerpt Separator
excerpt_separator: "\n---\n"
---

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接