fix: reading time was incorrect for cjk words (#20)

The liquid filter `number_of_words` is for english words, so here
we use a calculation to estimate accurately the words, especially
when the language is not English.
This commit is contained in:
jeffreytse 2021-01-11 11:18:35 +08:00
parent e3be95fe06
commit 48d4da4f1b
3 changed files with 24 additions and 2 deletions

View file

@ -2,14 +2,34 @@
{% assign article = include.params.article %}
{% endif %}
{% if include.params.lang %}
{% assign lang = include.params.lang %}
{% else %}
{% assign lang = lang | default: site.lang | default: "en" %}
{% endif %}
{% if include.params.speed %}
{% assign speed = include.params.speed %}
{% else %}
{% assign speed = 160 %}
{% endif %}
{% assign total_mins = article
| number_of_words
{% assign words = article | number_of_words %}
{% if lang != "en" %}
{% assign words = words
| times: 0.6
| round %}
{% assign words = article
| strip_html
| strip_newlines
| size
| times: 0.4
| plus: words
| round %}
{% endif %}
{% assign total_mins = words
| divided_by: speed
| at_least: 1 %}

View file

@ -13,6 +13,7 @@
{%- assign date_format = site.yat.date_format | default: "%b %-d, %Y" -%}
{% assign article = post.content %}
{% assign lang = post.lang %}
{%- include functions.html func='get_reading_time' -%}
{% assign reading_time = return %}

View file

@ -9,6 +9,7 @@
</time>
{% assign article = page.content %}
{% assign lang = page.lang %}
{%- include functions.html func='get_reading_time' -%}
{% assign reading_time = return %}
<span class="post-reading-time left-vsplit"><i class="fa fa-clock-o"></i> {{ reading_time }}</span>