简体   繁体   English

从主页/任何其他页面获取 Jekyll Pages 中特定帖子的阅读时间

[英]Get the read time for a specific post in Jekyll Pages from home page/ any other page

I am new to liquid and Jekyll, using the below code to calculate the post read time on the post page.我是 Liquid 和 Jekyll 的新手,使用下面的代码来计算帖子页面上的帖子阅读时间。 But how should I calculate the reading time on the home page or any other page to display it below a post title?但是我应该如何计算主页或任何其他页面上的阅读时间以将其显示在帖子标题下方?

When I use the same code on the homepage as well, I get different read times on the home page and inside that particular post.当我也在主页上使用相同的代码时,我会在主页和特定帖子中获得不同的阅读时间。

I think I am not able to get the context of that post to count the number of words on some other page.我想我无法获得该帖子的上下文来计算其他页面上的字数。 Please suggest.请建议。

<span class="reading-time" title="Estimated read time">
  {% assign words = content | number_of_words %}
  {% if words < 360 %}
    1 min
  {% else %}
    {{ words | divided_by:180 }} mins
  {% endif %}
</span>

This is probably happening because, on the home page, you also have some HTML formatting your page.这可能是因为在主页上,您还有一些 HTML 正在格式化您的页面。

Strictly speaking, you should indeed strip the HTML out of your content before counting the words, to have an accurate reading time.严格来说,您确实应该在计算单词之前从内容中去除 HTML,以获得准确的阅读时间。

So, using the strip_html filter could be a solution:因此,使用strip_html过滤器可能是一个解决方案:

{% assign words = content | strip_html | number_of_words %}

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

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