Word Count Statistics Without Plugins

Word Count Statistics Without Plugins

This article was tagged with: Jekyll

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

This article was published 2023-08-28 00:00:00 -0400, which makes this post and me old when I published 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 Articles page.

Other Jekyll Articles

Search with SimpleJekyllSearch

My modifications to Jake Lee's configuration of SimpleJekyllSearch to fit my Digital Garden.

Creating a Flexible Permalinking System in Jekyll

How to create and maintain a flexible permalinking system in Jekyll for your posts.

Using jekyll-feed + XSLT with GitHub Pages

A guide on how to set up and use the jekyll-feed plugin on GitHub Pages to create a stylized feed.xml for RSS feeds.

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