Word Count Statistics Without Plugins

Word Count Statistics Without Plugins

This article was published , which makes this post and me old when I published it.

There are 630 words in this article, and it will probably take you less than 4 minutes to read it.

When I wanted to implement a statistics page inspired by Luke Harris, I went on a search for how to implement it in Jekyll. In my search, I came across Jake Lee’s article which mentioned the plugin Jekyll-Posts-Word-Count by Matt Gemmell. Since I am still in the GitHub Pages Jekyll sandbox, I am unable to use external plugins that are not already bundled into the GitHub Pages environment. However, I did not fret, for I knew that this would essentially amount to a singular for loop, something that is very easy to do in Liquid.

Thus I came up with the following code to calculate the total number of words in Jekyll posts, the average number of words per post, as well as the longest and shortest Jekyll posts with their respective word counts.

<!-- create variable for storing number of words across all posts -->
{% assign total_words = 0 %}

<!-- create variables for storing largest number of words + the longest post -->
{% assign max_words = site.categories.Writing.first.content | number_of_words %}
{% assign longest_article = site.categories.Writing.first %}

<!-- create variables for storing smallest number of words + the shortest post -->
{% assign min_words = max_words %}
{% assign shortest_article = longest_article %}

<!-- min/max and longest/shortest are initialized to the same values -->
<!-- because they are just placeholder values -->
<!-- iterate through all posts within the Writing category (could be any category you want) -->
<!-- you could also do this by tag with site.tags.tag_name -->
{% for post in site.categories.Writing %}
    <!-- get word count of post -->
    {% assign word_count = post.content | number_of_words %}

    <!-- add word count of the post to the total count -->
    {% assign total_words = total_words | plus: word_count %}
    <!-- update longest article -->
    {% if word_count > max_words %}
        {% assign max_words = word_count %}
        {% assign longest_article = post %}
    <!-- update shortest article -->
    {% elsif word_count < min_words %}
        {% assign min_words = word_count %}
        {% assign shortest_article = post %}
    {% endif %}
{% endfor %}

<!-- calculate the average number of words per article -->
{% assign num_articles = site.categories.Writing | size %}
{% assign avg_words = total_words | divided_by: num_articles %}

You can see this code in action in the Statistics section on my Writing page.

Related Articles

Post and Author Age Without Plugins

How to display post and author age in Jekyll via JavaScript.

Numeric Value Humanization Without Plugins

Numeric Value Humanization Without Plugins

Getting Posts by Tag

How I improved my Liquid code in Jekyll to display posts by tag.