简体   繁体   English

如何在 Jekyll 中获取帖子摘录?

[英]How can I get a post excerpt in Jekyll?

I'm creating a new blog using Jekyll .我正在使用Jekyll创建一个新博客。

On the main page, there will be a list of my 10 most recent posts.在主页上,将列出我最近发表的 10 篇文章。

The entries on this list will include a title, the post date, and an excerpt , most likely the first paragraph.此列表中的条目将包括标题、发布日期和摘录,很可能是第一段。

I'm only familiar with using Jekyll for basic templates, so I can either put only a variable in the page, or include the entire post.我只熟悉将 Jekyll 用于基本模板,因此我可以只在页面中放置一个变量,也可以包含整个帖子。

Is there a way to somehow avoid using post.content in the paginator, and only include up to a certain point in the post, which I define (eg ``{% endexcerpt %}`?有没有办法避免在分页器中使用post.content ,并且只包含我定义的帖子中的某个点(例如“{% endexcerpt %}”?

Something like {{ post.content | strip_html | truncatewords: 50 }}{{ post.content | strip_html | truncatewords: 50 }} {{ post.content | strip_html | truncatewords: 50 }} {{ post.content | strip_html | truncatewords: 50 }} produces a more consistent excerpt. {{ post.content | strip_html | truncatewords: 50 }}产生更一致的摘录。 It gets the first 50 words and strips any formatting.它获取前 50 个单词并去除任何格式。

Sure, you can use {{ post.excerpt }} in place of {{ post.content }} .当然,您可以使用{{ post.excerpt }}代替{{ post.content }}

You can also manually override the automatically generated excerpts if you don't like them.如果您不喜欢自动生成的摘录,也可以手动覆盖它们。

Full documentation on how to do this here: http://jekyllrb.com/docs/posts/#post-excerpts关于如何在此处执行此操作的完整文档: http : //jekyllrb.com/docs/posts/#post-excerpts

To get a custom length excerpt for each post, you can add a excerpt_separator variable in front matter of your post.要为每个帖子获取自定义长度摘录,您可以在帖子的前面添加一个excerpt_separator变量。 If you set this variable to <!--end_excerpt--> , then post.excerpt will include all content before <!--end_excerpt--> .如果您将此变量设置为<!--end_excerpt--> ,则post.excerpt将包含<!--end_excerpt-->之前的所有内容。

---
excerpt_separator: <!--end_excerpt-->
---

This is included in excerpts.

This is also included in excerpts.

<!--end_excerpt-->

But this is not.

To save yourself the effort of adding excerpt_separator to front matter of each post, you can simply set it in _config.yml .为了省去将excerpt_separator添加到每个帖子的前端的工作,您可以简单地在_config.yml设置它。

Use使用

 {{ post.content | markdownify | strip_html | truncatewords: 50 }}

instead of {{ post.excerpt }} or {{ post.content }} .而不是{{ post.excerpt }}{{ post.content }}

This will give consistent length blocks of unformatted text with no raw markdown content in them.这将提供一致长度的无格式文本块,其中没有原始 Markdown 内容。 Tidy.整洁。


Thanks to this comment by @karolis-ramanauskas for the answer, I've made it a proper answer so it can get better visibility.感谢@karolis-ramanauskas 的这条评论给出的答案,我已经把它作为一个正确的答案,所以它可以得到更好的可见性。

What worked for me was: add this to the config.yml : excerpt_separator: "<!--more-->" #global excerpt separator for the blog posts and remember to restart the jekyll local server.对我有用的是:将其添加到 config.yml : excerpt_separator: "<!--more-->" #global excerpt separator for the blog posts并记住重新启动 jekyll 本地服务器。 All changes to config.yml requires a restart to take effect 👌所有对 config.yml 的更改都需要重启才能生效👌

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM