diff options
author | jeffreytse <jeffreytse.mail@gmail.com> | 2021-01-11 11:18:35 +0800 |
---|---|---|
committer | jeffreytse <jeffreytse.mail@gmail.com> | 2021-01-11 11:18:35 +0800 |
commit | 48d4da4f1b0ee6d8a095fca301e6f3486a35f0b2 (patch) | |
tree | 4a73066c24e5d8f5129eedaa3ff41a829c4f9565 /_includes/functions | |
parent | e3be95fe06ca57f7fa774e7b3d9efafe77cbb802 (diff) |
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.
Diffstat (limited to '_includes/functions')
-rw-r--r-- | _includes/functions/get_reading_time.html | 24 |
1 files changed, 22 insertions, 2 deletions
diff --git a/_includes/functions/get_reading_time.html b/_includes/functions/get_reading_time.html index 7747108..e1d23fe 100644 --- a/_includes/functions/get_reading_time.html +++ b/_includes/functions/get_reading_time.html @@ -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 %} |