eleventy-plugin-wordcount-extended
An Eleventy universal plugin to An eleventy plugin to count and output the number of words in a string.
Nunjuck's buildin filter wordcount
does not correctly calculate the word count of non-ASCII strings, for example, the following code generates 1 1
, the third line is ignored by nunjuck.
{{ "Eleventy" | wordcount }}
{{ "Eleventy, 一个更简单的静态站点生成器" | wordcount }}
{{ "😀😁😂😃😄😅😆😇😈" | wordcount }}
With this version of wordcount
, the result would be 1 14 9
.
Installation
Available on npm.
npm install eleventy-plugin-wordcount-extended --save
Open up your Eleventy config file (probably .eleventy.js
) and add the plugin:
const wordcountPlugin = require("eleventy-plugin-wordcount-extended");
module.exports = function (eleventyConfig) {
eleventyConfig.addPlugin(wordcountPlugin);
};
Usage
In your (Nunjucks, Liquid or Handlebars) templates, use the following syntax to get the word count of a string:
// nunjucks/liquid
{{ content | wordcount }}
// handlebars
{{{ wordcount content }}}
Note that the wordcount
filter from this plugin will override that of nunjuck, if this is not what you want, you can fork the repository, edit the entry point file, and replace wordcount
with other names:
module.exports = function (eleventyConfig) {
eleventyConfig.addFilter("wc", wordcount);
};
Then apply it to the templates:
{{ content | wc }}