如何在Jekyll中使用markdownify来显示索引的摘录

时间:2013-05-07 15:29:15

标签: markdown jekyll liquid

我希望在索引页面上显示较长帖子或页面的简短文本摘录。我打算在Front Matter中使用自定义变量并抓住它,但后来我看到了.excerpt的过滤器

我在Jekyll docs中看到有一个名为{{ page.excerpt | markdownify }}的内容我如何在页面或帖子上标记降价以使用该过滤器?

编辑:或者markdownify是否会获取整个.md文档?

4 个答案:

答案 0 :(得分:74)

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>

答案 1 :(得分:14)

在邮政标记文件中,您需要先设置摘录,以下是我的一篇文章中的示例

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。

答案 2 :(得分:2)

对mu或者集合不起作用,jekyll在击中除解析液体外时会发生恐慌。我不知道为什么会这样,它应该按你的建议工作。

还有另一种选择:

post.content或者我的情况是:blogX.content并通过一些限制内容大小的文本过滤器粉碎它。

即: {{blog.content | strip_html |截断词:100}}

答案 3 :(得分:1)

从参考84cfc1cef开始,的github版本支持每个帖子excerpt_separator,因此您必须添加对 Gemfile 的引用:

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

并使用以下YAML创建帖子:

---
title:  Post Excerpt Separator
excerpt_separator: "\n---\n"
---
相关问题