我想在索引页面显示一篇较长文章或页面的简短摘录。我本来打算在Front Matter中使用自定义变量并获取它,但后来看到了.excerpt
过滤器。
在Jekyll文档中,我看到有一个叫做{{ page.excerpt | markdownify }}
的东西。如何标记页面或文章上的Markdown以便使用该过滤器?
编辑:还是markdownify需要整个.md文档吗?
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>
在发布的 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的原因。
对于mu或collections,当遇到解析liquid时,jekyll会出现错误。我不知道为什么会这样,按照你的建议应该可以工作。
有一个替代方案:
使用post.content或者像我的情况一样:blogX.content,并通过一些文本过滤器来限制内容大小。
例如: {{ blog.content | strip_html | truncatewords: 100 }}
84cfc1cef
,jekyll的Github版本支持每篇文章的excerpt_separator
,因此您需要将引用添加到Gemfile中。gem 'jekyll', github: 'jekyll/jekyll', ref: '84cfc1ceff0474fd3eb3beb193ae59ae43694863'
YAML
的文章:---
title: Post Excerpt Separator
excerpt_separator: "\n---\n"
---