GuideHow-to recipes
Add reading time
Count the words of a page and show an estimated reading time, with Liquid filters in a layout.
A layout receives the page's HTML as content. Removing the tags and splitting the text on spaces gives a word count,
and dividing it by a reading speed gives minutes. No step or plugin is needed.
Compute it in the layoutLink to this heading
<!doctype html>
<html>
<body>
<article>
{%- assign words = content | strip_html | split: " " | size %}
{%- assign minutes = words | divided_by: 200.0 | ceil | at_least: 1 %}
<p class="reading-time">{{ minutes }} min read</p>
{{ content }}
</article>
</body>
</html>
For a page of 461 words, it writes:
<p class="reading-time">3 min read</p>
strip_htmlremoves tags, comments, scripts and styles.split: " "splits on any whitespace, newlines included, and skips empty strings, sosizeis the number of words.divided_by: 200.0divides as a decimal number (with200it would be an integer division),ceilrounds up andat_least: 1avoids "0 min read" on short pages. 200 words per minute is a common estimate for technical text; change it to suit your content.- The text of code blocks counts as words.
Reuse itLink to this heading
Put the two lines in an include, _includes/reading-time.liquid:
{%- assign words = content | strip_html | split: " " | size -%}
{%- assign minutes = words | divided_by: 200.0 | ceil | at_least: 1 -%}
<p class="reading-time">{{ minutes }} min read</p>
and use it in any layout:
{% include 'reading-time.liquid' %}
Show it in a list of postsLink to this heading
The items of a collection have the data of each page, not its HTML: the default collections step runs before
rendering. To compute reading times in an index page, add a collect step after markdown with content: true, and
read post.content in the list. Add an RSS feed shows that workflow.