Updated Eleventy Post Stats Plugin
Posted: July 7, 2023 | Categories: Eleventy
I published an update to my Eleventy Plugin Post Statistics plugin today. I intend to add post paragraph count, post word count, and, perhaps, post code block count to the plugin, so this version sets everything up for that work.
Changes in This Version
Changed count
to postCount
One of things I noticed after I published the plugin is that I erred in naming the post count property in the JSON object generated by the plugin. As you can see in the original example, I called the property count
when I really should have called it postCount
to be more clear in what/which count is reflected in the data.
{
"avgDays": 44.81,
"count": 16,
"firstPostDate": "2021-04-01T00:00:00.000Z",
"lastPostDate": "2023-03-19T00:00:00.000Z",
"years": [
{ "year": 2021, "count": 8, "avgDays": 24 },
{ "year": 2022, "count": 5, "avgDays": 81.8 },
{ "year": 2023, "count": 3, "avgDays": 38.67 }
]
}
In today's release, I changed the property name to postCount
as shown below:
{
"avgDays": 44.81,
"postCount": 16,
"firstPostDate": "2021-04-01T00:00:00.000Z",
"lastPostDate": "2023-03-19T00:00:00.000Z",
"years": [
{ "year": 2021, "postCount": 8, "avgDays": 24 },
{ "year": 2022, "postCount": 5, "avgDays": 81.8 },
{ "year": 2023, "postCount": 3, "avgDays": 38.67 }
]
}
This is a breaking change, so in any pages that display data generated by the plugin, you must change all references to count
to postCount
like the following examples from the plugin's sample index.liquid
file:
<li>
<strong>Post Count:</strong>
{{ collections.postStats.postCount }}</li>
<li>
The following displays the generated data in table format:
{% for year in collections.postStats.years %}
<tr>
<td>{{ year.year }}</td>
<td>{{ year.postCount }}</td>
<td>{{ year.avgDays }}</td>
</tr>
{% endfor %}
Because of the breaking change, I incremented the plugin's minor
version number so existing Eleventy projects will continue to use the older version until you manually upgrade the plugin in your project(s).
Debug Mode
I wanted to understand how to add configuration options to an Eleventy plugin, so I added a debugMode
option that outputs just a tiny little bit more output to the console when the plugin executes.
To enable debugMode
, change the plugin installation code in your project's eleventy.config.js
to the following:
eleventyConfig.addPlugin(postStats, { debugMode: true });
I plan on adding new features to the plugin, so I expect to add more options to the options
object over time. Stay tuned on that one.
TypeScript Conversion
Finally, I converted the project to TypeScript because, well, I wanted to. I enjoy the developer efficiencies I get by using TypeScript, so I converted the project. It only took me a few minutes and now future enhancements will be easier.
Next Post: Batching YAML Front Matter Updates
Previous Post: Eleventy Filter Parameters
If this content helps you in some way, please consider buying me a coffee.
Header image: Eleventy Project Home Page.