Feature: Add reading time to posts

This commit is contained in:
Cotes Chung 2020-12-17 15:17:21 +08:00
parent 87ba30a393
commit b9ffe15ffb
6 changed files with 66 additions and 24 deletions

7
_data/read_time.yml Normal file
View File

@ -0,0 +1,7 @@
# words per minute
wpm: 180
# minimum value
min_time: 1
prompt: min

15
_includes/read-time.html Normal file
View File

@ -0,0 +1,15 @@
<!--
Calculate the post's reading time, and display the word count in tooltip
-->
{% assign words = include.content | strip_html | number_of_words: "auto" %}
{% assign read_time = words | divided_by: site.data.read_time.wpm %}
{% unless read_time > 0 %}
{% assign read_time = site.data.read_time.min_time %}
{% endunless %}
<!-- return element -->
<span class="readtime" data-toggle="tooltip" data-placement="bottom" title="{{ words }} words">
{{- read_time -}}{{" "}}{{- site.data.read_time.prompt -}}
</span>

View File

@ -65,9 +65,13 @@ layout: page
<div class="post-meta text-muted"> <div class="post-meta text-muted">
<!-- posted date --> <!-- posted date -->
<i class="far fa-clock fa-fw"></i> <i class="far fa-calendar fa-fw"></i>
{% include timeago.html date=post.date tooltip=true %} {% include timeago.html date=post.date tooltip=true %}
<!-- time to read -->
<i class="far fa-clock fa-fw"></i>
<span>{% include read-time.html content=post.content %}</span>
<!-- page views --> <!-- page views -->
{% if site.google_analytics.pv.enabled %} {% if site.google_analytics.pv.enabled %}
<i class="far fa-eye fa-fw"></i> <i class="far fa-eye fa-fw"></i>
@ -75,6 +79,7 @@ layout: page
<i class="fas fa-spinner fa-spin fa-fw"></i> <i class="fas fa-spinner fa-spin fa-fw"></i>
</span> </span>
{% endif %} {% endif %}
</div> </div>
</div> <!-- .post-review --> </div> <!-- .post-review -->

View File

@ -18,33 +18,31 @@ layout: default
<div class="post-meta text-muted d-flex flex-column"> <div class="post-meta text-muted d-flex flex-column">
<!-- Published date and author --> <!-- Published date and author -->
<div> <div>
Posted
{% include timeago.html date=page.date tooltip=true %} {% include timeago.html date=page.date tooltip=true %}
by by
<span class="author"> <span class="author">
{% if page.author %} {{ page.author | default: site.author }}
{{ page.author }}
{% else %}
{{ site.author }}
{% endif %}
</span> </span>
</div> </div>
<!-- lastmod -->
{% if page.last_modified_at %}
<div> <div>
Updated <!-- lastmod -->
{% include timeago.html date=page.last_modified_at class="lastmod" tooltip=true %} {% if page.last_modified_at %}
</div> <span>
{% endif %} Updated
{% include timeago.html date=page.last_modified_at class="lastmod" tooltip=true %}
</span>
{% endif %}
<!-- page views --> <!-- read time -->
{% if site.google_analytics.pv.enabled %} {% include read-time.html content=content %}
<div>
<!-- page views -->
{% if site.google_analytics.pv.enabled %}
<span id="pv" class="pageviews"><i class="fas fa-spinner fa-spin fa-fw"></i></span> <span id="pv" class="pageviews"><i class="fas fa-spinner fa-spin fa-fw"></i></span>
views {% endif %}
</div> </div>
{% endif %}
</div> <!-- .post-meta --> </div> <!-- .post-meta -->

View File

@ -29,7 +29,7 @@
i { i {
font-size: 0.73rem; font-size: 0.73rem;
&:not(:first-child) { // post-meta icons on the homepage &:not(:first-child) { // post-meta icons on the homepage
margin-left: 1.5rem; margin-left: 1rem;
} }
} }
} }

View File

@ -27,6 +27,27 @@ $prompt-newer: "{{ site.data.label.post.button.next }}";
border-color: var(--btn-border-color); border-color: var(--btn-border-color);
} }
@mixin dot($pl: 0.2rem, $pr: 0.4rem) {
content: "\2022";
color: rgba(158, 158, 158, 0.8);
padding-left: $pl;
padding-right: $pr;
}
.post .post-meta {
> div:nth-child(2) {
> span:not(:first-child)::before {
@include dot;
}
}
#pv::after {
content: " views";
}
.readtime::after {
content: " read";
}
}
.post-content { .post-content {
> ol, > ul, > dl { > ol, > ul, > dl {
padding-left: 2rem; padding-left: 2rem;
@ -318,11 +339,7 @@ $prompt-newer: "{{ site.data.label.post.button.next }}";
.post { .post {
.post-meta { .post-meta {
>div:not(:first-child)::before { >div:not(:first-child)::before {
content: "\2022"; @include dot(.5rem, .2rem);
color: rgba(158, 158, 158, 0.8);
font-weight: bold;
padding-left: 0.5rem;
padding-right: 0.3rem;
} }
&.flex-column { &.flex-column {
-webkit-box-orient: horizontal!important; -webkit-box-orient: horizontal!important;