【问题标题】:How do I use markdownify in Jekyll to show an excerpt on the index如何在 Jekyll 中使用 markdownify 在索引中显示摘录
【发布时间】:2013-05-01 15:16:06
【问题描述】:

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

我在Jekyll docs 中看到了一个叫做{{ page.excerpt | markdownify }} 的东西我如何在页面或帖子上标记降价以使用该过滤器?

edit:还是 markdownify 会占用整个 .md 文档?

【问题讨论】:

    标签: markdown jekyll liquid


    【解决方案1】:

    在帖子降价文件中,您需要先设置摘录,这是我的一篇帖子中的示例

    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 }}
    

    这应该会输出你在降价文件中写的内容。很好很简单,为什么我喜欢 Jekyll。

    【讨论】:

      【解决方案2】:

      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.
      

      请注意,您必须准确输入&lt;!--more--&gt;,而不是&lt;!--More--&gt;&lt;!-- more --&gt;

      在你的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 实际上,我在第一个答案被接受几个月后回答了这个问题。
      • 我知道。这就是我发表评论的原因。这是更合适的答案。
      • @MaxfanZone:有没有办法在所需文本之前和之后指定专家分隔符?例如,我希望帖子显示纯文本摘录而不提取任何图像/嵌入视频?
      【解决方案3】:

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

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

      并使用以下YAML 创建帖子:

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

      【讨论】:

        【解决方案4】:

        不适用于 mu 或集合,jekyll 在遇到除了解析液体时会出现恐慌。我不知道这是为什么,它应该按照你的建议工作。

        还有一个选择:

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

        即: {{ 博客内容 | strip_html |截断字数:100 }}

        【讨论】:

          猜你喜欢
          • 2013-03-07
          • 1970-01-01
          • 1970-01-01
          • 2013-10-23
          • 2017-03-21
          • 1970-01-01
          • 1970-01-01
          • 2013-12-18
          • 1970-01-01
          相关资源
          最近更新 更多