loading...

Add reading time in Cobalt

booyaa profile image Mark Sta Ana Originally published at booyaa.wtf on ・1 min read

I wanted to add an approximate reading time to each of my blog posts, like those seen in medium posts. There's a lot of really nice javascript libraries out that, but I kinda figured this defeat the whole purpose
of using a static site generator.

I was also looking for an excuse to use liquid (the template rendering engine for Cobalt).

It's a fairly simplistic approach, perform a word count and divide by 200 which is the average of words read per min.

{% assign reading_wpm = 200 %}
{% assign word_count = page.content | split: " " | size %}
{% assign reading_time = word_count | divided_by: 200 %}
{% case reading_time %}
  {% when 0 %}
    {% assign phrase = "less than a minute." %}
  {% when 1 %}
    {% assign phrase = "about a minute." %}
  {% else %}
    {% assign phrase = " minutes." | prepend: reading_time %}
{% endcase %}
Reading time: {{ phrase }}

Posted on by:

booyaa profile

Mark Sta Ana

@booyaa

He/Him/Dat Boi. SRE for an Agribiz SaaS Startup. Terrorizing Azure with Terraform. Rusticating Windows.

Discussion

pic
Editor guide
 
[deleted]