UNPKG

eleventy-plugin-wordcount

Version:

Eleventy plugin word count; which calcutates the number of words in a text corpus.

40 lines (32 loc) 816 B
const tag = new RegExp("(<[^>]+>)", "g"); const entities = new RegExp("(&w+;)", "gi"); function htmlToPlainText(string) { return `${string || ""}` .trim() .replace(/\s+/g, " ") .replace(tag, "") .replace(entities, "") .trim(); } function plainTextMetadata(text) { const words = `${text || ""}` .trim() .split(/\n+/) .filter((s) => s) .map((s) => s.split(/\s+/).length); return { words }; } function wordCountCallback(value) { const text = htmlToPlainText(value); const { words } = plainTextMetadata(text); return words.reduce((a, c) => a + c); } function wordCount(eleventyConfig) { eleventyConfig.addFilter("wordCount", wordCountCallback); } module.exports = { htmlToPlainText, plainTextMetadata, wordCountCallback, wordCount, };