summaryrefslogtreecommitdiff
path: root/_includes
diff options
context:
space:
mode:
authorjeffreytse <jeffreytse.mail@gmail.com>2021-01-11 18:33:35 +0800
committerjeffreytse <jeffreytse.mail@gmail.com>2021-01-11 18:33:52 +0800
commita037056663c3ac910742d4f53834bae9a283f222 (patch)
tree87d2cbc62fd140939c2d73fe229a095ead4c520d /_includes
parent48d4da4f1b0ee6d8a095fca301e6f3486a35f0b2 (diff)
feat: add some article-related functions
These functions include get_article_excerpt and get_article_words.
Diffstat (limited to '_includes')
-rw-r--r--_includes/functions/get_article_excerpt.html35
-rw-r--r--_includes/functions/get_article_words.html26
2 files changed, 61 insertions, 0 deletions
diff --git a/_includes/functions/get_article_excerpt.html b/_includes/functions/get_article_excerpt.html
new file mode 100644
index 0000000..da1f914
--- /dev/null
+++ b/_includes/functions/get_article_excerpt.html
@@ -0,0 +1,35 @@
+{% if include.params.article %}
+ {% 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.excerpt_size %}
+ {% assign excerpt_size = include.params.excerpt_size %}
+{% else %}
+ {% assign excerpt_size = excerpt_size | default: 800 %}
+{% endif %}
+
+{%- include functions.html func='get_article_words' -%}
+{% assign words = return %}
+
+{% assign _article = article | strip_html %}
+{% assign _words = _article | size %}
+
+{% if lang != "en" %}
+ {% assign _size = words
+ | times: 1.0
+ | divided_by: _words
+ | times: excerpt_size
+ | round %}
+{% endif %}
+
+{% if _size > excerpt_size %}
+ {% assign _size = excerpt_size %}
+{% endif %}
+
+{% assign return = _article | truncate: _size %}
diff --git a/_includes/functions/get_article_words.html b/_includes/functions/get_article_words.html
new file mode 100644
index 0000000..69db9c6
--- /dev/null
+++ b/_includes/functions/get_article_words.html
@@ -0,0 +1,26 @@
+{% if include.params.article %}
+ {% 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 %}
+
+{% 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 return = words %}