This code can be used as an eleventy filter to create the reading time (in minutes) of a piece of content.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Calculate the reading time, in minutes, of a post | |
* | |
* Assumptions: | |
* - average reading time is 240 words per minute | |
* source: https://bit.ly/3HCogSr, "Most Comprehensive | |
* Review To Date Finds The Average Person’s Reading | |
* Speed Is Slower Than Previously Thought" | |
* | |
* Output: | |
* - reading time is rounded to the nearest minute | |
* - in the case of less than 1 minute, reading time is | |
* displayed as "less than a minute" | |
* | |
* @param {String} text | |
*/ | |
module.exports = function (text) { | |
var content = new String(text); | |
const speed = 240; // reading speed in words per minute | |
// remove all html elements | |
var re = /(<.*?>)|(<.*?>)/gi; | |
var plain = content.replace(re, ""); | |
// replace all newlines and 's with spaces | |
var plain = plain.replace(/\n+|'s/g, " "); | |
// create array of all the words in the post & count them | |
var words = plain.split(" "); | |
var count = words.length; | |
// calculate the reading time | |
var readingTime = Math.round(count / speed); | |
if (readingTime === 0) { | |
return "Less than 1 minute to read"; | |
} else if (readingTime === 1) { | |
return " About 1 minute to read"; | |
} else { | |
return "About " + readingTime + " minutes to read"; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment