Skip to content

Commit d8c15b6

Browse files
committed
Feature: Add reading time to posts
1 parent 3f65f1e commit d8c15b6

File tree

6 files changed

+66
-24
lines changed

6 files changed

+66
-24
lines changed

_data/read_time.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# words per minute
2+
wpm: 180
3+
4+
# minimum value
5+
min_time: 1
6+
7+
prompt: min

_includes/read-time.html

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<!--
2+
Calculate the post's reading time, and display the word count in tooltip
3+
-->
4+
{% assign words = include.content | strip_html | number_of_words: "auto" %}
5+
6+
{% assign read_time = words | divided_by: site.data.read_time.wpm %}
7+
8+
{% unless read_time > 0 %}
9+
{% assign read_time = site.data.read_time.min_time %}
10+
{% endunless %}
11+
12+
<!-- return element -->
13+
<span class="readtime" data-toggle="tooltip" data-placement="bottom" title="{{ words }} words">
14+
{{- read_time -}}{{" "}}{{- site.data.read_time.prompt -}}
15+
</span>

_layouts/home.html

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,16 +65,21 @@ <h1><a href="{{ post.url | relative_url }}">{{ post.title }}</a></h1>
6565

6666
<div class="post-meta text-muted">
6767
<!-- posted date -->
68-
<i class="far fa-clock fa-fw"></i>
68+
<i class="far fa-calendar fa-fw"></i>
6969
{% include timeago.html date=post.date tooltip=true %}
7070

71+
<!-- time to read -->
72+
<i class="far fa-clock fa-fw"></i>
73+
<span>{% include read-time.html content=post.content %}</span>
74+
7175
<!-- page views -->
7276
{% if site.google_analytics.pv.enabled %}
7377
<i class="far fa-eye fa-fw"></i>
7478
<span id="pv_{{-post.title-}}" class="pageviews">
7579
<i class="fas fa-spinner fa-spin fa-fw"></i>
7680
</span>
7781
{% endif %}
82+
7883
</div>
7984
</div> <!-- .post-review -->
8085

_layouts/post.html

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,33 +18,31 @@ <h1 data-toc-skip>{{ page.title }}</h1>
1818
<div class="post-meta text-muted d-flex flex-column">
1919
<!-- Published date and author -->
2020
<div>
21-
Posted
2221
{% include timeago.html date=page.date tooltip=true %}
2322
by
2423
<span class="author">
25-
{% if page.author %}
26-
{{ page.author }}
27-
{% else %}
28-
{{ site.author }}
29-
{% endif %}
24+
{{ page.author | default: site.author }}
3025
</span>
3126
</div>
3227

33-
<!-- lastmod -->
34-
{% if page.last_modified_at %}
3528
<div>
36-
Updated
37-
{% include timeago.html date=page.last_modified_at class="lastmod" tooltip=true %}
38-
</div>
39-
{% endif %}
29+
<!-- lastmod -->
30+
{% if page.last_modified_at %}
31+
<span>
32+
Updated
33+
{% include timeago.html date=page.last_modified_at class="lastmod" tooltip=true %}
34+
</span>
35+
{% endif %}
4036

41-
<!-- page views -->
42-
{% if site.google_analytics.pv.enabled %}
43-
<div>
37+
<!-- read time -->
38+
{% include read-time.html content=content %}
39+
40+
<!-- page views -->
41+
{% if site.google_analytics.pv.enabled %}
4442
<span id="pv" class="pageviews"><i class="fas fa-spinner fa-spin fa-fw"></i></span>
45-
views
43+
{% endif %}
44+
4645
</div>
47-
{% endif %}
4846

4947
</div> <!-- .post-meta -->
5048

assets/css/home.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
i {
3030
font-size: 0.73rem;
3131
&:not(:first-child) { // post-meta icons on the homepage
32-
margin-left: 1.5rem;
32+
margin-left: 1rem;
3333
}
3434
}
3535
}

assets/css/post.scss

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,27 @@ $prompt-newer: "{{ site.data.label.post.button.next }}";
2727
border-color: var(--btn-border-color);
2828
}
2929

30+
@mixin dot($pl: 0.2rem, $pr: 0.4rem) {
31+
content: "\2022";
32+
color: rgba(158, 158, 158, 0.8);
33+
padding-left: $pl;
34+
padding-right: $pr;
35+
}
36+
37+
.post .post-meta {
38+
> div:nth-child(2) {
39+
> span:not(:first-child)::before {
40+
@include dot;
41+
}
42+
}
43+
#pv::after {
44+
content: " views";
45+
}
46+
.readtime::after {
47+
content: " read";
48+
}
49+
}
50+
3051
.post-content {
3152
> ol, > ul, > dl {
3253
padding-left: 2rem;
@@ -318,11 +339,7 @@ $prompt-newer: "{{ site.data.label.post.button.next }}";
318339
.post {
319340
.post-meta {
320341
>div:not(:first-child)::before {
321-
content: "\2022";
322-
color: rgba(158, 158, 158, 0.8);
323-
font-weight: bold;
324-
padding-left: 0.5rem;
325-
padding-right: 0.3rem;
342+
@include dot(.5rem, .2rem);
326343
}
327344
&.flex-column {
328345
-webkit-box-orient: horizontal!important;

0 commit comments

Comments
 (0)